File tree Expand file tree Collapse file tree 4 files changed +56
-10
lines changed
src/Symfony/Component/Translation Expand file tree Collapse file tree 4 files changed +56
-10
lines changed Original file line number Diff line number Diff line change 13
13
14
14
/**
15
15
* 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.
17
17
*
18
18
* @author Matthias Pigulla <mp@webfactory.de>
19
19
*/
@@ -28,6 +28,10 @@ class FallbackLocaleProvider implements FallbackLocaleProviderInterface
28
28
29
29
public function setFallbackLocales (array $ locales ): void
30
30
{
31
+ foreach ($ locales as $ locale ) {
32
+ LocaleValidator::assertValidLocale ($ locale );
33
+ }
34
+
31
35
$ this ->fallbackLocales = $ locales ;
32
36
}
33
37
@@ -44,6 +48,8 @@ public function getFallbackLocales(): array
44
48
*/
45
49
public function computeFallbackLocales (string $ locale ): array
46
50
{
51
+ LocaleValidator::assertValidLocale ($ locale );
52
+
47
53
$ this ->parentLocales ??= json_decode (file_get_contents (__DIR__ .'/Resources/data/parents.json ' ), true );
48
54
49
55
$ originLocale = $ locale ;
Original file line number Diff line number Diff line change 11
11
12
12
namespace Symfony \Component \Translation ;
13
13
14
+ use Symfony \Component \Translation \Exception \InvalidArgumentException ;
15
+
14
16
/**
15
17
* For a given locale, implementations provide the list of alternative locales to
16
18
* try when a translation cannot be found.
19
21
*/
20
22
interface FallbackLocaleProviderInterface
21
23
{
24
+ /**
25
+ * Sets the fallback locales.
26
+ *
27
+ * @param string[] $locales
28
+ *
29
+ * @throws InvalidArgumentException If a locale contains invalid characters
30
+ */
22
31
public function setFallbackLocales (array $ locales ): void ;
23
32
24
33
/**
@@ -28,6 +37,9 @@ public function setFallbackLocales(array $locales): void;
28
37
public function getFallbackLocales (): array ;
29
38
30
39
/**
40
+ * For a given locale, this method provides the ordered list of alternative (fallback) locales
41
+ * to try.
42
+ *
31
43
* @return string[]
32
44
*/
33
45
public function computeFallbackLocales (string $ locale ): array ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -147,14 +147,10 @@ public function getLocale(): string
147
147
*/
148
148
public function setFallbackLocales (array $ locales )
149
149
{
150
+ $ this ->fallbackLocaleProvider ->setFallbackLocales ($ locales );
151
+
150
152
// needed as the fallback locales are linked to the already loaded catalogues
151
153
$ this ->catalogues = [];
152
-
153
- foreach ($ locales as $ locale ) {
154
- $ this ->assertValidLocale ($ locale );
155
- }
156
-
157
- $ this ->fallbackLocaleProvider ->setFallbackLocales ($ locales );
158
154
$ this ->cacheVary ['fallback_locales ' ] = $ locales ;
159
155
}
160
156
@@ -398,9 +394,7 @@ protected function computeFallbackLocales(string $locale)
398
394
*/
399
395
protected function assertValidLocale (string $ locale )
400
396
{
401
- if (!preg_match ('/^[a-z0-9@_ \\. \\-]*$/i ' , $ locale )) {
402
- throw new InvalidArgumentException (sprintf ('Invalid "%s" locale. ' , $ locale ));
403
- }
397
+ LocaleValidator::assertValidLocale ($ locale );
404
398
}
405
399
406
400
/**
You can’t perform that action at this time.
0 commit comments