Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to specify translator by type via command option for Yves #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ bin/cli-toolkit translation:yves:generate es_ES --working-dir=../b2b-demo-market
bin/cli-toolkit translation:yves:generate es_ES fr_FR --working-dir=../b2b-demo-marketplace --translation-engine=deepl
```

3. Generate missing "category" and "product" translations Yves to Spanish from Spain (es_ES) with default engine.
```bash
bin/cli-toolkit translation:yves:generate es_ES --working-dir=../b2b-demo-marketplace --translator=category,product
```

### Generate translations for the Spryker Zed backoffice

```bash
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/AbstractYvesTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ protected function prepareTranslatedRecords(TranslationRequest $value, string $t
/**
* @return string
*/
abstract protected function getType(): string;
abstract public function getType(): string;

/**
* @return string
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/CategoryTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CategoryTranslator extends AbstractYvesTranslator
/**
* @return string
*/
protected function getType(): string
public function getType(): string
{
return 'category';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/CmsBlockTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CmsBlockTranslator extends AbstractYvesTranslator
/**
* @return string
*/
protected function getType(): string
public function getType(): string
{
return 'cms_block';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/CmsPageTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CmsPageTranslator extends AbstractYvesTranslator
/**
* @return string
*/
protected function getType(): string
public function getType(): string
{
return 'cms_page';
}
Expand Down
90 changes: 88 additions & 2 deletions src/Translator/Commands/YvesTranslationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
namespace SprykerCommunity\CliToolKit\Translator\Commands;

use Monolog\Logger;
use RuntimeException;
use SprykerCommunity\CliToolKit\Translator\AbstractYvesTranslator;
use SprykerCommunity\CliToolKit\Translator\CategoryTranslator;
use SprykerCommunity\CliToolKit\Translator\CmsBlockTranslator;
use SprykerCommunity\CliToolKit\Translator\CmsPageTranslator;
Expand All @@ -28,10 +30,16 @@
use SprykerCommunity\CliToolKit\Translator\ProductSetTranslator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class YvesTranslationCommand extends AbstractTranslationCommand
{
/**
* @var string
*/
protected const TRANSLATOR = 'translator';

/**
* @return void
*/
Expand All @@ -41,7 +49,14 @@ protected function configure(): void

$this
->setName('translation:yves:generate')
->setDescription('Generate Yves translations to the specified target locale');
->setDescription('Generate Yves translations to the specified target locale')
->addOption(
static::TRANSLATOR,
't',
InputOption::VALUE_REQUIRED,
'Comma separated list of translators to use',
null,
);
}

/**
Expand All @@ -54,8 +69,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
parent::execute($input, $output);

$translators = $this->getTranslators();

$this->validateTranslatorOption($translators, $input);

$translators = $this->filterTranslatorsByTypes($translators, $this->getTranslatorFilterTypes($input));

try {
foreach ($this->getTranslators() as $translator) {
foreach ($translators as $translator) {
$translator->setOutput($output);
$translator->translate($this->directory, $this->locales);
}
Expand Down Expand Up @@ -107,4 +128,69 @@ protected function getDirectory(?string $directory): string

return $directory;
}

/**
* @param array<\SprykerCommunity\CliToolKit\Translator\AbstractYvesTranslator> $availableTranslators
* @param \Symfony\Component\Console\Input\InputInterface $input
*
* @throws \RuntimeException
*
* @return void
*/
protected function validateTranslatorOption(array $availableTranslators, InputInterface $input): void
{
$translatorTypes = $this->getTranslatorFilterTypes($input);
$availableTranslatorTypes = array_map(
fn (AbstractYvesTranslator $translator): string => $translator->getType(),
$availableTranslators,
);

if (!count($translatorTypes)) {
return;
}

$invalidTranslatorTypes = array_diff($translatorTypes, $availableTranslatorTypes);

if (count($invalidTranslatorTypes)) {
throw new RuntimeException(sprintf(
'Invalid translator types "%s". Available types: "%s"',
implode(', ', $invalidTranslatorTypes),
implode(', ', $availableTranslatorTypes),
));
}
}

/**
* @param array<\SprykerCommunity\CliToolKit\Translator\AbstractYvesTranslator> $translators
* @param array<string> $types
*
* @return array<\SprykerCommunity\CliToolKit\Translator\AbstractYvesTranslator>
*/
protected function filterTranslatorsByTypes(array $translators, array $types): array
{
if (!count($types)) {
return $translators;
}

return array_filter(
$this->getTranslators(),
fn (AbstractYvesTranslator $translator): bool => in_array($translator->getType(), $types),
);
}

/**
* @param \Symfony\Component\Console\Input\InputInterface $input
*
* @return array<string>
*/
protected function getTranslatorFilterTypes(InputInterface $input): array
{
$optionTranslators = $input->getOption(static::TRANSLATOR);

if (!$optionTranslators) {
return [];
}

return explode(',', $optionTranslators);
}
}
2 changes: 1 addition & 1 deletion src/Translator/ContentBannerTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ContentBannerTranslator extends AbstractYvesTranslator
/**
* @return string
*/
protected function getType(): string
public function getType(): string
{
return 'content_banner';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/GlossaryTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected function prepareTranslatedRecords(TranslationRequest $value, string $t
/**
* @return string
*/
protected function getType(): string
public function getType(): string
{
return 'glossary';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/MerchantProfileTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MerchantProfileTranslator extends AbstractYvesTranslator
/**
* @return string
*/
protected function getType(): string
public function getType(): string
{
return 'merchant_profile';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/NavigationNodeCategoryTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class NavigationNodeCategoryTranslator extends AbstractYvesTranslator
/**
* @return string
*/
protected function getType(): string
public function getType(): string
{
return 'navigation_node';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/ProductAbstractTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ProductAbstractTranslator extends AbstractYvesTranslator
/**
* @return string
*/
protected function getType(): string
public function getType(): string
{
return 'product_abstract';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/ProductConcreteTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ProductConcreteTranslator extends AbstractYvesTranslator
/**
* @return string
*/
protected function getType(): string
public function getType(): string
{
return 'product_concrete';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/ProductDiscontinuedTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ProductDiscontinuedTranslator extends AbstractYvesTranslator
/**
* @return string
*/
protected function getType(): string
public function getType(): string
{
return 'product_discontinued';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/ProductLabelTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ProductLabelTranslator extends AbstractYvesTranslator
/**
* @return string
*/
protected function getType(): string
public function getType(): string
{
return 'product_label';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/ProductManagementAttributeTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ProductManagementAttributeTranslator extends AbstractYvesTranslator
/**
* @return string
*/
protected function getType(): string
public function getType(): string
{
return 'product_management_attribute';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/ProductOptionTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ProductOptionTranslator extends AbstractYvesTranslator
/**
* @return string
*/
protected function getType(): string
public function getType(): string
{
return 'product_option';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/ProductSearchAttributeTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ProductSearchAttributeTranslator extends AbstractYvesTranslator
/**
* @return string
*/
protected function getType(): string
public function getType(): string
{
return 'product_search_attribute';
}
Expand Down
2 changes: 1 addition & 1 deletion src/Translator/ProductSetTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ProductSetTranslator extends AbstractYvesTranslator
/**
* @return string
*/
protected function getType(): string
public function getType(): string
{
return 'product_set';
}
Expand Down
Loading