Skip to content

Commit 75d1dd4

Browse files
committed
bug symfony#31411 [Intl] Fix root fallback locale (ro0NL)
This PR was merged into the 3.4 branch. Discussion ---------- [Intl] Fix root fallback locale | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no | Tests pass? | yes (including intl-data group) | Fixed tickets | #... <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> We should never return "root" as a fallback locale for the "root" locale itself. While at it, i realized the alias meta files are pointless :) 4.2) https://github.com/ro0NL/symfony/commit/b9fc8b785773cc38cef86dd0136046234fd77331 4.3) https://github.com/ro0NL/symfony/commit/922a1eb Commits ------- 11ff24a [Intl] Fix root fallback locale
2 parents 6c6f76f + 11ff24a commit 75d1dd4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+68
-199
lines changed

src/Symfony/Component/Intl/Data/Generator/LocaleDataGenerator.php

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
use Symfony\Component\Filesystem\Filesystem;
1515
use Symfony\Component\Intl\Data\Bundle\Compiler\BundleCompilerInterface;
1616
use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReaderInterface;
17-
use Symfony\Component\Intl\Data\Provider\LanguageDataProvider;
18-
use Symfony\Component\Intl\Data\Provider\RegionDataProvider;
19-
use Symfony\Component\Intl\Data\Provider\ScriptDataProvider;
2017
use Symfony\Component\Intl\Data\Util\LocaleScanner;
2118
use Symfony\Component\Intl\Exception\MissingResourceException;
2219
use Symfony\Component\Intl\Locale;
@@ -31,23 +28,11 @@
3128
*/
3229
class LocaleDataGenerator extends AbstractDataGenerator
3330
{
34-
private $languageDataProvider;
35-
private $scriptDataProvider;
36-
private $regionDataProvider;
3731
private $locales;
3832
private $localeAliases;
3933
private $fallbackMapping;
4034
private $fallbackCache = [];
4135

42-
public function __construct(BundleCompilerInterface $compiler, $dirName, LanguageDataProvider $languageDataProvider, ScriptDataProvider $scriptDataProvider, RegionDataProvider $regionDataProvider)
43-
{
44-
parent::__construct($compiler, $dirName);
45-
46-
$this->languageDataProvider = $languageDataProvider;
47-
$this->scriptDataProvider = $scriptDataProvider;
48-
$this->regionDataProvider = $regionDataProvider;
49-
}
50-
5136
/**
5237
* {@inheritdoc}
5338
*/
@@ -66,8 +51,12 @@ protected function scanLocales(LocaleScanner $scanner, $sourceDir)
6651
protected function compileTemporaryBundles(BundleCompilerInterface $compiler, $sourceDir, $tempDir)
6752
{
6853
$filesystem = new Filesystem();
69-
$filesystem->mkdir($tempDir.'/lang');
54+
$filesystem->mkdir([
55+
$tempDir.'/lang',
56+
$tempDir.'/region',
57+
]);
7058
$compiler->compile($sourceDir.'/lang', $tempDir.'/lang');
59+
$compiler->compile($sourceDir.'/region', $tempDir.'/region');
7160
}
7261

7362
/**
@@ -83,19 +72,14 @@ protected function preGenerate()
8372
*/
8473
protected function generateDataForLocale(BundleEntryReaderInterface $reader, $tempDir, $displayLocale)
8574
{
86-
// Generate aliases, needed to enable proper fallback from alias to its
87-
// target
75+
// Don't generate aliases, as they are resolved during runtime
8876
if (isset($this->localeAliases[$displayLocale])) {
89-
return ['%%ALIAS' => $this->localeAliases[$displayLocale]];
77+
return;
9078
}
9179

9280
// Generate locale names for all locales that have translations in
9381
// at least the language or the region bundle
94-
try {
95-
$displayFormat = $reader->readEntry($tempDir.'/lang', $displayLocale, ['localeDisplayPattern']);
96-
} catch (MissingResourceException $e) {
97-
$displayFormat = $reader->readEntry($tempDir.'/lang', 'root', ['localeDisplayPattern']);
98-
}
82+
$displayFormat = $reader->readEntry($tempDir.'/lang', $displayLocale, ['localeDisplayPattern']);
9983
$pattern = $displayFormat['pattern'] ?? '{0} ({1})';
10084
$separator = $displayFormat['separator'] ?? '{0}, {1}';
10185
$localeNames = [];
@@ -110,7 +94,7 @@ protected function generateDataForLocale(BundleEntryReaderInterface $reader, $te
11094
// Each locale name has the form: "Language (Script, Region, Variant1, ...)
11195
// Script, Region and Variants are optional. If none of them is
11296
// available, the braces are not printed.
113-
$localeNames[$locale] = $this->generateLocaleName($locale, $displayLocale, $pattern, $separator);
97+
$localeNames[$locale] = $this->generateLocaleName($reader, $tempDir, $locale, $displayLocale, $pattern, $separator);
11498
} catch (MissingResourceException $e) {
11599
// Silently ignore incomplete locale names
116100
// In this case one should configure at least one fallback locale that is complete (e.g. English) during
@@ -158,22 +142,26 @@ protected function generateDataForMeta(BundleEntryReaderInterface $reader, $temp
158142
/**
159143
* @return string
160144
*/
161-
private function generateLocaleName($locale, $displayLocale, $pattern, $separator)
145+
private function generateLocaleName(BundleEntryReaderInterface $reader, $tempDir, $locale, $displayLocale, $pattern, $separator)
162146
{
163147
// Apply generic notation using square brackets as described per http://cldr.unicode.org/translation/language-names
164-
$name = str_replace(['(', ')'], ['[', ']'], $this->languageDataProvider->getName(\Locale::getPrimaryLanguage($locale), $displayLocale));
148+
$name = str_replace(['(', ')'], ['[', ']'], $reader->readEntry($tempDir.'/lang', $displayLocale, ['Languages', \Locale::getPrimaryLanguage($locale)]));
165149
$extras = [];
166150

167151
// Discover the name of the script part of the locale
168152
// i.e. in zh_Hans_MO, "Hans" is the script
169153
if ($script = \Locale::getScript($locale)) {
170-
$extras[] = str_replace(['(', ')'], ['[', ']'], $this->scriptDataProvider->getName($script, $displayLocale));
154+
$extras[] = str_replace(['(', ')'], ['[', ']'], $reader->readEntry($tempDir.'/lang', $displayLocale, ['Scripts', $script]));
171155
}
172156

173157
// Discover the name of the region part of the locale
174158
// i.e. in de_AT, "AT" is the region
175159
if ($region = \Locale::getRegion($locale)) {
176-
$extras[] = str_replace(['(', ')'], ['[', ']'], $this->regionDataProvider->getName($region, $displayLocale));
160+
if (!RegionDataGenerator::isValidCountryCode($region)) {
161+
throw new MissingResourceException('Skipping "'.$locale.'" due an invalid country.');
162+
}
163+
164+
$extras[] = str_replace(['(', ')'], ['[', ']'], $reader->readEntry($tempDir.'/region', $displayLocale, ['Countries', $region]));
177165
}
178166

179167
if ($extras) {

src/Symfony/Component/Intl/Data/Generator/RegionDataGenerator.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ class RegionDataGenerator extends AbstractDataGenerator
4848
*/
4949
private $regionCodes = [];
5050

51+
public static function isValidCountryCode($region)
52+
{
53+
if (isset(self::$blacklist[$region])) {
54+
return false;
55+
}
56+
57+
// WORLD/CONTINENT/SUBCONTINENT/GROUPING
58+
if (ctype_digit($region) || \is_int($region)) {
59+
return false;
60+
}
61+
62+
return true;
63+
}
64+
5165
/**
5266
* {@inheritdoc}
5367
*/
@@ -125,12 +139,7 @@ protected function generateRegionNames(ArrayAccessibleResourceBundle $localeBund
125139
$regionNames = [];
126140

127141
foreach ($unfilteredRegionNames as $region => $regionName) {
128-
if (isset(self::$blacklist[$region])) {
129-
continue;
130-
}
131-
132-
// WORLD/CONTINENT/SUBCONTINENT/GROUPING
133-
if (ctype_digit($region) || \is_int($region)) {
142+
if (!self::isValidCountryCode($region)) {
134143
continue;
135144
}
136145

src/Symfony/Component/Intl/Locale.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class Locale extends \Locale
3131
* The default fallback locale is used as fallback for locales that have no
3232
* fallback otherwise.
3333
*
34-
* @param string $locale The default fallback locale
34+
* @param string|null $locale The default fallback locale
3535
*
3636
* @see getFallback()
3737
*/
@@ -43,7 +43,7 @@ public static function setDefaultFallback($locale)
4343
/**
4444
* Returns the default fallback locale.
4545
*
46-
* @return string The default fallback locale
46+
* @return string|null The default fallback locale
4747
*
4848
* @see setDefaultFallback()
4949
* @see getFallback()
@@ -70,7 +70,7 @@ public static function getFallback($locale)
7070
if (\function_exists('locale_parse')) {
7171
$localeSubTags = locale_parse($locale);
7272
if (1 === \count($localeSubTags)) {
73-
if (self::$defaultFallback === $localeSubTags['language']) {
73+
if ('root' !== self::$defaultFallback && self::$defaultFallback === $localeSubTags['language']) {
7474
return 'root';
7575
}
7676

@@ -98,7 +98,7 @@ public static function getFallback($locale)
9898
return substr($locale, 0, $pos);
9999
}
100100

101-
if (self::$defaultFallback === $locale) {
101+
if ('root' !== self::$defaultFallback && self::$defaultFallback === $locale) {
102102
return 'root';
103103
}
104104

src/Symfony/Component/Intl/Resources/bin/update-data.php

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,13 @@
1111

1212
use Symfony\Component\Filesystem\Filesystem;
1313
use Symfony\Component\Intl\Data\Bundle\Compiler\GenrbCompiler;
14-
use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReader;
15-
use Symfony\Component\Intl\Data\Bundle\Reader\JsonBundleReader;
1614
use Symfony\Component\Intl\Data\Bundle\Writer\JsonBundleWriter;
1715
use Symfony\Component\Intl\Data\Generator\CurrencyDataGenerator;
1816
use Symfony\Component\Intl\Data\Generator\GeneratorConfig;
1917
use Symfony\Component\Intl\Data\Generator\LanguageDataGenerator;
2018
use Symfony\Component\Intl\Data\Generator\LocaleDataGenerator;
2119
use Symfony\Component\Intl\Data\Generator\RegionDataGenerator;
2220
use Symfony\Component\Intl\Data\Generator\ScriptDataGenerator;
23-
use Symfony\Component\Intl\Data\Provider\LanguageDataProvider;
24-
use Symfony\Component\Intl\Data\Provider\RegionDataProvider;
25-
use Symfony\Component\Intl\Data\Provider\ScriptDataProvider;
2621
use Symfony\Component\Intl\Intl;
2722
use Symfony\Component\Intl\Locale;
2823
use Symfony\Component\Intl\Util\GitRepository;
@@ -171,27 +166,13 @@
171166
$compiler = new GenrbCompiler($genrb, $genrbEnv);
172167
$config = new GeneratorConfig($sourceDir.'/data', $icuVersionInDownload);
173168
$jsonDir = dirname(__DIR__).'/data';
174-
$targetDirs = [$jsonDir];
175-
$workingDirs = [$jsonDir];
176169

177170
$config->addBundleWriter($jsonDir, new JsonBundleWriter());
178171

179172
echo "Starting resource bundle compilation. This may take a while...\n";
180173

181-
$filesystem->remove($workingDirs);
182-
183-
foreach ($workingDirs as $targetDir) {
184-
$filesystem->mkdir([
185-
$targetDir.'/'.Intl::CURRENCY_DIR,
186-
$targetDir.'/'.Intl::LANGUAGE_DIR,
187-
$targetDir.'/'.Intl::LOCALE_DIR,
188-
$targetDir.'/'.Intl::REGION_DIR,
189-
$targetDir.'/'.Intl::SCRIPT_DIR,
190-
]);
191-
}
192-
193174
// We don't want to use fallback to English during generation
194-
Locale::setDefaultFallback(null);
175+
Locale::setDefaultFallback('root');
195176

196177
echo "Generating language data...\n";
197178

@@ -215,14 +196,7 @@
215196

216197
echo "Generating locale data...\n";
217198

218-
$reader = new BundleEntryReader(new JsonBundleReader());
219-
$generator = new LocaleDataGenerator(
220-
$compiler,
221-
Intl::LOCALE_DIR,
222-
new LanguageDataProvider($jsonDir.'/'.Intl::LANGUAGE_DIR, $reader),
223-
new ScriptDataProvider($jsonDir.'/'.Intl::SCRIPT_DIR, $reader),
224-
new RegionDataProvider($jsonDir.'/'.Intl::REGION_DIR, $reader)
225-
);
199+
$generator = new LocaleDataGenerator($compiler, Intl::LOCALE_DIR);
226200
$generator->generateData($config);
227201

228202
echo "Resource bundle compilation complete.\n";
@@ -238,18 +212,15 @@
238212
239213
GIT_INFO;
240214

241-
foreach ($targetDirs as $targetDir) {
242-
$gitInfoFile = $targetDir.'/git-info.txt';
215+
$gitInfoFile = $jsonDir.'/git-info.txt';
243216

244-
file_put_contents($gitInfoFile, $gitInfo);
217+
file_put_contents($gitInfoFile, $gitInfo);
245218

246-
echo "Wrote $gitInfoFile.\n";
219+
echo "Wrote $gitInfoFile.\n";
247220

248-
$versionFile = $targetDir.'/version.txt';
221+
$versionFile = $jsonDir.'/version.txt';
249222

250-
file_put_contents($versionFile, "$icuVersionInDownload\n");
251-
252-
echo "Wrote $versionFile.\n";
253-
}
223+
file_put_contents($versionFile, "$icuVersionInDownload\n");
254224

225+
echo "Wrote $versionFile.\n";
255226
echo "Done.\n";

src/Symfony/Component/Intl/Resources/data/locales/az_AZ.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/bs_BA.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/en_NH.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/en_RH.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/ff_CM.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/ff_GN.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/ff_MR.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/ff_SN.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/in.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/in_ID.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/iw.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/iw_IL.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/mo.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/no.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/no_NO.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/no_NO_NY.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/pa_IN.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/pa_PK.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/sh.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/sh_BA.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/sh_CS.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/sh_YU.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/sr_BA.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/sr_CS.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/sr_Cyrl_CS.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/sr_Cyrl_YU.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/sr_Latn_CS.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/sr_Latn_YU.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/sr_ME.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/sr_RS.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/sr_XK.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Component/Intl/Resources/data/locales/sr_YU.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)