Skip to content

[Intl] Fixed support of Locale::getFallback #24157

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
Sep 30, 2017
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
43 changes: 33 additions & 10 deletions src/Symfony/Component/Intl/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,44 @@ public static function getDefaultFallback()
*/
public static function getFallback($locale)
{
if (false === $pos = strrpos($locale, '_')) {
if (self::$defaultFallback === $locale) {
return 'root';
}
if (function_exists('locale_parse')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks weird to me. This function is part of ext-intl, so the polyfill will never be used when this function exists

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the point. did I miss something?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, I missed the fact that this is not a class from the polyfill. so fine

$localeSubTags = locale_parse($locale);
if (1 === count($localeSubTags)) {
if (self::$defaultFallback === $localeSubTags['language']) {
return 'root';
}

// Don't return default fallback for "root", "meta" or others
// Normal locales have two or three letters
if (strlen($locale) < 4) {
return self::$defaultFallback;
}

// Don't return default fallback for "root", "meta" or others
// Normal locales have two or three letters
if (strlen($locale) < 4) {
return self::$defaultFallback;
return null;
}

return;
array_pop($localeSubTags);

return locale_compose($localeSubTags);
}

if (false !== $pos = strrpos($locale, '_')) {
return substr($locale, 0, $pos);
}

return substr($locale, 0, $pos);
if (false !== $pos = strrpos($locale, '-')) {
return substr($locale, 0, $pos);
}

if (self::$defaultFallback === $locale) {
return 'root';
}

// Don't return default fallback for "root", "meta" or others
// Normal locales have two or three letters
if (strlen($locale) < 4) {
return self::$defaultFallback;
}
}

/**
Expand Down
49 changes: 49 additions & 0 deletions src/Symfony/Component/Intl/Tests/LocaleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Intl\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Intl\Locale;

class LocaleTest extends TestCase
{
public function provideGetFallbackTests()
{
$tests = array(
array('sl_Latn_IT', 'sl_Latn_IT_nedis'),
array('sl_Latn', 'sl_Latn_IT'),
array('fr', 'fr_FR'),
array('fr', 'fr-FR'),
array('en', 'fr'),
array('root', 'en'),
array(null, 'root'),
);

if (function_exists('locale_parse')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we also add these complex locales (sl_Latn_IT, etc.) to the normal tests when locale_parse() doesn't exist? The result would be of course different ... but I think we should test that too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added more tests.

$tests[] = array('sl_Latn_IT', 'sl-Latn-IT-nedis');
$tests[] = array('sl_Latn', 'sl-Latn_IT');
} else {
$tests[] = array('sl-Latn-IT', 'sl-Latn-IT-nedis');
$tests[] = array('sl-Latn', 'sl-Latn-IT');
}

return $tests;
}

/**
* @dataProvider provideGetFallbackTests
*/
public function testGetFallback($expected, $locale)
{
$this->assertSame($expected, Locale::getFallback($locale));
}
}