Skip to content

Commit b6adb52

Browse files
bug symfony#52677 [Translation] [Lokalise] Fix language format on Lokalise Provider (welcoMattic)
This PR was merged into the 5.4 branch. Discussion ---------- [Translation] [Lokalise] Fix language format on Lokalise Provider | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT Lokalise has changed their API a little bit, and ISO languages are formatted with a `-`, which is not wrong. But we use it as locale to load the translation with XliffLoader (and other loaders), and it leads to wrong filename like `messages+intl-icu.en-US.xlf`. This PR fixes the issue. Commits ------- 7a8897c Fix language format on Lokalise Provider
2 parents 5f942a9 + 7a8897c commit b6adb52

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

src/Symfony/Component/Translation/Bridge/Lokalise/LokaliseProvider.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ private function exportFiles(array $locales, array $domains): array
147147
'json' => [
148148
'format' => 'symfony_xliff',
149149
'original_filenames' => true,
150-
'directory_prefix' => '%LANG_ISO%',
151150
'filter_langs' => array_values($locales),
152151
'filter_filenames' => array_map([$this, 'getLokaliseFilenameFromDomain'], $domains),
153152
'export_empty_as' => 'skip',
@@ -167,7 +166,12 @@ private function exportFiles(array $locales, array $domains): array
167166
throw new ProviderException(sprintf('Unable to export translations from Lokalise: "%s".', $response->getContent(false)), $response);
168167
}
169168

170-
return $responseContent['files'];
169+
// Lokalise returns languages with "-" separator, we need to reformat them to "_" separator.
170+
$reformattedLanguages = array_map(function ($language) {
171+
return str_replace('-', '_', $language);
172+
}, array_keys($responseContent['files']));
173+
174+
return array_combine($reformattedLanguages, $responseContent['files']);
171175
}
172176

173177
private function createKeys(array $keys, string $domain): array

src/Symfony/Component/Translation/Bridge/Lokalise/Tests/LokaliseProviderTest.php

+31-7
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,6 @@ public function testReadForOneLocaleAndOneDomain(string $locale, string $domain,
559559
$expectedBody = json_encode([
560560
'format' => 'symfony_xliff',
561561
'original_filenames' => true,
562-
'directory_prefix' => '%LANG_ISO%',
563562
'filter_langs' => [$locale],
564563
'filter_filenames' => [$domain.'.xliff'],
565564
'export_empty_as' => 'skip',
@@ -581,15 +580,10 @@ public function testReadForOneLocaleAndOneDomain(string $locale, string $domain,
581580
]));
582581
};
583582

584-
$loader = $this->getLoader();
585-
$loader->expects($this->once())
586-
->method('load')
587-
->willReturn((new XliffFileLoader())->load($responseContent, $locale, $domain));
588-
589583
$provider = self::createProvider((new MockHttpClient($response))->withOptions([
590584
'base_uri' => 'https://api.lokalise.com/api2/projects/PROJECT_ID/',
591585
'headers' => ['X-Api-Token' => 'API_KEY'],
592-
]), $loader, $this->getLogger(), $this->getDefaultLocale(), 'api.lokalise.com');
586+
]), new XliffFileLoader(), $this->getLogger(), $this->getDefaultLocale(), 'api.lokalise.com');
593587
$translatorBag = $provider->read([$domain], [$locale]);
594588

595589
// We don't want to assert equality of metadata here, due to the ArrayLoader usage.
@@ -761,6 +755,36 @@ public static function getResponsesForOneLocaleAndOneDomain(): \Generator
761755
$expectedTranslatorBagEn,
762756
];
763757

758+
$expectedTranslatorBagEnUS = new TranslatorBag();
759+
$expectedTranslatorBagEnUS->addCatalogue($arrayLoader->load([
760+
'index.hello' => 'Hello',
761+
'index.greetings' => 'Welcome, {firstname}!',
762+
], 'en_US'));
763+
764+
yield ['en_US', 'messages', <<<'XLIFF'
765+
<?xml version="1.0" encoding="UTF-8"?>
766+
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 http://docs.oasis-open.org/xliff/v1.2/os/xliff-core-1.2-strict.xsd">
767+
<file original="" datatype="plaintext" xml:space="preserve" source-language="en" target-language="en-US">
768+
<header>
769+
<tool tool-id="lokalise.com" tool-name="Lokalise"/>
770+
</header>
771+
<body>
772+
<trans-unit id="index.greetings" resname="index.greetings">
773+
<source>index.greetings</source>
774+
<target>Welcome, {firstname}!</target>
775+
</trans-unit>
776+
<trans-unit id="index.hello" resname="index.hello">
777+
<source>index.hello</source>
778+
<target>Hello</target>
779+
</trans-unit>
780+
</body>
781+
</file>
782+
</xliff>
783+
XLIFF
784+
,
785+
$expectedTranslatorBagEnUS,
786+
];
787+
764788
$expectedTranslatorBagFr = new TranslatorBag();
765789
$expectedTranslatorBagFr->addCatalogue($arrayLoader->load([
766790
'index.hello' => 'Bonjour',

0 commit comments

Comments
 (0)