8000 [Translation] Add `--as-tree` option to `translation:pull` command by syffer · Pull Request #51153 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Translation] Add --as-tree option to translation:pull command #51153

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

Merged
merged 1 commit into from
Sep 20, 2023
Merged
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
1 change: 1 addition & 0 deletions src/Symfony/Component/Translation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Give current locale to `LocaleSwitcher::runWithLocale()`'s callback
* Add `--as-tree` option to `translation:pull` command to write YAML messages as a tree-like structure

6.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ protected function configure(): void
new InputOption('domains', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Specify the domains to pull.'),
new InputOption('locales', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Specify the locales to pull.'),
new InputOption('format', null, InputOption::VALUE_OPTIONAL, 'Override the default output format.', 'xlf12'),
new InputOption('as-tree', null, InputOption::VALUE_OPTIONAL, 'Write messages as a tree-like structure. Needs --format=yaml. The given value defines the level where to switch to inline YAML'),
])
->setHelp(<<<'EOF'
The <info>%command.name%</> command pulls translations from the given provider. Only
Expand Down Expand Up @@ -126,6 +127,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$locales = $input->getOption('locales') ?: $this->enabledLocales;
$domains = $input->getOption('domains');
$format = $input->getOption('format');
$asTree = (int) $input->getOption('as-tree');
$xliffVersion = '1.2';

if ($intlIcu && !$force) {
Expand All @@ -142,6 +144,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'path' => end($this->transPaths),
'xliff_version' => $xliffVersion,
'default_locale' => $this->defaultLocale,
'as_tree' => (bool) $asTree,
'inline' => $asTree,
];

if (!$domains) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ protected function getProviderCollection(ProviderInterface $provider, array $pro
return new TranslationProviderCollection($collection);
}

protected function createYamlFile(array $messages = ['node' => 'NOTE'], $targetLanguage = 'en', $fileNamePattern = 'messages.%locale%.yml'): string
{
$yamlContent = '';
foreach ($messages as $key => $value) {
$yamlContent .= "$key: $value\n";
}
$yamlContent .= "\n";

$filename = sprintf('%s/%s', $this->translationAppDir.'/translations', str_replace('%locale%', $targetLanguage, $fileNamePattern));
file_put_contents($filename, $yamlContent);

$this->files[] = $filename;

return $filename;
}

protected function createFile(array $messages = ['note' => 'NOTE'], $targetLanguage = 'en', $fileNamePattern = 'messages.%locale%.xlf', string $xlfVersion = 'xlf12'): string
{
if ('xlf12' === $xlfVersion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Translation\Command\TranslationPullCommand;
use Symfony\Component\Translation\Dumper\XliffFileDumper;
use Symfony\Component\Translation\Dumper\YamlFileDumper;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\Loader\XliffFileLoader;
use Symfony\Component\Translation\Loader\YamlFileLoader;
use Symfony\Component\Translation\Provider\ProviderInterface;
use Symfony\Component\Translation\Reader\TranslationReader;
use Symfony\Component\Translation\TranslatorBag;
Expand Down Expand Up @@ -235,6 +237,96 @@ public function testPullNewXlf20Messages()
, file_get_contents($filenameFr));
}

public function testPullNewYamlMessagesAsInlined()
{
$arrayLoader = new ArrayLoader();
$filenameEn = $this->createYamlFile(['note' => 'NOTE'], 'en', 'messages.%locale%.yml');
$filenameFr = $this->createYamlFile(['note' => 'NOTE'], 'fr', 'messages.%locale%.yml');
$locales = ['en', 'fr'];
$domains = ['messages'];

$providerReadTranslatorBag = new TranslatorBag();
$providerReadTranslatorBag->addCatalogue($arrayLoader->load([
'note' => 'NOTE',
'new.foo' => 'newFoo',
], 'en'));
$providerReadTranslatorBag->addCatalogue($arrayLoader->load([
'note' => 'NOTE',
'new.foo' => 'nouveauFoo',
], 'fr'));

$provider = $this->createMock(ProviderInterface::class);
$provider->expects($this->once())
->method('read')
->with($domains, $locales)
->willReturn($providerReadTranslatorBag);

$provider->expects($this->once())
->method('__toString')
->willReturn('null://default');

$tester = $this->createCommandTester($provider, $locales, $domains);
$tester->execute(['--locales' => ['en', 'fr'], '--domains' => ['messages'], '--format' => 'yml']);

$this->assertStringContainsString('[OK] New translations from "null" has been written locally (for "en, fr" locale(s), and "messages" domain(s)).', trim($tester->getDisplay()));
$this->assertEquals(<<<YAML
new.foo: newFoo
note: NOTE

YAML, file_get_contents($filenameEn));
$this->assertEquals(<<<YAML
new.foo: nouveauFoo
note: NOTE

YAML, file_get_contents($filenameFr));
}

public function testPullNewYamlMessagesAsTree()
{
$arrayLoader = new ArrayLoader();
$filenameEn = $this->createYamlFile(['note' => 'NOTE'], 'en', 'messages.%locale%.yml');
$filenameFr = $this->createYamlFile(['note' => 'NOTE'], 'fr', 'messages.%locale%.yml');
$locales = ['en', 'fr'];
$domains = ['messages'];

$providerReadTranslatorBag = new TranslatorBag();
$providerReadTranslatorBag->addCatalogue($arrayLoader->load([
'note' => 'NOTE',
'new.foo' => 'newFoo',
], 'en'));
$providerReadTranslatorBag->addCatalogue($arrayLoader->load([
'note' => 'NOTE',
'new.foo' => 'nouveauFoo',
], 'fr'));

$provider = $this->createMock(ProviderInterface::class);
$provider->expects($this->once())
->method('read')
->with($domains, $locales)
->willReturn($providerReadTranslatorBag);

$provider->expects($this->once())
->method('__toString')
->willReturn('null://default');

$tester = $this->createCommandTester($provider, $locales, $domains);
$tester->execute(['--locales' => ['en', 'fr'], '--domains' => ['messages'], '--format' => 'yml', '--as-tree' => 10]);

$this->assertStringContainsString('[OK] New translations from "null" has been written locally (for "en, fr" locale(s), and "messages" domain(s)).', trim($tester->getDisplay()));
$this->assertEquals(<<<YAML
new:
foo: newFoo
note: NOTE

YAML, file_get_contents($filenameEn));
$this->assertEquals(<<<YAML
new:
foo: nouveauFoo
note: NOTE

YAML, file_get_contents($filenameFr));
}

public function testPullForceMessages()
{
$arrayLoader = new ArrayLoader();
Expand Down Expand Up @@ -641,9 +733,11 @@ private function createCommand(ProviderInterface $provider, array $locales = ['e
{
$writer = new TranslationWriter();
$writer->addDumper('xlf', new XliffFileDumper());
$writer->addDumper('yml', new YamlFileDumper());

$reader = new TranslationReader();
$reader->addLoader('xlf', new XliffFileLoader());
$reader->addLoader('yml', new YamlFileLoader());

return new TranslationPullCommand(
$this->getProviderCollection($provider, $providerNames, $locales, $domains),
Expand Down
0