Skip to content

Commit bd23c3b

Browse files
[Intl] Add polyfill for Locale::canonicalize()
1 parent 65b2bcd commit bd23c3b

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

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

Lines changed: 33 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 <eriksen.costa@infranology.com.br>
2323
* @author Bernhard Schussek <bschussek@gmail.com>
@@ -57,6 +57,37 @@ 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+
if ('' === $locale = (string) $locale) {
73+
return self::getDefault();
74+
}
75+
76+
if (preg_match('/^([a-z]{2})[-_]([a-z]{2})(?:([a-z]{2})(?:[-_]([a-z]{2}))?)?$/i', $locale, $m)) {
77+
if (!empty($m[4])) {
78+
return strtolower($m[1]).'_'.ucfirst(strtolower($m[2].$m[3])).'_'.strtoupper($m[4]);
79+
}
80+
81+
if (!empty($m[3])) {
82+
return strtolower($m[1]).'_'.ucfirst(strtolower($m[2].$m[3]));
83+
}
84+
85+
return strtolower($m[1]).'_'.strtoupper($m[2]);
86+
}
87+
88+
return $locale;
89+
}
90+
6091
/**
6192
* Not supported. Returns a correctly ordered and delimited locale code.
6293
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ 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('fr_FR', $this->call('canonicalize', 'FR-fr'));
28+
$this->assertSame('uz_Latn', $this->call('canonicalize', 'UZ-lATN'));
29+
$this->assertSame('uz_Cyrl_UZ', $this->call('canonicalize', 'UZ-cYRL-uz'));
30+
$this->assertSame('123', $this->call('canonicalize', 123));
31+
}
32+
2433
/**
2534
* @expectedException \Symfony\Component\Intl\Exception\MethodNotImplementedException
2635
*/

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy