Skip to content

[Loco] Fix Loco Provider ID and pull & push local messages reading #43967

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
Nov 8, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ private function createAssets(array $keys): array
foreach ($keys as $key) {
$responses[$key] = $this->client->request('POST', 'assets', [
'body' => [
'id' => $key,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

be careful. Translation keys are only unique per domain, not globally. so they might note be suitable as loco ids.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, so may be we can concatenate the domain as prefix or suffix to the key?

'text' => $key,
'type' => 'text',
'default' => 'untranslated',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public function testCompleteWriteProcess()
$responses = [
'createAsset1' => function (string $method, string $url, array $options = []) use ($expectedAuthHeader): ResponseInterface {
$expectedBody = http_build_query([
'id' => 'a',
'text' => 'a',
'type' => 'text',
'default' => 'untranslated',
Expand Down Expand Up @@ -99,6 +100,7 @@ public function testCompleteWriteProcess()
},
'createAsset2' => function (string $method, string $url, array $options = []) use ($expectedAuthHeader): ResponseInterface {
$expectedBody = http_build_query([
'id' => 'post.num_comments',
'text' => 'post.num_comments',
'type' => 'text',
'default' => 'untranslated',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ private function readLocalTranslations(array $locales, array $domains, array $tr

if ($domains) {
foreach ($domains as $domain) {
$catalogue = $this->filterCatalogue($catalogue, $domain);
$bag->addCatalogue($catalogue);
$bag->addCatalogue($this->filterCatalogue($catalogue, $domain));
}
} else {
$bag->addCatalogue($catalogue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,20 +196,30 @@ public function testPullNewXlf20Messages()
public function testPullForceMessages()
{
$arrayLoader = new ArrayLoader();
$filenameEn = $this->createFile();
$filenameFr = $this->createFile(['note' => 'NOTE'], 'fr');
$filenameMessagesEn = $this->createFile(['note' => 'NOTE'], 'en');
$filenameMessagesFr = $this->createFile(['note' => 'NOTE'], 'fr');
$filenameValidatorsEn = $this->createFile(['foo.error' => 'Wrong value'], 'en', 'validators.%locale%.xlf');
$filenameValidatorsFr = $this->createFile(['foo.error' => 'Valeur erronée'], 'fr', 'validators.%locale%.xlf');
$locales = ['en', 'fr'];
$domains = ['messages'];
$domains = ['messages', 'validators'];

$providerReadTranslatorBag = new TranslatorBag();
$providerReadTranslatorBag->addCatalogue($arrayLoader->load([
'note' => 'UPDATED NOTE',
'new.foo' => 'newFoo',
], 'en'));
], 'en', 'messages'));
$providerReadTranslatorBag->addCatalogue($arrayLoader->load([
'note' => 'NOTE MISE À JOUR',
'new.foo' => 'nouveauFoo',
], 'fr'));
], 'fr', 'messages'));
$providerReadTranslatorBag->addCatalogue($arrayLoader->load([
'foo.error' => 'Bad value',
'bar.error' => 'Bar error',
], 'en', 'validators'));
$providerReadTranslatorBag->addCatalogue($arrayLoader->load([
'foo.error' => 'Valeur invalide',
'bar.error' => 'Bar erreur',
], 'fr', 'validators'));

$provider = $this->createMock(ProviderInterface::class);
$provider->expects($this->once())
Expand All @@ -222,9 +232,9 @@ public function testPullForceMessages()
->willReturn('null://default');

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

$this->assertStringContainsString('[OK] Local translations has been updated from "null" (for "en, fr" locale(s), and "messages" domain(s)).', trim($tester->getDisplay()));
$this->assertStringContainsString('[OK] Local translations has been updated from "null" (for "en, fr" locale(s), and "messages, validators" domain(s)).', trim($tester->getDisplay()));
$this->assertXmlStringEqualsXmlString(<<<XLIFF
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
Expand All @@ -245,7 +255,7 @@ public function testPullForceMessages()
</file>
</xliff>
XLIFF
, file_get_contents($filenameEn));
, file_get_contents($filenameMessagesEn));
$this->assertXmlStringEqualsXmlString(<<<XLIFF
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
Expand All @@ -266,7 +276,50 @@ public function testPullForceMessages()
</file>
</xliff>
XLIFF
, file_get_contents($filenameFr));
, file_get_contents($filenameMessagesFr));

$this->assertXmlStringEqualsXmlString(<<<XLIFF
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" target-language="en" datatype="plaintext" original="file.ext">
<header>
<tool tool-id="symfony" tool-name="Symfony"/>
</header>
<body>
<trans-unit id="kA4akVr" resname="foo.error">
<source>foo.error</source>
<target>Bad value</target>
</trans-unit>
<trans-unit id="OcBtn3X" resname="bar.error">
<source>bar.error</source>
<target>Bar error</target>
</trans-unit>
</body>
</file>
</xliff>
XLIFF
, file_get_contents($filenameValidatorsEn));
$this->assertXmlStringEqualsXmlString(<<<XLIFF
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file source-language="en" target-language="fr" datatype="plaintext" original="file.ext">
<header>
<tool tool-id="symfony" tool-name="Symfony"/>
</header>
<body>
<trans-unit id="kA4akVr" resname="foo.error">
<source>foo.error</source>
<target>Valeur invalide</target>
</trans-unit>
<trans-unit id="OcBtn3X" resname="bar.error">
<source>bar.error</source>
<target>Bar erreur</target>
</trans-unit>
</body>
</file>
</xliff>
XLIFF
, file_get_contents($filenameValidatorsFr));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,32 @@ public function testPushNewMessages()
public function testPushForceMessages()
{
$xliffLoader = new XliffFileLoader();
$filenameEn = $this->createFile([
$filenameMessagesEn = $this->createFile([
'note' => 'NOTE UPDATED',
'new.foo' => 'newFoo',
]);
$filenameFr = $this->createFile([
], 'en');
$filenameMessagesFr = $this->createFile([
'note' => 'NOTE MISE À JOUR',
'new.foo' => 'nouveauFoo',
], 'fr');
$filenameValidatorsEn = $this->createFile([
'foo.error' => 'Wrong value',
'bar.success' => 'Form valid!',
], 'en', 'validators.%locale%.xlf');
$filenameValidatorsFr = $this->createFile([
'foo.error' => 'Valeur erronée',
'bar.success' => 'Formulaire valide !',
], 'fr', 'validators.%locale%.xlf');
$locales = ['en', 'fr'];
$domains = ['messages'];
$domains = ['messages', 'validators'];

$provider = $this->createMock(ProviderInterface::class);

$localTranslatorBag = new TranslatorBag();
$localTranslatorBag->addCatalogue($xliffLoader->load($filenameEn, 'en'));
$localTranslatorBag->addCatalogue($xliffLoader->load($filenameFr, 'fr'));
$localTranslatorBag->addCatalogue($xliffLoader->load($filenameMessagesEn, 'en', 'messages'));
$localTranslatorBag->addCatalogue($xliffLoader->load($filenameMessagesFr, 'fr', 'messages'));
$localTranslatorBag->addCatalogue($xliffLoader->load($filenameValidatorsEn, 'en', 'validators'));
$localTranslatorBag->addCatalogue($xliffLoader->load($filenameValidatorsFr, 'fr', 'validators'));

$provider->expects($this->once())
->method('write')
Expand All @@ -117,9 +127,9 @@ public function testPushForceMessages()

$tester = $this->createCommandTester($provider, $locales, $domains);

$tester->execute(['--locales' => ['en', 'fr'], '--domains' => ['messages'], '--force' => true]);
$tester->execute(['--locales' => $locales, '--domains' => $domains, '--force' => true]);

$this->assertStringContainsString('[OK] All local translations has been sent to "null" (for "en, fr" locale(s), and "messages" domain(s)).', trim($tester->getDisplay()));
$this->assertStringContainsString('[OK] All local translations has been sent to "null" (for "en, fr" locale(s), and "messages, validators" domain(s)).', trim($tester->getDisplay()));
}

public function testDeleteMissingMessages()
Expand Down