Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Translation] translation:push command diff using intl-icu #47986

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

Closed
wants to merge 3 commits into from
Closed
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 @@ -137,7 +137,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// Reading local translations must be done after retrieving the domains from the provider
// in order to manage only translations from configured domains
$localTranslations = $this->readLocalTranslations($locales, $domains, $this->transPaths);
$localTranslations = $this->readLocalTranslations($locales, $domains, $this->transPaths, true);

if (!$domains) {
$domains = $this->getDomainsFromTranslatorBag($localTranslations);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
trait TranslationTrait
{
private function readLocalTranslations(array $locales, array $domains, array $transPaths): TranslatorBag
private function readLocalTranslations(array $locales, array $domains, array $transPaths, bool $mergeIntlIcu = false): TranslatorBag
{
$bag = new TranslatorBag();

Expand All @@ -32,7 +32,7 @@ private function readLocalTranslations(array $locales, array $domains, array $tr

if ($domains) {
foreach ($domains as $domain) {
$bag->addCatalogue($this->filterCatalogue($catalogue, $domain));
$bag->addCatalogue($this->filterCatalogue($catalogue, $domain, $mergeIntlIcu));
}
} else {
$bag->addCatalogue($catalogue);
Expand All @@ -42,14 +42,14 @@ private function readLocalTranslations(array $locales, array $domains, array $tr
return $bag;
}

private function filterCatalogue(MessageCatalogue $catalogue, string $domain): MessageCatalogue
private function filterCatalogue(MessageCatalogue $catalogue, string $domain, bool $mergeIntlIcu): MessageCatalogue
{
$filteredCatalogue = new MessageCatalogue($catalogue->getLocale());

// extract intl-icu messages only
$intlDomain = $domain.MessageCatalogueInterface::INTL_DOMAIN_SUFFIX;
if ($intlMessages = $catalogue->all($intlDomain)) {
$filteredCatalogue->add($intlMessages, $intlDomain);
$filteredCatalogue->add($intlMessages, $mergeIntlIcu ? $domain : $intlDomain);
}

// extract all messages and subtract intl-icu messages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,52 @@ public function testPushNewMessages()
$this->assertStringContainsString('[OK] New local translations has been sent to "null" (for "en, fr" locale(s), and "messages" domain(s)).', trim($tester->getDisplay()));
}

public function testPushNewIntlIcuMessages()
{
$arrayLoader = new ArrayLoader();
$xliffLoader = new XliffFileLoader();
$locales = ['en', 'fr'];
$domains = ['messages'];

// Simulate existing messages on Provider
$providerReadTranslatorBag = new TranslatorBag();
$providerReadTranslatorBag->addCatalogue($arrayLoader->load(['note' => 'NOTE'], 'en'));
$providerReadTranslatorBag->addCatalogue($arrayLoader->load(['note' => 'NOTE'], 'fr'));

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

// Create local files, with a new message
$filenameEn = $this->createFile([
'note' => 'NOTE',
'new.foo' => 'newFooIntlIcu',
], 'en', 'messages+intl-icu.%locale%.xlf');
$filenameFr = $this->createFile([
'note' => 'NOTE',
'new.foo' => 'nouveauFooIntlIcu',
], 'fr', 'messages+intl-icu.%locale%.xlf');
$localTranslatorBag = new TranslatorBag();
$localTranslatorBag->addCatalogue($xliffLoader->load($filenameEn, 'en'));
$localTranslatorBag->addCatalogue($xliffLoader->load($filenameFr, 'fr'));

$provider->expects($this->once())
->method('write')
->with($localTranslatorBag->diff($providerReadTranslatorBag));

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

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

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

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

public function testPushForceMessages()
{
$xliffLoader = new XliffFileLoader();
Expand Down