8000 bug #26452 [Intl] Load locale aliases to support alias fallbacks (jak… · symfony/symfony@c202a37 · GitHub
[go: up one dir, main page]

Skip to content

Commit c202a37

Browse files
committed
bug #26452 [Intl] Load locale aliases to support alias fallbacks (jakzal)
This PR was squashed before being merged into the 2.7 branch (closes #26452). Discussion ---------- [Intl] Load locale aliases to support alias fallbacks | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #21457 | License | MIT | Doc PR | - For example, `zh_TW` is an alias to `zh_Hant_TW`. Without aliases,` zh_TW` would fall back to `zh` (which is incorrect). With aliases loaded, `zh_TW` will fall back properly to `zh_Hant_TW`. Judging by git history this has never worked. ```php \Locale::setDefault('zh'); dump(Intl::getRegionBundle()->getCountryName('AD')); \Locale::setDefault('zh_TW'); dump(Intl::getRegionBundle()->getCountryName('AD')); \Locale::setDefault('zh_Hant_TW'); dump(Intl::getRegionBundle()->getCountryName('AD')); ``` Before: ``` "安道尔" "安道尔" "安道爾" ``` After: ``` "安道尔" "安道爾" "安道爾" ``` All tests are passing, including those from the `intl-data` group. Commits ------- 1debf79 [Intl] Load locale aliases to support alias fallbacks
2 parents 76c1251 + 1debf79 commit c202a37

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

src/Symfony/Component/Intl/Intl.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Intl\Data\Bundle\Reader\BufferedBundleReader;
1616
use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReader;
1717
use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReaderInterface;
18+
use Symfony\Component\Intl\Data\Provider\LocaleDataProvider;
1819
use Symfony\Component\Intl\Data\Provider\ScriptDataProvider;
1920
use Symfony\Component\Intl\ResourceBundle\CurrencyBundle;
2021
use Symfony\Component\Intl\ResourceBundle\CurrencyBundleInterface;
@@ -259,6 +260,11 @@ private static function getEntryReader()
259260
new JsonBundleReader(),
260261
self::BUFFER_SIZE
261262
));
263+
$localeDataProvider = new LocaleDataProvider(
264+
self::getDataDirectory().'/'.self::LOCALE_DIR,
265+
self::$entryReader
266+
);
267+
self::$entryReader->setLocaleAliases($localeDataProvider->getAliases());
262268
}
263269

264270
return self::$entryReader;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Intl\Tests;
13+
14+
use Symfony\Component\Intl\Intl;
15+
use PHPUnit\Framework\TestCase;
16+
17+
class IntlTest extends TestCase
18+
{
19+
/**
20+
* @requires extension intl
21+
*/
22+
public function testIsExtensionLoadedChecksIfIntlExtensionIsLoaded()
23+
{
24+
$this->assertTrue(Intl::isExtensionLoaded());
25+
}
26+
27+
public function testGetCurrencyBundleCreatesTheCurrencyBundle()
28+
{
29+
$this->assertInstanceOf('Symfony\Component\Intl\ResourceBundle\CurrencyBundleInterface', Intl::getCurrencyBundle());
30+
}
31+
32+
public function testGetLanguageBundleCreatesTheLanguageBundle()
33+
{
34+
$this->assertInstanceOf('Symfony\Component\Intl\ResourceBundle\LanguageBundleInterface', Intl::getLanguageBundle());
35+
}
36+
37+
public function testGetLocaleBundleCreatesTheLocaleBundle()
38+
{
39+
$this->assertInstanceOf('Symfony\Component\Intl\ResourceBundle\LocaleBundleInterface', Intl::getLocaleBundle());
40+
}
41+
42+
public function testGetRegionBundleCreatesTheRegionBundle()
43+
{
44+
$this->assertInstanceOf('Symfony\Component\Intl\ResourceBundle\LocaleBundleInterface', Intl::getLocaleBundle());
45+
}
46+
47+
public function testGetIcuVersionReadsTheVersionOfInstalledIcuLibrary()
48+
{
49+
$this->assertStringMatchesFormat('%d.%d', Intl::getIcuVersion());
50+
}
51+
52+
public function testGetIcuDataVersionReadsTheVersionOfInstalledIcuData()
53+
{
54+
$this->assertStringMatchesFormat('%d.%d', Intl::getIcuDataVersion());
55+
}
56+
57+
public function testGetIcuStubVersionReadsTheVersionOfBundledStubs()
58+
{
59+
$this->assertStringMatchesFormat('%d.%d', Intl::getIcuStubVersion());
60+
}
61+
62+
public function testGetDataDirectoryReturnsThePathToIcuData()
63+
{
64+
$this->assertTrue(is_dir(Intl::getDataDirectory()));
65+
}
66+
67+
/**
68+
* @requires extension intl
69+
*/
70+
public function testLocaleAliasesAreLoaded()
71+
{
72+
\Locale::setDefault('zh_TW');
73+
$countryNameZhTw = Intl::getRegionBundle()->getCountryName('AD');
74+
75+
\Locale::setDefault('zh_Hant_TW');
76+
$countryNameHantZhTw = Intl::getRegionBundle()->getCountryName('AD');
77+
78+
\Locale::setDefault('zh');
79+
$countryNameZh = Intl::getRegionBundle()->getCountryName('AD');
80+
81+
$this->assertSame($countryNameZhTw, $countryNameHantZhTw, 'zh_TW is an alias to zh_Hant_TW');
82+
$this->assertNotSame($countryNameZh, $countryNameZhTw, 'zh_TW does not fall back to zh');
83+
}
84+
}

0 commit comments

Comments
 (0)
0