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

Skip to content

Commit aa7300b

Browse files
committed
[Intl] Fixed support of Locale::getFallback
1 parent cd83745 commit aa7300b

File tree

2 files changed

+81
-10
lines changed

2 files changed

+81
-10
lines changed

src/Symfony/Component/Intl/Locale.php

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,44 @@ public static function getDefaultFallback()
6767
*/
6868
public static function getFallback($locale)
6969
{
70-
if (false === $pos = strrpos($locale, '_')) {
71-
if (self::$defaultFallback === $locale) {
72-
return 'root';
73-
}
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+
// Don't return default fallback for "root", "meta" or others
78+
// Normal locales have two or three letters
79+
if (strlen($locale) < 4) {
80+
return self::$defaultFallback;
81+
}
7482

75-
// Don't return default fallback for "root", "meta" or others
76-
// Normal locales have two or three letters
77-
if (strlen($locale) < 4) {
78-
return self::$defaultFallback;
83+
return null;
7984
}
8085

81-
return;
86+
array_pop($localesSubTags);
87+
88+
return locale_compose($localesSubTags);
89+
}
90+
91+
if (false !== $pos = strrpos($locale, '_')) {
92+
return substr($locale, 0, $pos);
8293
}
8394

84-
return substr($locale, 0, $pos);
95+
if (false !== $pos = strrpos($locale, '-')) {
96+
return substr($locale, 0, $pos);
97+
}
98+
99+
if (self::$defaultFallback === $locale) {
100+
return 'root';
101+
}
102+
103+
// Don't return default fallback for "root", "meta" or others
104+
// Normal locales have two or three letters
105+
if (strlen($locale) < 4) {
106+
return self::$defaultFallback;
107+
}
85108
}
86109

87110
/**
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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('fr', 'fr-FR'),
24+
array('en', 'fr'),
25+
array('root', 'en'),
26+
array(null, 'root'),
27+
);
28+
29+
if (function_exists('locale_parse')) {
30+
$tests[] = array('sl_Latn_IT', 'sl_Latn_IT_nedis');
31+
$tests[] = array('sl_Latn_IT', 'sl-Latn-IT-nedis');
32+
$tests[] = array('sl_Latn', 'sl_Latn_IT');
33+
$tests[] = array('sl_Latn', 'sl-Latn-IT');
34+
$tests[] = array('sl', 'sl_Latn');
35+
$tests[] = array('sl', 'sl-Latn');
36+
}
37+
38+
return $tests;
39+
}
40+
41+
/**
42+
* @dataProvider provideGetFallbackTests
43+
*/
44+
public function testGetFallback($expected, $locale)
45+
{
46+
$this->assertSame($expected, Locale::getFallback($locale));
47+
}
48+
}

0 commit comments

Comments
 (0)
0