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 $ fallbackMapping ;
34
- private $ fallbackCache = [];
30
+ use FallbackTrait ;
31
+
32
+ private $ locales = [] ;
33
+ private $ localeAliases = [];
35
34
36
35
/**
37
36
* {@inheritdoc}
@@ -40,7 +39,6 @@ protected function scanLocales(LocaleScanner $scanner, $sourceDir)
40
39
{
41
40
$ this ->locales = $ scanner ->scanLocales ($ sourceDir .'/locales ' );
42
41
$ this ->localeAliases = $ scanner ->scanAliases ($ sourceDir .'/locales ' );
43
- $ this ->fallbackMapping = $ this ->generateFallbackMapping (array_diff ($ this ->locales , array_keys ($ this ->localeAliases )), $ this ->localeAliases );
44
42
45
43
return $ this ->locales ;
46
44
}
@@ -64,7 +62,6 @@ protected function compileTemporaryBundles(BundleCompilerInterface $compiler, $s
64
62
*/
65
63
protected function preGenerate ()
66
64
{
67
- $ this ->fallbackCache = [];
68
65
}
69
66
70
67
/**
@@ -73,7 +70,8 @@ protected function preGenerate()
73
70
protected function generateDataForLocale (BundleEntryReaderInterface $ reader , $ tempDir , $ displayLocale )
74
71
{
75
72
// 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 ) {
77
75
return ;
78
76
}
79
77
@@ -85,7 +83,7 @@ protected function generateDataForLocale(BundleEntryReaderInterface $reader, $te
85
83
$ localeNames = [];
86
84
foreach ($ this ->locales as $ locale ) {
87
85
// Ensure a normalized list of pure locales
88
- if (isset ( $ this -> localeAliases [ $ displayLocale ]) || \Locale::getAllVariants ($ locale )) {
86
+ if (\Locale::getAllVariants ($ locale )) {
89
87
continue ;
90
88
}
91
89
@@ -102,21 +100,27 @@ protected function generateDataForLocale(BundleEntryReaderInterface $reader, $te
102
100
}
103
101
}
104
102
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 ;
115
111
}
116
112
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 ;
119
121
}
122
+
123
+ return $ data ;
120
124
}
121
125
122
126
/**
@@ -131,12 +135,10 @@ protected function generateDataForRoot(BundleEntryReaderInterface $reader, $temp
131
135
*/
132
136
protected function generateDataForMeta (BundleEntryReaderInterface $ reader , $ tempDir )
133
137
{
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
+ ];
140
142
}
141
143
142
144
/**
@@ -175,30 +177,4 @@ private function generateLocaleName(BundleEntryReaderInterface $reader, $tempDir
175
177
176
178
return $ name ;
177
179
}
178
-
179
- private function generateFallbackMapping (array $ displayLocales , array $ aliases )
180
- {
181
- $ displayLocales = array_flip ($ displayLocales );
182
- $ mapping = [];
183
-
184
- foreach ($ displayLocales as $ 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
- }
204
180
}
0 commit comments