16
16
use Symfony \Component \Intl \Data \Bundle \Reader \BundleEntryReaderInterface ;
17
17
use Symfony \Component \Intl \Data \Util \LocaleScanner ;
18
18
use Symfony \Component \Intl \Exception \MissingResourceException ;
19
- use Symfony \Component \Intl \Locale ;
20
19
21
20
/**
22
21
* The rule for compiling the locale bundle.
28
27
*/
29
28
class LocaleDataGenerator extends AbstractDataGenerator
30
29
{
31
- private $ locales ;
32
- private $ localeAliases ;
33
- private $ localeParents ;
34
- private $ fallbackMapping ;
35
- private $ fallbackCache = [];
30
+ use FallbackTrait ;
31
+
32
+ private $ locales = [] ;
33
+ private $ localeAliases = [] ;
34
+ private $ localeParents = [];
36
35
37
36
/**
38
37
* {@inheritdoc}
@@ -42,7 +41,6 @@ protected function scanLocales(LocaleScanner $scanner, $sourceDir)
42
41
$ this ->locales = $ scanner ->scanLocales ($ sourceDir .'/locales ' );
43
42
$ this ->localeAliases = $ scanner ->scanAliases ($ sourceDir .'/locales ' );
44
43
$ this ->localeParents = $ scanner ->scanParents ($ sourceDir .'/locales ' );
45
- $ this ->fallbackMapping = $ this ->generateFallbackMapping (array_diff ($ this ->locales , array_keys ($ this ->localeAliases )), $ this ->localeAliases );
46
44
47
45
return $ this ->locales ;
48
46
}
@@ -66,8 +64,6 @@ protected function compileTemporaryBundles(BundleCompilerInterface $compiler, $s
66
64
*/
67
65
protected function preGenerate ()
68
66
{
69
- $ this ->fallbackCache = [];
70
-
71
67
// Write parents locale file for the Translation component
72
68
\file_put_contents (
73
69
__DIR__ .'/../../../Translation/Resources/data/parents.json ' ,
@@ -81,7 +77,8 @@ protected function preGenerate()
81
77
protected function generateDataForLocale (BundleEntryReaderInterface $ reader , $ tempDir , $ displayLocale )
82
78
{
83
79
// Don't generate aliases, as they are resolved during runtime
84
- if (isset ($ this ->localeAliases [$ displayLocale ])) {
80
+ // Unless an alias is needed as fallback for de-duplication purposes
81
+ if (isset ($ this ->localeAliases [$ displayLocale ]) && !$ this ->generatingFallback ) {
85
82
return ;
86
83
}
87
84
@@ -93,7 +90,7 @@ protected function generateDataForLocale(BundleEntryReaderInterface $reader, $te
93
90
$ localeNames = [];
94
91
foreach ($ this ->locales as $ locale ) {
95
92
// Ensure a normalized list of pure locales
96
- if (isset ( $ this -> localeAliases [ $ displayLocale ]) || \Locale::getAllVariants ($ locale )) {
93
+ if (\Locale::getAllVariants ($ locale )) {
97
94
continue ;
98
95
}
99
96
@@ -110,21 +107,27 @@ protected function generateDataForLocale(BundleEntryReaderInterface $reader, $te
110
107
}
111
108
}
112
109
113
- // Process again to de-duplicate locales and their fallback locales
114
- // Only keep the differences
115
- $ fallback = $ displayLocale ;
116
- while (isset ($ this ->fallbackMapping [$ fallback ])) {
117
- if (!isset ($ this ->fallbackCache [$ fallback = $ this ->fallbackMapping [$ fallback ]])) {
118
- $ this ->fallbackCache [$ fallback ] = $ this ->generateDataForLocale ($ reader , $ tempDir , $ fallback ) ?: [];
119
- }
120
- if (isset ($ this ->fallbackCache [$ fallback ]['Names ' ])) {
121
- $ localeNames = array_diff ($ localeNames , $ this ->fallbackCache [$ fallback ]['Names ' ]);
122
- }
110
+ $ data = [
111
+ 'Names ' => $ localeNames ,
112
+ ];
113
+
114
+ // Don't de-duplicate a fallback locale
115
+ // Ensures the display locale can be de-duplicated on itself
116
+ if ($ this ->generatingFallback ) {
117
+ return $ data ;
123
118
}
124
119
125
- if ($ localeNames ) {
126
- return ['Names ' => $ localeNames ];
120
+ // Process again to de-duplicate locale and its fallback locales
121
+ // Only keep the differences
122
+ $ fallbackData = $ this ->generateFallbackData ($ reader , $ tempDir , $ displayLocale );
123
+ if (isset ($ fallbackData ['Names ' ])) {
124
+ $ data ['Names ' ] = array_diff ($ data ['Names ' ], $ fallbackData ['Names ' ]);
125
+ }
126
+ if (!$ data ['Names ' ]) {
127
+ return ;
127
128
}
129
+
130
+ return $ data ;
128
131
}
129
132
130
133
/**
@@ -139,12 +142,10 @@ protected function generateDataForRoot(BundleEntryReaderInterface $reader, $temp
139
142
*/
140
143
protected function generateDataForMeta (BundleEntryReaderInterface $ reader , $ tempDir )
141
144
{
142
- if ($ this ->locales || $ this ->localeAliases ) {
143
- return [
144
- 'Locales ' => $ this ->locales ,
145
- 'Aliases ' => $ this ->localeAliases ,
146
- ];
147
- }
145
+ return [
146
+ 'Locales ' => $ this ->locales ,
147
+ 'Aliases ' => $ this ->localeAliases ,
148
+ ];
148
149
}
149
150
150
151
/**
@@ -183,30 +184,4 @@ private function generateLocaleName(BundleEntryReaderInterface $reader, $tempDir
183
184
184
185
return $ name ;
185
186
}
186
-
187
- private function generateFallbackMapping (array $ displayLocales , array $ aliases )
188
- {
189
- $ displayLocales = array_flip ($ displayLocales );
190
- $ mapping = [];
191
-
192
- foreach ($ displayLocales as $ displayLocale => $ _ ) {
193
- $ mapping [$ displayLocale ] = null ;
194
- $ fallback = $ displayLocale ;
195
-
196
- // Recursively search for a fallback locale until one is found
197
- while (null !== ($ fallback = Locale::getFallback ($ fallback ))) {
198
- // Currently, no locale has an alias as fallback locale.
199
- // If this starts to be the case, we need to add code here.
200
- \assert (!isset ($ aliases [$ fallback ]));
201
-
202
- // Check whether the fallback exists
203
- if (isset ($ displayLocales [$ fallback ])) {
204
- $ mapping [$ displayLocale ] = $ fallback ;
205
- break ;
206
- }
207
- }
208
- }
209
-
210
- return $ mapping ;
211
- }
212
187
}
0 commit comments