Skip to content

Commit 99223bd

Browse files
thewilkybarkidfabpot
authored andcommitted
[Translator] Use ICU parent locales as fallback locales
1 parent 2021b24 commit 99223bd

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

Data/Generator/LocaleDataGenerator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function generateData(GeneratorConfig $config)
5757

5858
$locales = $localeScanner->scanLocales($config->getSourceDir().'/locales');
5959
$aliases = $localeScanner->scanAliases($config->getSourceDir().'/locales');
60+
$parents = $localeScanner->scanParents($config->getSourceDir().'/locales');
6061

6162
// Flip to facilitate lookup
6263
$flippedLocales = array_flip($locales);
@@ -134,6 +135,12 @@ public function generateData(GeneratorConfig $config)
134135
'Aliases' => $aliases,
135136
));
136137
}
138+
139+
// Write parents locale file for the Translation component
140+
\file_put_contents(
141+
__DIR__.'/../../../Translation/Resources/data/parents.json',
142+
\json_encode($parents, \JSON_PRETTY_PRINT).\PHP_EOL
143+
);
137144
}
138145

139146
private function generateLocaleName($locale, $displayLocale)

Data/Util/LocaleScanner.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,24 @@ public function scanAliases($sourceDir)
8282

8383
return $aliases;
8484
}
85+
86+
/**
87+
* Returns all locale parents found in the given directory.
88+
*/
89+
public function scanParents(string $sourceDir): array
90+
{
91+
$locales = $this->scanLocales($sourceDir);
92+
$fallbacks = array();
93+
94+
foreach ($locales as $locale) {
95+
$content = \file_get_contents($sourceDir.'/'.$locale.'.txt');
96+
97+
// Aliases contain the text "%%PARENT" followed by the aliased locale
98+
if (\preg_match('/%%Parent{"([^"]+)"}/', $content, $matches)) {
99+
$fallbacks[$locale] = $matches[1];
100+
}
101+
}
102+
103+
return $fallbacks;
104+
}
85105
}

Tests/Data/Util/LocaleScannerTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,13 @@ protected function setUp()
4242

4343
$this->filesystem->touch($this->directory.'/en.txt');
4444
$this->filesystem->touch($this->directory.'/en_alias.txt');
45+
$this->filesystem->touch($this->directory.'/en_child.txt');
4546
$this->filesystem->touch($this->directory.'/de.txt');
4647
$this->filesystem->touch($this->directory.'/de_alias.txt');
48+
$this->filesystem->touch($this->directory.'/de_child.txt');
4749
$this->filesystem->touch($this->directory.'/fr.txt');
4850
$this->filesystem->touch($this->directory.'/fr_alias.txt');
51+
$this->filesystem->touch($this->directory.'/fr_child.txt');
4952
$this->filesystem->touch($this->directory.'/root.txt');
5053
$this->filesystem->touch($this->directory.'/supplementalData.txt');
5154
$this->filesystem->touch($this->directory.'/supplementaldata.txt');
@@ -54,6 +57,9 @@ protected function setUp()
5457
file_put_contents($this->directory.'/en_alias.txt', 'en_alias{"%%ALIAS"{"en"}}');
5558
file_put_contents($this->directory.'/de_alias.txt', 'de_alias{"%%ALIAS"{"de"}}');
5659
file_put_contents($this->directory.'/fr_alias.txt', 'fr_alias{"%%ALIAS"{"fr"}}');
60+
file_put_contents($this->directory.'/en_child.txt', 'en_GB{%%Parent{"en"}}');
61+
file_put_contents($this->directory.'/de_child.txt', 'en_GB{%%Parent{"de"}}');
62+
file_put_contents($this->directory.'/fr_child.txt', 'en_GB{%%Parent{"fr"}}');
5763
}
5864

5965
protected function tearDown()
@@ -63,7 +69,7 @@ protected function tearDown()
6369

6470
public function testScanLocales()
6571
{
66-
$sortedLocales = array('de', 'de_alias', 'en', 'en_alias', 'fr', 'fr_alias');
72+
$sortedLocales = array('de', 'de_alias', 'de_child', 'en', 'en_alias', 'en_child', 'fr', 'fr_alias', 'fr_child');
6773

6874
$this->assertSame($sortedLocales, $this->scanner->scanLocales($this->directory));
6975
}
@@ -74,4 +80,11 @@ public function testScanAliases()
7480

7581
$this->assertSame($sortedAliases, $this->scanner->scanAliases($this->directory));
7682
}
83+
84+
public function testScanParents()
85+
{
86+
$sortedParents = array('de_child' => 'de', 'en_child' => 'en', 'fr_child' => 'fr');
87+
88+
$this->assertSame($sortedParents, $this->scanner->scanParents($this->directory));
89+
}
7790
}

0 commit comments

Comments
 (0)