Skip to content

[Translation] Crowdin Bridge: Fix locale vs LanguageId #50040

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
Apr 20, 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
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public function read(array $domains, array $locales): TranslatorBag
$translatorBag = new TranslatorBag();
$responses = [];

$localeLanguageMap = $this->mapLocalesToLanguageId($locales);

foreach ($domains as $domain) {
$fileId = $this->getFileIdByDomain($fileList, $domain);

Expand All @@ -118,7 +120,7 @@ public function read(array $domains, array $locales): TranslatorBag

foreach ($locales as $locale) {
if ($locale !== $this->defaultLocale) {
$response = $this->exportProjectTranslations($locale, $fileId);
$response = $this->exportProjectTranslations($localeLanguageMap[$locale], $fileId);
Copy link
Contributor

Choose a reason for hiding this comment

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

The same issue probably is also relevant for the uploadTranslations method

Copy link
Author

Choose a reason for hiding this comment

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

Actually from my observation the mapping is done on Crowdin side and works (i.e. de-DE source gets properly mapped to de)

} else {
$response = $this->downloadSourceFile($fileId);
}
Expand Down Expand Up @@ -403,4 +405,38 @@ private function getFileList(): array

return $result;
}

private function mapLocalesToLanguageId(array $locales): array
{
/**
* We cannot query by locales, we need to fetch all and filter out the relevant ones.
*
* @see https://developer.crowdin.com/api/v2/#operation/api.languages.getMany (Crowdin API)
* @see https://developer.crowdin.com/enterprise/api/v2/#operation/api.languages.getMany (Crowdin Enterprise API)
*/
$response = $this->client->request('GET', '../../languages?limit=500');

if (200 !== $response->getStatusCode()) {
throw new ProviderException('Unable to list set languages.', $response);
}

$localeLanguageMap = [];
foreach ($response->toArray()['data'] as $language) {
foreach (['locale', 'osxLocale', 'id'] as $key) {
if (\in_array($language['data'][$key], $locales)) {
$localeLanguageMap[$language['data'][$key]] = $language['data']['id'];
}
}
}

if (\count($localeLanguageMap) !== \count($locales)) {
$message = implode('", "', array_diff($locales, array_keys($localeLanguageMap)));
$message = sprintf('Unable to find all requested locales: "%s" not found.', $message);
$this->logger->error($message);

throw new ProviderException($message, $response);
}

return $localeLanguageMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,30 @@ public function testReadForOneLocaleAndOneDomain(string $locale, string $domain,
],
]));
},
'listLanguages' => function (string $method, string $url, array $options = []): ResponseInterface {
$this->assertSame('GET', $method);
$this->assertSame('https://api.crowdin.com/api/v2/languages?limit=500', $url);
$this->assertSame('Authorization: Bearer API_TOKEN', $options['normalized_headers']['authorization'][0]);

return new MockResponse(json_encode([
'data' => [
[
'data' => [
'id' => 'en-GB',
'osxLocale' => 'en_GB',
'locale' => 'en-GB',
],
],
[
'data' => [
'id' => 'fr',
'osxLocale' => 'fr_FR',
'locale' => 'fr-FR',
],
],
],
]));
},
'exportProjectTranslations' => function (string $method, string $url, array $options = []) use ($expectedTargetLanguageId): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://api.crowdin.com/api/v2/projects/1/translations/exports', $url);
Expand Down Expand Up @@ -746,12 +770,37 @@ public function testReadForDefaultLocaleAndOneDomain(string $locale, string $dom
],
]));
},
'listLanguages' => function (string $method, string $url, array $options = []): ResponseInterface {
$this->assertSame('GET', $method);
$this->assertSame('https://api.crowdin.com/api/v2/languages?limit=500', $url);
$this->assertSame('Authorization: Bearer API_TOKEN', $options['normalized_headers']['authorization'][0]);

return new MockResponse(json_encode([
'data' => [
[
'data' => [
'id' => 'en',
'osxLocale' => 'en_GB',
'locale' => 'en-GB',
],
],
[
'data' => [
'id' => 'fr',
'osxLocale' => 'fr_FR',
'locale' => 'fr-FR',
],
],
],
]));
},
'downloadSource' => function (string $method, string $url): ResponseInterface {
$this->assertSame('GET', $method);
$this->assertSame('https://api.crowdin.com/api/v2/projects/1/files/12/download', $url);

return new MockResponse(json_encode(['data' => ['url' => 'https://file.url']]));
},

'downloadFile' => function (string $method, string $url) use ($responseContent): ResponseInterface {
$this->assertSame('GET', $method);
$this->assertSame('https://file.url/', $url);
Expand Down Expand Up @@ -826,6 +875,30 @@ public function testReadServerException()
],
]));
},
'listLanguages' => function (string $method, string $url, array $options = []): ResponseInterface {
$this->assertSame('GET', $method);
$this->assertSame('https://api.crowdin.com/api/v2/languages?limit=500', $url);
$this->assertSame('Authorization: Bearer API_TOKEN', $options['normalized_headers']['authorization'][0]);

return new MockResponse(json_encode([
'data' => [
[
'data' => [
'id' => 'en',
'osxLocale' => 'en_GB',
'locale' => 'en-GB',
],
],
[
'data' => [
'id' => 'fr',
'osxLocale' => 'fr_FR',
'locale' => 'fr-FR',
],
],
],
]));
},
'exportProjectTranslations' => function (string $method, string $url, array $options = []): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://api.crowdin.com/api/v2/projects/1/translations/exports', $url);
Expand Down Expand Up @@ -861,6 +934,30 @@ public function testReadDownloadServerException()
],
]));
},
'listLanguages' => function (string $method, string $url, array $options = []): ResponseInterface {
$this->assertSame('GET', $method);
$this->assertSame('https://api.crowdin.com/api/v2/languages?limit=500', $url);
$this->assertSame('Authorization: Bearer API_TOKEN', $options['normalized_headers']['authorization'][0]);

return new MockResponse(json_encode([
'data' => [
[
'data' => [
'id' => 'en',
'osxLocale' => 'en_GB',
'locale' => 'en-GB',
],
],
[
'data' => [
'id' => 'fr',
'osxLocale' => 'fr_FR',
'locale' => 'fr-FR',
],
],
],
]));
},
'exportProjectTranslations' => function (string $method, string $url, array $options = []): ResponseInterface {
$this->assertSame('POST', $method);
$this->assertSame('https://api.crowdin.com/api/v2/projects/1/translations/exports', $url);
Expand Down