Skip to content

Commit 7ba97ad

Browse files
committed
Move local validation out of Translator and use it to validate locales in FallbackLocaleProvider as well
1 parent bc8e9b4 commit 7ba97ad

File tree

4 files changed

+56
-10
lines changed

4 files changed

+56
-10
lines changed

src/Symfony/Component/Translation/FallbackLocaleProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
/**
1515
* Derives fallback locales based on ICU parent locale information, by shortening locale
16-
* sub tags and ultimately by going through a list of configured fallbacl locales.
16+
* sub tags and ultimately by going through a list of configured fallback locales.
1717
*
1818
* @author Matthias Pigulla <mp@webfactory.de>
1919
*/
@@ -28,6 +28,10 @@ class FallbackLocaleProvider implements FallbackLocaleProviderInterface
2828

2929
public function setFallbackLocales(array $locales): void
3030
{
31+
foreach ($locales as $locale) {
32+
LocaleValidator::assertValidLocale($locale);
33+
}
34+
3135
$this->fallbackLocales = $locales;
3236
}
3337

@@ -44,6 +48,8 @@ public function getFallbackLocales(): array
4448
*/
4549
public function computeFallbackLocales(string $locale): array
4650
{
51+
LocaleValidator::assertValidLocale($locale);
52+
4753
$this->parentLocales ??= json_decode(file_get_contents(__DIR__.'/Resources/data/parents.json'), true);
4854

4955
$originLocale = $locale;

src/Symfony/Component/Translation/FallbackLocaleProviderInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Translation;
1313

14+
use Symfony\Component\Translation\Exception\InvalidArgumentException;
15+
1416
/**
1517
* For a given locale, implementations provide the list of alternative locales to
1618
* try when a translation cannot be found.
@@ -19,6 +21,13 @@
1921
*/
2022
interface FallbackLocaleProviderInterface
2123
{
24+
/**
25+
* Sets the fallback locales.
26+
*
27+
* @param string[] $locales
28+
*
29+
* @throws InvalidArgumentException If a locale contains invalid characters
30+
*/
2231
public function setFallbackLocales(array $locales): void;
2332

2433
/**
@@ -28,6 +37,9 @@ public function setFallbackLocales(array $locales): void;
2837
public function getFallbackLocales(): array;
2938

3039
/**
40+
* For a given locale, this method provides the ordered list of alternative (fallback) locales
41+
* to try.
42+
*
3143
* @return string[]
3244
*/
3345
public function computeFallbackLocales(string $locale): array;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Translation;
13+
14+
use Symfony\Component\Translation\Exception\InvalidArgumentException;
15+
16+
/**
17+
* Asserts (syntactical) validity for given locale identifiers.
18+
*
19+
* @author Matthias Pigulla <mp@webfactory.de>
20+
*/
21+
class LocaleValidator
22+
{
23+
/**
24+
* Asserts that the locale is valid, throws an Exception if not.
25+
*
26+
* @throws InvalidArgumentException If the locale contains invalid characters
27+
*/
28+
public static function assertValidLocale(string $locale): void
29+
{
30+
if (!preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
31+
throw new InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
32+
}
33+
}
34+
}

src/Symfony/Component/Translation/Translator.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,10 @@ public function getLocale(): string
147147
*/
148148
public function setFallbackLocales(array $locales)
149149
{
150+
$this->fallbackLocaleProvider->setFallbackLocales($locales);
151+
150152
// needed as the fallback locales are linked to the already loaded catalogues
151153
$this->catalogues = [];
152-
153-
foreach ($locales as $locale) {
154-
$this->assertValidLocale($locale);
155-
}
156-
157-
$this->fallbackLocaleProvider->setFallbackLocales($locales);
158154
$this->cacheVary['fallback_locales'] = $locales;
159155
}
160156

@@ -398,9 +394,7 @@ protected function computeFallbackLocales(string $locale)
398394
*/
399395
protected function assertValidLocale(string $locale)
400396
{
401-
if (!preg_match('/^[a-z0-9@_\\.\\-]*$/i', $locale)) {
402-
throw new InvalidArgumentException(sprintf('Invalid "%s" locale.', $locale));
403-
}
397+
LocaleValidator::assertValidLocale($locale);
404398
}
405399

406400
/**

0 commit comments

Comments
 (0)