Skip to content

Commit daa7d24

Browse files
committed
aws translate
1 parent a755408 commit daa7d24

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed

databox/api/src/Attribute/AttributeManager.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,11 @@ public function getAttributeDefinitionBySlug(string $workspaceId, string $slug):
2121
'workspace' => $workspaceId,
2222
]);
2323
}
24+
25+
public function getAttributeDefinitions(string $workspaceId): iterable
26+
{
27+
return $this->em->getRepository(AttributeDefinition::class)->findAll([
28+
'workspace' => $workspaceId,
29+
]);
30+
}
2431
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Integration\Aws\Translate;
6+
7+
use Alchemy\Workflow\Model\Workflow;
8+
use App\Integration\Aws\AbstractAwsIntegration;
9+
use App\Integration\IntegrationConfig;
10+
use App\Integration\WorkflowHelper;
11+
use App\Integration\WorkflowIntegrationInterface;
12+
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
13+
14+
class AwsTranslateIntegration extends AbstractAwsIntegration implements WorkflowIntegrationInterface
15+
{
16+
public function getWorkflowJobDefinitions(IntegrationConfig $config, Workflow $workflow): iterable
17+
{
18+
yield WorkflowHelper::createIntegrationJob(
19+
$config,
20+
TranslateAction::class,
21+
);
22+
}
23+
24+
public function buildConfiguration(NodeBuilder $builder): void
25+
{
26+
$this->addCredentialConfigNode($builder);
27+
$this->addRegionConfigNode($builder);
28+
29+
$builder
30+
->scalarNode('source_lng')
31+
->isRequired()
32+
->cannotBeEmpty()
33+
->info('The language code to translate from')
34+
->end()
35+
->scalarNode('destination_lng')
36+
->isRequired()
37+
->cannotBeEmpty()
38+
->info('The language code of to translate to')
39+
->end()
40+
;
41+
42+
$builder->append($this->createBudgetLimitConfigNode(true));
43+
}
44+
45+
protected function getSupportedRegions(): array
46+
{
47+
return [
48+
'ap-northeast-1',
49+
'ap-northeast-2',
50+
'ap-south-1',
51+
'ap-southeast-1',
52+
'ap-southeast-2',
53+
'eu-central-1',
54+
'eu-west-1',
55+
'eu-west-2',
56+
'us-east-1',
57+
'us-east-2',
58+
'us-west-2',
59+
];
60+
}
61+
62+
public static function getTitle(): string
63+
{
64+
return 'AWS Translate';
65+
}
66+
67+
public static function getName(): string
68+
{
69+
return 'aws.translate';
70+
}
71+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Integration\Aws\Translate;
6+
7+
use Alchemy\Workflow\Executor\RunContext;
8+
use App\Api\Model\Input\Attribute\AssetAttributeBatchUpdateInput;
9+
use App\Api\Model\Input\Attribute\AttributeActionInput;
10+
use App\Asset\Attribute\AttributesResolver;
11+
use App\Attribute\AttributeInterface;
12+
use App\Attribute\AttributeManager;
13+
use App\Attribute\BatchAttributeManager;
14+
use App\Entity\Core\Attribute;
15+
use App\Integration\AbstractIntegrationAction;
16+
use App\Integration\ApiBudgetLimiter;
17+
use App\Integration\IfActionInterface;
18+
use App\Integration\IntegrationConfig;
19+
use Aws\Translate\TranslateClient;
20+
21+
class TranslateAction extends AbstractIntegrationAction implements IfActionInterface
22+
{
23+
public function __construct(
24+
private readonly ApiBudgetLimiter $apiBudgetLimiter,
25+
private readonly AttributesResolver $attributesResolver,
26+
private readonly AttributeManager $attributeManager,
27+
private BatchAttributeManager $batchAttributeManager,
28+
) {
29+
}
30+
31+
public function doHandle(RunContext $context): void
32+
{
33+
$config = $this->getIntegrationConfig($context);
34+
$asset = $this->getAsset($context);
35+
36+
$this->apiBudgetLimiter->acceptIntegrationApiCall($config);
37+
38+
$sourceLangage = $config['source_lng'];
39+
$destinationLanguage = $config['destination_lng'];
40+
41+
$attributeIndex = $this->attributesResolver->resolveAssetAttributes($asset, false);
42+
43+
$attrDefs = $this->attributeManager->getAttributeDefinitions($asset->getWorkspaceId());
44+
45+
foreach ($attrDefs as $attrDef) {
46+
if (!$attrDef->isTranslatable()) {
47+
continue;
48+
}
49+
50+
$text = $attributeIndex->getAttribute($attrDef->getId(), AttributeInterface::NO_LOCALE)?->getValue();
51+
if (empty($text)) {
52+
continue;
53+
}
54+
$client = $this->createClient($config);
55+
56+
$result = $client->translateText([
57+
'Settings' => [
58+
'Brevity' => 'ON',
59+
'Formality' => 'FORMAL',
60+
'Profanity' => 'MASK',
61+
],
62+
'SourceLanguageCode' => $sourceLangage,
63+
'TargetLanguageCode' => $destinationLanguage,
64+
65+
'Text' => $text,
66+
]);
67+
68+
$translatedText = $result->get('TranslatedText');
69+
70+
$input = new AssetAttributeBatchUpdateInput();
71+
$i = new AttributeActionInput();
72+
$i->definitionId = $attrDef->getId();
73+
$i->action = BatchAttributeManager::ACTION_SET;
74+
$i->origin = Attribute::ORIGIN_MACHINE;
75+
$i->originVendor = AwsTranslateIntegration::getName();
76+
$i->value = $translatedText;
77+
$i->locale = $destinationLanguage;
78+
$input->actions[] = $i;
79+
80+
$this->batchAttributeManager->handleBatch(
81+
$asset->getWorkspaceId(),
82+
[$asset->getId()],
83+
$input,
84+
null
85+
);
86+
87+
}
88+
}
89+
90+
private function createClient(IntegrationConfig $options): TranslateClient
91+
{
92+
return new TranslateClient([
93+
'region' => $options['region'],
94+
'credentials' => [
95+
'key' => $options['accessKeyId'],
96+
'secret' => $options['accessKeySecret'],
97+
],
98+
'version' => 'latest',
99+
]);
100+
}
101+
}

0 commit comments

Comments
 (0)