Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Translation] Add--as-tree option totranslation: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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
nicolas-grekas merged 1 commit intosymfony:6.4fromsyffer:translation-pull/add-as-tree-argument
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionssrc/Symfony/Component/Translation/CHANGELOG.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp