Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 06ab73b

Browse files
feature #26152 [Intl] Add polyfill for Locale::canonicalize() (nicolas-grekas)
This PR was merged into the 4.1-dev branch. Discussion ---------- [Intl] Add polyfill for Locale::canonicalize() | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Required after #26069 Commits ------- 972a330 [Intl] Add polyfill for Locale::canonicalize()
2 parents a9f2a09 + 972a330 commit 06ab73b

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

src/Symfony/Component/Intl/Locale/Locale.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
/**
1717
* Replacement for PHP's native {@link \Locale} class.
1818
*
19-
* The only method supported in this class is {@link getDefault}. This method
20-
* will always return "en". All other methods will throw an exception when used.
19+
* The only methods supported in this class are `getDefault` and `canonicalize`.
20+
* All other methods will throw an exception when used.
2121
*
2222
* @author Eriksen Costa <[email protected]>
2323
* @author Bernhard Schussek <[email protected]>
@@ -57,6 +57,39 @@ public static function acceptFromHttp($header)
5757
throw new MethodNotImplementedException(__METHOD__);
5858
}
5959

60+
/**
61+
* Returns a canonicalized locale string.
62+
*
63+
* This polyfill doesn't implement the full-spec algorithm. It only
64+
* canonicalizes locale strings handled by the `LocaleBundle` class.
65+
*
66+
* @param string $locale
67+
*
68+
* @return string
69+
*/
70+
public static function canonicalize($locale)
71+
{
72+
$locale = (string) $locale;
73+
74+
if ('' === $locale || '.' === $locale[0]) {
75+
return self::getDefault();
76+
}
77+
78+
if (!preg_match('/^([a-z]{2})[-_]([a-z]{2})(?:([a-z]{2})(?:[-_]([a-z]{2}))?)?(?:\..*)?$/i', $locale, $m)) {
79+
return $locale;
80+
}
81+
82+
if (!empty($m[4])) {
83+
return strtolower($m[1]).'_'.ucfirst(strtolower($m[2].$m[3])).'_'.strtoupper($m[4]);
84+
}
85+
86+
if (!empty($m[3])) {
87+
return strtolower($m[1]).'_'.ucfirst(strtolower($m[2].$m[3]));
88+
}
89+
90+
return strtolower($m[1]).'_'.strtoupper($m[2]);
91+
}
92+
6093
/**
6194
* Not supported. Returns a correctly ordered and delimited locale code.
6295
*

src/Symfony/Component/Intl/Tests/Locale/LocaleTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ public function testAcceptFromHttp()
2121
$this->call('acceptFromHttp', 'pt-br,en-us;q=0.7,en;q=0.5');
2222
}
2323

24+
public function testCanonicalize()
25+
{
26+
$this->assertSame('en', $this->call('canonicalize', ''));
27+
$this->assertSame('en', $this->call('canonicalize', '.utf8'));
28+
$this->assertSame('fr_FR', $this->call('canonicalize', 'FR-fr'));
29+
$this->assertSame('fr_FR', $this->call('canonicalize', 'FR-fr.utf8'));
30+
$this->assertSame('uz_Latn', $this->call('canonicalize', 'UZ-lATN'));
31+
$this->assertSame('uz_Cyrl_UZ', $this->call('canonicalize', 'UZ-cYRL-uz'));
32+
$this->assertSame('123', $this->call('canonicalize', 123));
33+
}
34+
2435
/**
2536
* @expectedException \Symfony\Component\Intl\Exception\MethodNotImplementedException
2637
*/

src/Symfony/Component/Validator/Constraints/LocaleValidator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\Validator\Constraints;
1313

1414
use Symfony\Component\Intl\Intl;
15-
use Symfony\Component\Intl\Locale as IntlLocale;
1615
use Symfony\Component\Validator\Constraint;
1716
use Symfony\Component\Validator\ConstraintValidator;
1817
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
@@ -43,7 +42,7 @@ public function validate($value, Constraint $constraint)
4342

4443
$value = (string) $value;
4544
if ($constraint->canonicalize) {
46-
$value = IntlLocale::canonicalize($value);
45+
$value = \Locale::canonicalize($value);
4746
}
4847
$localeBundle = Intl::getLocaleBundle();
4948
$locales = $localeBundle->getLocaleNames();

src/Symfony/Component/Validator/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"symfony/http-foundation": "~4.1",
2525
"symfony/http-kernel": "~3.4|~4.0",
2626
"symfony/var-dumper": "~3.4|~4.0",
27-
"symfony/intl": "~3.4|~4.0",
27+
"symfony/intl": "~4.1",
2828
"symfony/yaml": "~3.4|~4.0",
2929
"symfony/config": "~3.4|~4.0",
3030
"symfony/dependency-injection": "~3.4|~4.0",
@@ -39,6 +39,7 @@
3939
"phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0",
4040
"symfony/dependency-injection": "<3.4",
4141
"symfony/http-kernel": "<3.4",
42+
"symfony/intl": "<4.1",
4243
"symfony/yaml": "<3.4"
4344
},
4445
"suggest": {

0 commit comments

Comments
 (0)