Skip to content

Commit f9327be

Browse files
feature #51153 [Translation] Add --as-tree option to translation:pull command (syffer)
This PR was merged into the 6.4 branch. Discussion ---------- [Translation] Add `--as-tree` option to `translation:pull` command | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes/no | New feature? | yes | Deprecations? | no | Tickets | n/a | License | MIT | Doc PR | todo This PR adds an option `--as-tree` to the command `translation:pull`, like the PR #38393 did it for the command `translation:extract`. This option would ease the use of the command `translation:pull` when the yaml translations are stored as a tree. Without it, the yaml translations are inlined after a `translation:pull`, regardless of whether it was stored as inline or as a tree. To bypass this, you would either have to - re-indent the yaml file yourself after a `translation:pull` - replace one service (e.g. `translation.dumper.yaml`) to forcefully add the option `as_tree` for the yaml file dumper Commits ------- 2f328ad [Translation] Add `--as-tree` option to `translation:pull` command
2 parents 18685c4 + 2f328ad commit f9327be

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

src/Symfony/Component/Translation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

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

910
6.3
1011
---

src/Symfony/Component/Translation/Command/TranslationPullCommand.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ protected function configure(): void
9595
new InputOption('domains', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Specify the domains to pull.'),
9696
new InputOption('locales', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Specify the locales to pull.'),
9797
new InputOption('format', null, InputOption::VALUE_OPTIONAL, 'Override the default output format.', 'xlf12'),
98+
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'),
9899
])
99100
->setHelp(<<<'EOF'
100101
The <info>%command.name%</> command pulls translations from the given provider. Only
@@ -126,6 +127,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
126127
$locales = $input->getOption('locales') ?: $this->enabledLocales;
127128
$domains = $input->getOption('domains');
128129
$format = $input->getOption('format');
130+
$asTree = (int) $input->getOption('as-tree');
129131
$xliffVersion = '1.2';
130132

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

147151
if (!$domains) {

src/Symfony/Component/Translation/Tests/Command/TranslationProviderTestCase.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ protected function getProviderCollection(ProviderInterface $provider, array $pro
5555
return new TranslationProviderCollection($collection);
5656
}
5757

58+
protected function createYamlFile(array $messages = ['node' => 'NOTE'], $targetLanguage = 'en', $fileNamePattern = 'messages.%locale%.yml'): string
59+
{
60+
$yamlContent = '';
61+
foreach ($messages as $key => $value) {
62+
$yamlContent .= "$key: $value\n";
63+
}
64+
$yamlContent .= "\n";
65+
66+
$filename = sprintf('%s/%s', $this->translationAppDir.'/translations', str_replace('%locale%', $targetLanguage, $fileNamePattern));
67+
file_put_contents($filename, $yamlContent);
68+
69+
$this->files[] = $filename;
70+
71+
return $filename;
72+
}
73+
5874
protected function createFile(array $messages = ['note' => 'NOTE'], $targetLanguage = 'en', $fileNamePattern = 'messages.%locale%.xlf', string $xlfVersion = 'xlf12'): string
5975
{
6076
if ('xlf12' === $xlfVersion) {

src/Symfony/Component/Translation/Tests/Command/TranslationPullCommandTest.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
use Symfony\Component\Console\Tester\CommandTester;
1717
use Symfony\Component\Translation\Command\TranslationPullCommand;
1818
use Symfony\Component\Translation\Dumper\XliffFileDumper;
19+
use Symfony\Component\Translation\Dumper\YamlFileDumper;
1920
use Symfony\Component\Translation\Loader\ArrayLoader;
2021
use Symfony\Component\Translation\Loader\XliffFileLoader;
22+
use Symfony\Component\Translation\Loader\YamlFileLoader;
2123
use Symfony\Component\Translation\Provider\ProviderInterface;
2224
use Symfony\Component\Translation\Reader\TranslationReader;
2325
use Symfony\Component\Translation\TranslatorBag;
@@ -235,6 +237,96 @@ public function testPullNewXlf20Messages()
235237
, file_get_contents($filenameFr));
236238
}
237239

240+
public function testPullNewYamlMessagesAsInlined()
241+
{
242+
$arrayLoader = new ArrayLoader();
243+
$filenameEn = $this->createYamlFile(['note' => 'NOTE'], 'en', 'messages.%locale%.yml');
244+
$filenameFr = $this->createYamlFile(['note' => 'NOTE'], 'fr', 'messages.%locale%.yml');
245+
$locales = ['en', 'fr'];
246+
$domains = ['messages'];
247+
248+
$providerReadTranslatorBag = new TranslatorBag();
249+
$providerReadTranslatorBag->addCatalogue($arrayLoader->load([
250+
'note' => 'NOTE',
251+
'new.foo' => 'newFoo',
252+
], 'en'));
253+
$providerReadTranslatorBag->addCatalogue($arrayLoader->load([
254+
'note' => 'NOTE',
255+
'new.foo' => 'nouveauFoo',
256+
], 'fr'));
257+
258+
$provider = $this->createMock(ProviderInterface::class);
259+
$provider->expects($this->once())
260+
->method('read')
261+
->with($domains, $locales)
262+
->willReturn($providerReadTranslatorBag);
263+
264+
$provider->expects($this->once())
265+
->method('__toString')
266+
->willReturn('null://default');
267+
268+
$tester = $this->createCommandTester($provider, $locales, $domains);
269+
$tester->execute(['--locales' => ['en', 'fr'], '--domains' => ['messages'], '--format' => 'yml']);
270+
271+
$this->assertStringContainsString('[OK] New translations from "null" has been written locally (for "en, fr" locale(s), and "messages" domain(s)).', trim($tester->getDisplay()));
272+
$this->assertEquals(<<<YAML
273+
new.foo: newFoo
274+
note: NOTE
275+
276+
YAML, file_get_contents($filenameEn));
277+
$this->assertEquals(<<<YAML
278+
new.foo: nouveauFoo
279+
note: NOTE
280+
281+
YAML, file_get_contents($filenameFr));
282+
}
283+
284+
public function testPullNewYamlMessagesAsTree()
285+
{
286+
$arrayLoader = new ArrayLoader();
287+
$filenameEn = $this->createYamlFile(['note' => 'NOTE'], 'en', 'messages.%locale%.yml');
288+
$filenameFr = $this->createYamlFile(['note' => 'NOTE'], 'fr', 'messages.%locale%.yml');
289+
$locales = ['en', 'fr'];
290+
$domains = ['messages'];
291+
292+
$providerReadTranslatorBag = new TranslatorBag();
293+
$providerReadTranslatorBag->addCatalogue($arrayLoader->load([
294+
'note' => 'NOTE',
295+
'new.foo' => 'newFoo',
296+
], 'en'));
297+
$providerReadTranslatorBag->addCatalogue($arrayLoader->load([
298+
'note' => 'NOTE',
299+
'new.foo' => 'nouveauFoo',
300+
], 'fr'));
301+
302+
$provider = $this->createMock(ProviderInterface::class);
303+
$provider->expects($this->once())
304+
->method('read')
305+
->with($domains, $locales)
306+
->willReturn($providerReadTranslatorBag);
307+
308+
$provider->expects($this->once())
309+
->method('__toString')
310+
->willReturn('null://default');
311+
312+
$tester = $this->createCommandTester($provider, $locales, $domains);
313+
$tester->execute(['--locales' => ['en', 'fr'], '--domains' => ['messages'], '--format' => 'yml', '--as-tree' => 10]);
314+
315+
$this->assertStringContainsString('[OK] New translations from "null" has been written locally (for "en, fr" locale(s), and "messages" domain(s)).', trim($tester->getDisplay()));
316+
$this->assertEquals(<<<YAML
317+
new:
318+
foo: newFoo
319+
note: NOTE
320+
321+
YAML, file_get_contents($filenameEn));
322+
$this->assertEquals(<<<YAML
323+
new:
324+
foo: nouveauFoo
325+
note: NOTE
326+
327+
YAML, file_get_contents($filenameFr));
328+
}
329+
238330
public function testPullForceMessages()
239331
{
240332
$arrayLoader = new ArrayLoader();
@@ -641,9 +733,11 @@ private function createCommand(ProviderInterface $provider, array $locales = ['e
641733
{
642734
$writer = new TranslationWriter();
643735
$writer->addDumper('xlf', new XliffFileDumper());
736+
$writer->addDumper('yml', new YamlFileDumper());
644737

645738
$reader = new TranslationReader();
646739
$reader->addLoader('xlf', new XliffFileLoader());
740+
$reader->addLoader('yml', new YamlFileLoader());
647741

648742
return new TranslationPullCommand(
649743
$this->getProviderCollection($provider, $providerNames, $locales, $domains),

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy