Skip to content

[Translation] [Lokalise] Fix language format on Lokalise Provider #52677

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 24, 2023
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
Fix language format on Lokalise Provider
  • Loading branch information
welcoMattic committed Nov 24, 2023
commit 7a8897c766fb54e48d2cf8dca7a3c6eca67ad97c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ private function exportFiles(array $locales, array $domains): array
'json' => [
'format' => 'symfony_xliff',
'original_filenames' => true,
'directory_prefix' => '%LANG_ISO%',
'filter_langs' => array_values($locales),
'filter_filenames' => array_map([$this, 'getLokaliseFilenameFromDomain'], $domains),
'export_empty_as' => 'skip',
Expand All @@ -167,7 +166,12 @@ private function exportFiles(array $locales, array $domains): array
throw new ProviderException(sprintf('Unable to export translations from Lokalise: "%s".', $response->getContent(false)), $response);
}

return $responseContent['files'];
// Lokalise returns languages with "-" separator, we need to reformat them to "_" separator.
$reformattedLanguages = array_map(function ($language) {
return str_replace('-', '_', $language);
}, array_keys($responseContent['files']));

return array_combine($reformattedLanguages, $responseContent['files']);
}

private function createKeys(array $keys, string $domain): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,6 @@ public function testReadForOneLocaleAndOneDomain(string $locale, string $domain,
$expectedBody = json_encode([
'format' => 'symfony_xliff',
'original_filenames' => true,
'directory_prefix' => '%LANG_ISO%',
'filter_langs' => [$locale],
'filter_filenames' => [$domain.'.xliff'],
'export_empty_as' => 'skip',
Expand All @@ -581,15 +580,10 @@ public function testReadForOneLocaleAndOneDomain(string $locale, string $domain,
]));
};

$loader = $this->getLoader();
$loader->expects($this->once())
->method('load')
->willReturn((new XliffFileLoader())->load($responseContent, $locale, $domain));

$provider = self::createProvider((new MockHttpClient($response))->withOptions([
'base_uri' => 'https://api.lokalise.com/api2/projects/PROJECT_ID/',
'headers' => ['X-Api-Token' => 'API_KEY'],
]), $loader, $this->getLogger(), $this->getDefaultLocale(), 'api.lokalise.com');
]), new XliffFileLoader(), $this->getLogger(), $this->getDefaultLocale(), 'api.lokalise.com');
$translatorBag = $provider->read([$domain], [$locale]);

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

$expectedTranslatorBagEnUS = new TranslatorBag();
$expectedTranslatorBagEnUS->addCatalogue($arrayLoader->load([
'index.hello' => 'Hello',
'index.greetings' => 'Welcome, {firstname}!',
], 'en_US'));

yield ['en_US', 'messages', <<<'XLIFF'
<?xml version="1.0" encoding="UTF-8"?>
<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">
<file original="" datatype="plaintext" xml:space="preserve" source-language="en" target-language="en-US">
<header>
<tool tool-id="lokalise.com" tool-name="Lokalise"/>
</header>
<body>
<trans-unit id="index.greetings" resname="index.greetings">
<source>index.greetings</source>
<target>Welcome, {firstname}!</target>
</trans-unit>
<trans-unit id="index.hello" resname="index.hello">
<source>index.hello</source>
<target>Hello</target>
</trans-unit>
</body>
</file>
</xliff>
XLIFF
,
$expectedTranslatorBagEnUS,
];

$expectedTranslatorBagFr = new TranslatorBag();
$expectedTranslatorBagFr->addCatalogue($arrayLoader->load([
'index.hello' => 'Bonjour',
Expand Down