Skip to content

[Intl] fix Locale::getFallback() throwing exception on long $locale #40162

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 17, 2021
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
3 changes: 2 additions & 1 deletion src/Symfony/Component/Intl/Locale.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public static function getDefaultFallback(): ?string
public static function getFallback(string $locale): ?string
{
if (\function_exists('locale_parse')) {
$localeSubTags = locale_parse($locale);
$localeSubTags = locale_parse($locale) ?? ['language' => $locale];

if (1 === \count($localeSubTags)) {
if ('root' !== self::$defaultFallback && self::$defaultFallback === $localeSubTags['language']) {
return 'root';
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Intl/Tests/LocaleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,16 @@ public function testDefaultRootFallback()

Locale::setDefaultFallback($prev);
}

/**
* @requires function locale_parse
*/
public function testLongLocaleFallback()
{
$locale = 'LC_TYPE=fr_FR.UTF-8;LC_NUMERIC=C;LC_TIME=fr_FR.UTF-8;LC_COLLATE=fr_FR.UTF-8;'.
'LC_MONETARY=fr_FR.UTF-8;LC_MESSAGES=fr_FR.UTF-8;LC_PAPER=fr_FR.UTF-8;LC_NAME=fr_FR.UTF-8;'.
'LC_ADDRESS=fr_FR.UTF-8;LC_TELEPHONE=fr_FR.UTF-8;LC_MEASUREMENT=fr_FR.UTF-8;LC_IDENTIFICATION=fr_FR.UTF-8';

$this->assertNull(Locale::getFallback($locale));
Copy link
Contributor

Choose a reason for hiding this comment

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

do we agree root is the fallback then?

compared to above test $this->assertSame('root', Locale::getFallback('nl'));

Copy link
Contributor

Choose a reason for hiding this comment

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

actually $defaultFallback / en is expected

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ro0NL the function is returning null on long $locale by this change:
$localeSubTags = locale_parse($locale) ?? ['language' => $locale];

Copy link
Member

Choose a reason for hiding this comment

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

I think null is correct also.

}
}