1616use Symfony \Component \Intl \Data \Bundle \Reader \BundleEntryReaderInterface ;
1717use Symfony \Component \Intl \Data \Util \LocaleScanner ;
1818use Symfony \Component \Intl \Exception \MissingResourceException ;
19- use Symfony \Component \Intl \Locale ;
2019
2120/**
2221 * The rule for compiling the locale bundle.
2827 */
2928class LocaleDataGeneratorextends AbstractDataGenerator
3029{
31- private $ locales ;
32- private $ localeAliases ;
33- private $ fallbackMapping ;
34- private $ fallbackCache = [];
30+ use FallbackTrait ;
31+
32+ private $ locales = [] ;
33+ private $ localeAliases = [];
3534
3635/**
3736 * {@inheritdoc}
@@ -40,7 +39,6 @@ protected function scanLocales(LocaleScanner $scanner, $sourceDir)
4039 {
4140$ this ->locales =$ scanner ->scanLocales ($ sourceDir .'/locales ' );
4241$ this ->localeAliases =$ scanner ->scanAliases ($ sourceDir .'/locales ' );
43- $ this ->fallbackMapping =$ this ->generateFallbackMapping (array_diff ($ this ->locales ,array_keys ($ this ->localeAliases )),$ this ->localeAliases );
4442
4543return $ this ->locales ;
4644 }
@@ -64,7 +62,6 @@ protected function compileTemporaryBundles(BundleCompilerInterface $compiler, $s
6462 */
6563protected function preGenerate ()
6664 {
67- $ this ->fallbackCache = [];
6865 }
6966
7067/**
@@ -73,7 +70,8 @@ protected function preGenerate()
7370protected function generateDataForLocale (BundleEntryReaderInterface $ reader ,$ tempDir ,$ displayLocale )
7471 {
7572// Don't generate aliases, as they are resolved during runtime
76- if (isset ($ this ->localeAliases [$ displayLocale ])) {
73+ // Unless an alias is needed as fallback for de-duplication purposes
74+ if (isset ($ this ->localeAliases [$ displayLocale ]) && !$ this ->generatingFallback ) {
7775return ;
7876 }
7977
@@ -85,7 +83,7 @@ protected function generateDataForLocale(BundleEntryReaderInterface $reader, $te
8583$ localeNames = [];
8684foreach ($ this ->locales as $ locale ) {
8785// Ensure a normalized list of pure locales
88- if (isset ( $ this -> localeAliases [ $ displayLocale ]) || \Locale::getAllVariants ($ locale )) {
86+ if (\Locale::getAllVariants ($ locale )) {
8987continue ;
9088 }
9189
@@ -102,21 +100,27 @@ protected function generateDataForLocale(BundleEntryReaderInterface $reader, $te
102100 }
103101 }
104102
105- // Process again to de-duplicate locales and their fallback locales
106- // Only keep the differences
107- $ fallback =$ displayLocale ;
108- while (isset ($ this ->fallbackMapping [$ fallback ])) {
109- if (!isset ($ this ->fallbackCache [$ fallback =$ this ->fallbackMapping [$ fallback ]])) {
110- $ this ->fallbackCache [$ fallback ] =$ this ->generateDataForLocale ($ reader ,$ tempDir ,$ fallback ) ?: [];
111- }
112- if (isset ($ this ->fallbackCache [$ fallback ]['Names ' ])) {
113- $ localeNames =array_diff ($ localeNames ,$ this ->fallbackCache [$ fallback ]['Names ' ]);
114- }
103+ $ data = [
104+ 'Names ' =>$ localeNames ,
105+ ];
106+
107+ // Don't de-duplicate a fallback locale
108+ // Ensures the display locale can be de-duplicated on itself
109+ if ($ this ->generatingFallback ) {
110+ return $ data ;
115111 }
116112
117- if ($ localeNames ) {
118- return ['Names ' =>$ localeNames ];
113+ // Process again to de-duplicate locale and its fallback locales
114+ // Only keep the differences
115+ $ fallbackData =$ this ->generateFallbackData ($ reader ,$ tempDir ,$ displayLocale );
116+ if (isset ($ fallbackData ['Names ' ])) {
117+ $ data ['Names ' ] =array_diff ($ data ['Names ' ],$ fallbackData ['Names ' ]);
118+ }
119+ if (!$ data ['Names ' ]) {
120+ return ;
119121 }
122+
123+ return $ data ;
120124 }
121125
122126/**
@@ -131,12 +135,10 @@ protected function generateDataForRoot(BundleEntryReaderInterface $reader, $temp
131135 */
132136protected function generateDataForMeta (BundleEntryReaderInterface $ reader ,$ tempDir )
133137 {
134- if ($ this ->locales ||$ this ->localeAliases ) {
135- return [
136- 'Locales ' =>$ this ->locales ,
137- 'Aliases ' =>$ this ->localeAliases ,
138- ];
139- }
138+ return [
139+ 'Locales ' =>$ this ->locales ,
140+ 'Aliases ' =>$ this ->localeAliases ,
141+ ];
140142 }
141143
142144/**
@@ -175,30 +177,4 @@ private function generateLocaleName(BundleEntryReaderInterface $reader, $tempDir
175177
176178return $ name ;
177179 }
178-
179- private function generateFallbackMapping (array $ displayLocales ,array $ aliases )
180- {
181- $ displayLocales =array_flip ($ displayLocales );
182- $ mapping = [];
183-
184- foreach ($ displayLocalesas $ displayLocale =>$ _ ) {
185- $ mapping [$ displayLocale ] =null ;
186- $ fallback =$ displayLocale ;
187-
188- // Recursively search for a fallback locale until one is found
189- while (null !== ($ fallback = Locale::getFallback ($ fallback ))) {
190- // Currently, no locale has an alias as fallback locale.
191- // If this starts to be the case, we need to add code here.
192- \assert (!isset ($ aliases [$ fallback ]));
193-
194- // Check whether the fallback exists
195- if (isset ($ displayLocales [$ fallback ])) {
196- $ mapping [$ displayLocale ] =$ fallback ;
197- break ;
198- }
199- }
200- }
201-
202- return $ mapping ;
203- }
204180}