Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Intl] Add polyfill for Locale::canonicalize() #26152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions 37 src/Symfony/Component/Intl/Locale/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
/**
* Replacement for PHP's native {@link \Locale} class.
*
* The only method supported in this class is {@link getDefault}. This method
* will always return "en". All other methods will throw an exception when used.
* The only methods supported in this class are `getDefault` and `canonicalize`.
* All other methods will throw an exception when used.
*
* @author Eriksen Costa <eriksen.costa@infranology.com.br>
* @author Bernhard Schussek <bschussek@gmail.com>
Expand Down Expand Up @@ -57,6 +57,39 @@ public static function acceptFromHttp($header)
throw new MethodNotImplementedException(__METHOD__);
}

/**
* Returns a canonicalized locale string.
*
* This polyfill doesn't implement the full-spec algorithm. It only
* canonicalizes locale strings handled by the `LocaleBundle` class.
*
* @param string $locale
*
* @return string
*/
public static function canonicalize($locale)
{
$locale = (string) $locale;

if ('' === $locale || '.' === $locale[0]) {
return self::getDefault();
}

if (!preg_match('/^([a-z]{2})[-_]([a-z]{2})(?:([a-z]{2})(?:[-_]([a-z]{2}))?)?(?:\..*)?$/i', $locale, $m)) {
return $locale;
}

if (!empty($m[4])) {
return strtolower($m[1]).'_'.ucfirst(strtolower($m[2].$m[3])).'_'.strtoupper($m[4]);
}

if (!empty($m[3])) {
return strtolower($m[1]).'_'.ucfirst(strtolower($m[2].$m[3]));
}

return strtolower($m[1]).'_'.strtoupper($m[2]);
}

/**
* Not supported. Returns a correctly ordered and delimited locale code.
*
Expand Down
11 changes: 11 additions & 0 deletions 11 src/Symfony/Component/Intl/Tests/Locale/LocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ public function testAcceptFromHttp()
$this->call('acceptFromHttp', 'pt-br,en-us;q=0.7,en;q=0.5');
}

public function testCanonicalize()
{
$this->assertSame('en', $this->call('canonicalize', ''));
$this->assertSame('en', $this->call('canonicalize', '.utf8'));
$this->assertSame('fr_FR', $this->call('canonicalize', 'FR-fr'));
$this->assertSame('fr_FR', $this->call('canonicalize', 'FR-fr.utf8'));
$this->assertSame('uz_Latn', $this->call('canonicalize', 'UZ-lATN'));
$this->assertSame('uz_Cyrl_UZ', $this->call('canonicalize', 'UZ-cYRL-uz'));
$this->assertSame('123', $this->call('canonicalize', 123));
}

/**
* @expectedException \Symfony\Component\Intl\Exception\MethodNotImplementedException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Intl\Intl;
use Symfony\Component\Intl\Locale as IntlLocale;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
Expand Down Expand Up @@ -43,7 +42,7 @@ public function validate($value, Constraint $constraint)

$value = (string) $value;
if ($constraint->canonicalize) {
$value = IntlLocale::canonicalize($value);
$value = \Locale::canonicalize($value);
}
$localeBundle = Intl::getLocaleBundle();
$locales = $localeBundle->getLocaleNames();
Expand Down
3 changes: 2 additions & 1 deletion 3 src/Symfony/Component/Validator/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"symfony/http-foundation": "~4.1",
"symfony/http-kernel": "~3.4|~4.0",
"symfony/var-dumper": "~3.4|~4.0",
"symfony/intl": "~3.4|~4.0",
"symfony/intl": "~4.1",
"symfony/yaml": "~3.4|~4.0",
"symfony/config": "~3.4|~4.0",
"symfony/dependency-injection": "~3.4|~4.0",
Expand All @@ -39,6 +39,7 @@
"phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0",
"symfony/dependency-injection": "<3.4",
"symfony/http-kernel": "<3.4",
"symfony/intl": "<4.1",
"symfony/yaml": "<3.4"
},
"suggest": {
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.