Skip to content

Commit f73d8d2

Browse files
committed
bug #24157 [Intl] Fixed support of Locale::getFallback (lyrixx)
This PR was merged into the 2.7 branch. Discussion ---------- [Intl] Fixed support of Locale::getFallback | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #24154 | License | MIT | Doc PR | Commits ------- 2560552 [Intl] Fixed support of Locale::getFallback
2 parents 166f64e + 2560552 commit f73d8d2

File tree

2 files changed

+82
-10
lines changed

2 files changed

+82
-10
lines changed

src/Symfony/Component/Intl/Locale.php

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

0 commit comments

Comments
 (0)