8000 [Intl] Fixed support of Locale::getFallback · symfony/symfony@ebc80a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit ebc80a1

Browse files
committed
[Intl] Fixed support of Locale::getFallback
1 parent bcfe152 commit ebc80a1

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

src/Symfony/Component/Intl/Locale.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,21 @@ public static function getDefaultFallback()
6767
*/
6868
public static function getFallback($locale)
6969
{
70+
if (function_exists('locale_parse')) {
71+
$localesSubTags = locale_parse($locale);
72+
if (1 === count($localesSubTags)) {
73+
if (self::$defaultFallback === $localesSubTags['language']) {
74+
return 'root';
75+
}
76+
77+
return self::$defaultFallback;
78+
}
79+
80+
array_pop($localesSubTags);
81+
82+
return locale_compose($localesSubTags);
83+
}
84+
7085
if (false === $pos = strrpos($locale, '_')) {
7186
if (self::$defaultFallback === $locale) {
7287
return 'root';
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
10000
@@ -0,0 +1,46 @@
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 PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Intl\Locale;
16+
17+
class LocaleTest extends TestCase
18+
{
19+
public function provideGetFallbackTests()
20+
{
21+
$tests = array(
22+
array('fr', 'fr_FR'),
23+
array('en', 'fr'),
24+
array('root', 'en'),
25+
);
26+
27+
if (function_exists('locale_parse')) {
28+
$tests[] = array('sl_Latn_IT', 'sl_Latn_IT_nedis');
29+
$tests[] = array('sl_Latn_IT', 'sl-Latn-IT-nedis');
30+
$tests[] = array('sl_Latn', 'sl_Latn_IT');
31+
$tests[] = array('sl_Latn', 'sl-Latn-IT');
32+
$tests[] = array('sl', 'sl_Latn');
33+
$tests[] = array('sl', 'sl-Latn');
34+
}
35+
36+
return $tests;
37+
}
38+
39+
/**
40+
* @dataProvider provideGetFallbackTests
41+
*/
42+
public function testGetFallback($expected, $locale)
43+
{
44+
$this->assertSame($expected, Locale::getFallback($locale));
45+
}
46+
}

0 commit comments

Comments
 (0)
0