@@ -40,11 +40,6 @@ class Translator implements TranslatorInterface, TranslatorBagInterface, LocaleA
40
40
41
41
private string $ locale ;
42
42
43
- /**
44
- * @var string[]
45
- */
46
- private array $ fallbackLocales = [];
47
-
48
43
/**
49
44
* @var LoaderInterface[]
50
45
*/
@@ -62,14 +57,17 @@ class Translator implements TranslatorInterface, TranslatorBagInterface, LocaleA
62
57
63
58
private ?ConfigCacheFactoryInterface $ configCacheFactory ;
64
59
65
- private array $ parentLocales ;
66
-
67
60
private bool $ hasIntlFormatter ;
68
61
62
+ /**
63
+ * @var FallbackLocaleProvider
64
+ */
65
+ private $ fallbackLocaleProvider ;
66
+
69
67
/**
70
68
* @throws InvalidArgumentException If a locale contains invalid characters
71
69
*/
72
- public function __construct (string $ locale , MessageFormatterInterface $ formatter = null , string $ cacheDir = null , bool $ debug = false , array $ cacheVary = [])
70
+ public function __construct (string $ locale , MessageFormatterInterface $ formatter = null , string $ cacheDir = null , bool $ debug = false , array $ cacheVary = [], FallbackLocaleProvider $ fallbackLocaleProvider = null )
73
71
{
74
72
$ this ->setLocale ($ locale );
75
73
@@ -82,6 +80,7 @@ public function __construct(string $locale, MessageFormatterInterface $formatter
82
80
$ this ->debug = $ debug ;
83
81
$ this ->cacheVary = $ cacheVary ;
84
82
$ this ->hasIntlFormatter = $ formatter instanceof IntlFormatterInterface;
83
+ $ this ->fallbackLocaleProvider = $ fallbackLocaleProvider ?? new FallbackLocaleProvider ();
85
84
}
86
85
87
86
public function setConfigCacheFactory (ConfigCacheFactoryInterface $ configCacheFactory )
@@ -118,7 +117,7 @@ public function addResource(string $format, mixed $resource, string $locale, str
118
117
119
118
$ this ->resources [$ locale ][] = [$ format , $ resource , $ domain ];
120
119
121
- if (\in_array ($ locale , $ this ->fallbackLocales )) {
120
+ if (\in_array ($ locale , $ this ->fallbackLocaleProvider -> getFallbackLocales () )) {
122
121
$ this ->catalogues = [];
123
122
} else {
124
123
unset($ this ->catalogues [$ locale ]);
@@ -158,17 +157,16 @@ public function setFallbackLocales(array $locales)
158
157
$ this ->assertValidLocale ($ locale );
159
158
}
160
159
161
- $ this ->fallbackLocales = $ this ->cacheVary ['fallback_locales ' ] = $ locales ;
160
+ $ this ->fallbackLocaleProvider ->setFallbackLocales ($ locales );
161
+ $ this ->cacheVary ['fallback_locales ' ] = $ locales ;
162
162
}
163
163
164
164
/**
165
- * Gets the fallback locales.
166
- *
167
165
* @internal
168
166
*/
169
167
public function getFallbackLocales (): array
170
168
{
171
- return $ this ->fallbackLocales ;
169
+ return $ this ->fallbackLocaleProvider -> getFallbackLocales () ;
172
170
}
173
171
174
172
/**
@@ -393,43 +391,7 @@ private function loadFallbackCatalogues(string $locale): void
393
391
394
392
protected function computeFallbackLocales (string $ locale )
395
393
{
396
- $ this ->parentLocales ??= json_decode (file_get_contents (__DIR__ .'/Resources/data/parents.json ' ), true );
397
-
398
- $ originLocale = $ locale ;
399
- $ locales = [];
400
-
401
- while ($ locale ) {
402
- $ parent = $ this ->parentLocales [$ locale ] ?? null ;
403
-
404
- if ($ parent ) {
405
- $ locale = 'root ' !== $ parent ? $ parent : null ;
406
- } elseif (\function_exists ('locale_parse ' )) {
407
- $ localeSubTags = locale_parse ($ locale );
408
- $ locale = null ;
409
- if (1 < \count ($ localeSubTags )) {
410
- array_pop ($ localeSubTags );
411
- $ locale = locale_compose ($ localeSubTags ) ?: null ;
412
- }
413
- } elseif ($ i = strrpos ($ locale , '_ ' ) ?: strrpos ($ locale , '- ' )) {
414
- $ locale = substr ($ locale , 0 , $ i );
415
- } else {
416
- $ locale = null ;
417
- }
418
-
419
- if (null !== $ locale ) {
420
- $ locales [] = $ locale ;
421
- }
422
- }
423
-
424
- foreach ($ this ->fallbackLocales as $ fallback ) {
425
- if ($ fallback === $ originLocale ) {
426
- continue ;
427
- }
428
-
429
- $ locales [] = $ fallback ;
430
- }
431
-
432
- return array_unique ($ locales );
394
+ return $ this ->fallbackLocaleProvider ->computeFallbackLocales ($ locale );
433
395
}
434
396
435
397
/**
0 commit comments