Skip to content
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

[Intl] Load locale aliases to support alias fallbacks #26452

Closed
Closed
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
6 changes: 6 additions & 0 deletions src/Symfony/Component/Intl/Intl.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Intl\Data\Bundle\Reader\BufferedBundleReader;
use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReader;
use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReaderInterface;
use Symfony\Component\Intl\Data\Provider\LocaleDataProvider;
use Symfony\Component\Intl\Data\Provider\ScriptDataProvider;
use Symfony\Component\Intl\ResourceBundle\CurrencyBundle;
use Symfony\Component\Intl\ResourceBundle\CurrencyBundleInterface;
Expand Down Expand Up @@ -259,6 +260,11 @@ private static function getEntryReader()
new JsonBundleReader(),
self::BUFFER_SIZE
));
$localeDataProvider = new LocaleDataProvider(
self::getDataDirectory().'/'.self::LOCALE_DIR,
self::$entryReader
);
self::$entryReader->setLocaleAliases($localeDataProvider->getAliases());
}

return self::$entryReader;
Expand Down
84 changes: 84 additions & 0 deletions src/Symfony/Component/Intl/Tests/IntlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Intl\Tests;

use Symfony\Component\Intl\Intl;
use PHPUnit\Framework\TestCase;

class IntlTest extends TestCase
{
/**
* @requires extension intl
*/
public function testIsExtensionLoadedChecksIfIntlExtensionIsLoaded()
{
$this->assertTrue(Intl::isExtensionLoaded());
}

public function testGetCurrencyBundleCreatesTheCurrencyBundle()
{
$this->assertInstanceOf('Symfony\Component\Intl\ResourceBundle\CurrencyBundleInterface', Intl::getCurrencyBundle());
}

public function testGetLanguageBundleCreatesTheLanguageBundle()
{
$this->assertInstanceOf('Symfony\Component\Intl\ResourceBundle\LanguageBundleInterface', Intl::getLanguageBundle());
}

public function testGetLocaleBundleCreatesTheLocaleBundle()
{
$this->assertInstanceOf('Symfony\Component\Intl\ResourceBundle\LocaleBundleInterface', Intl::getLocaleBundle());
}

public function testGetRegionBundleCreatesTheRegionBundle()
{
$this->assertInstanceOf('Symfony\Component\Intl\ResourceBundle\LocaleBundleInterface', Intl::getLocaleBundle());
}

public function testGetIcuVersionReadsTheVersionOfInstalledIcuLibrary()
{
$this->assertStringMatchesFormat('%d.%d', Intl::getIcuVersion());
}

public function testGetIcuDataVersionReadsTheVersionOfInstalledIcuData()
{
$this->assertStringMatchesFormat('%d.%d', Intl::getIcuDataVersion());
}

public function testGetIcuStubVersionReadsTheVersionOfBundledStubs()
{
$this->assertStringMatchesFormat('%d.%d', Intl::getIcuStubVersion());
}

public function testGetDataDirectoryReturnsThePathToIcuData()
{
$this->assertTrue(is_dir(Intl::getDataDirectory()));
}

/**
* @requires extension intl
*/
public function testLocaleAliasesAreLoaded()
{
\Locale::setDefault('zh_TW');
$countryNameZhTw = Intl::getRegionBundle()->getCountryName('AD');

\Locale::setDefault('zh_Hant_TW');
$countryNameHantZhTw = Intl::getRegionBundle()->getCountryName('AD');

\Locale::setDefault('zh');
$countryNameZh = Intl::getRegionBundle()->getCountryName('AD');

$this->assertSame($countryNameZhTw, $countryNameHantZhTw, 'zh_TW is an alias to zh_Hant_TW');
$this->assertNotSame($countryNameZh, $countryNameZhTw, 'zh_TW does not fall back to zh');
}
}