14
14
use Symfony \Component \Filesystem \Filesystem ;
15
15
use Symfony \Component \Intl \Data \Bundle \Compiler \BundleCompilerInterface ;
16
16
use Symfony \Component \Intl \Data \Bundle \Reader \BundleEntryReaderInterface ;
17
- use Symfony \Component \Intl \Data \Provider \LanguageDataProvider ;
18
- use Symfony \Component \Intl \Data \Provider \RegionDataProvider ;
19
- use Symfony \Component \Intl \Data \Provider \ScriptDataProvider ;
20
17
use Symfony \Component \Intl \Data \Util \LocaleScanner ;
21
18
use Symfony \Component \Intl \Exception \MissingResourceException ;
22
19
use Symfony \Component \Intl \Locale ;
31
28
*/
32
29
class LocaleDataGenerator extends AbstractDataGenerator
33
30
{
34
- private $ languageDataProvider ;
35
- private $ scriptDataProvider ;
36
- private $ regionDataProvider ;
37
31
private $ locales ;
38
32
private $ localeAliases ;
39
33
private $ localeParents ;
40
34
private $ fallbackMapping ;
41
35
private $ fallbackCache = [];
42
36
43
- public function __construct (BundleCompilerInterface $ compiler , string $ dirName , LanguageDataProvider $ languageDataProvider , ScriptDataProvider $ scriptDataProvider , RegionDataProvider $ regionDataProvider )
44
- {
45
- parent ::__construct ($ compiler , $ dirName );
46
-
47
- $ this ->languageDataProvider = $ languageDataProvider ;
48
- $ this ->scriptDataProvider = $ scriptDataProvider ;
49
- $ this ->regionDataProvider = $ regionDataProvider ;
50
- }
51
-
52
37
/**
53
38
* {@inheritdoc}
54
39
*/
@@ -68,8 +53,12 @@ protected function scanLocales(LocaleScanner $scanner, $sourceDir)
68
53
protected function compileTemporaryBundles (BundleCompilerInterface $ compiler , $ sourceDir , $ tempDir )
69
54
{
70
55
$ filesystem = new Filesystem ();
71
- $ filesystem ->mkdir ($ tempDir .'/lang ' );
56
+ $ filesystem ->mkdir ([
57
+ $ tempDir .'/lang ' ,
58
+ $ tempDir .'/region ' ,
59
+ ]);
72
60
$ compiler ->compile ($ sourceDir .'/lang ' , $ tempDir .'/lang ' );
61
+ $ compiler ->compile ($ sourceDir .'/region ' , $ tempDir .'/region ' );
73
62
}
74
63
75
64
/**
@@ -91,19 +80,14 @@ protected function preGenerate()
91
80
*/
92
81
protected function generateDataForLocale (BundleEntryReaderInterface $ reader , $ tempDir , $ displayLocale )
93
82
{
94
- // Generate aliases, needed to enable proper fallback from alias to its
95
- // target
83
+ // Don't generate aliases, as they are resolved during runtime
96
84
if (isset ($ this ->localeAliases [$ displayLocale ])) {
97
- return [ ' %%ALIAS ' => $ this -> localeAliases [ $ displayLocale ]] ;
85
+ return ;
98
86
}
99
87
100
88
// Generate locale names for all locales that have translations in
101
89
// at least the language or the region bundle
102
- try {
103
- $ displayFormat = $ reader ->readEntry ($ tempDir .'/lang ' , $ displayLocale , ['localeDisplayPattern ' ]);
104
- } catch (MissingResourceException $ e ) {
105
- $ displayFormat = $ reader ->readEntry ($ tempDir .'/lang ' , 'root ' , ['localeDisplayPattern ' ]);
106
- }
90
+ $ displayFormat = $ reader ->readEntry ($ tempDir .'/lang ' , $ displayLocale , ['localeDisplayPattern ' ]);
107
91
$ pattern = $ displayFormat ['pattern ' ] ?? '{0} ({1}) ' ;
108
92
$ separator = $ displayFormat ['separator ' ] ?? '{0}, {1} ' ;
109
93
$ localeNames = [];
@@ -118,7 +102,7 @@ protected function generateDataForLocale(BundleEntryReaderInterface $reader, $te
118
102
// Each locale name has the form: "Language (Script, Region, Variant1, ...)
119
103
// Script, Region and Variants are optional. If none of them is
120
104
// available, the braces are not printed.
121
- $ localeNames [$ locale ] = $ this ->generateLocaleName ($ locale , $ displayLocale , $ pattern , $ separator );
105
+ $ localeNames [$ locale ] = $ this ->generateLocaleName ($ reader , $ tempDir , $ locale , $ displayLocale , $ pattern , $ separator );
122
106
} catch (MissingResourceException $ e ) {
123
107
// Silently ignore incomplete locale names
124
108
// In this case one should configure at least one fallback locale that is complete (e.g. English) during
@@ -166,22 +150,26 @@ protected function generateDataForMeta(BundleEntryReaderInterface $reader, $temp
166
150
/**
167
151
* @return string
168
152
*/
169
- private function generateLocaleName ($ locale , $ displayLocale , $ pattern , $ separator )
153
+ private function generateLocaleName (BundleEntryReaderInterface $ reader , $ tempDir , $ locale , $ displayLocale , $ pattern , $ separator )
170
154
{
171
155
// Apply generic notation using square brackets as described per http://cldr.unicode.org/translation/language-names
172
- $ name = str_replace (['( ' , ') ' ], ['[ ' , '] ' ], $ this -> languageDataProvider -> getName ( \Locale::getPrimaryLanguage ($ locale ), $ displayLocale ));
156
+ $ name = str_replace (['( ' , ') ' ], ['[ ' , '] ' ], $ reader -> readEntry ( $ tempDir . ' /lang ' , $ displayLocale , [ ' Languages ' , \Locale::getPrimaryLanguage ($ locale )] ));
173
157
$ extras = [];
174
158
175
159
// Discover the name of the script part of the locale
176
160
// i.e. in zh_Hans_MO, "Hans" is the script
177
161
if ($ script = \Locale::getScript ($ locale )) {
178
- $ extras [] = str_replace (['( ' , ') ' ], ['[ ' , '] ' ], $ this -> scriptDataProvider -> getName ( $ script , $ displayLocale ));
162
+ $ extras [] = str_replace (['( ' , ') ' ], ['[ ' , '] ' ], $ reader -> readEntry ( $ tempDir . ' /lang ' , $ displayLocale, [ ' Scripts ' , $ script ] ));
179
163
}
180
164
181
165
// Discover the name of the region part of the locale
182
166
// i.e. in de_AT, "AT" is the region
183
167
if ($ region = \Locale::getRegion ($ locale )) {
184
- $ extras [] = str_replace (['( ' , ') ' ], ['[ ' , '] ' ], $ this ->regionDataProvider ->getName ($ region , $ displayLocale ));
168
+ if (!RegionDataGenerator::isValidCountryCode ($ region )) {
169
+ throw new MissingResourceException ('Skipping " ' .$ locale .'" due an invalid country. ' );
170
+ }
171
+
172
+ $ extras [] = str_replace (['( ' , ') ' ], ['[ ' , '] ' ], $ reader ->readEntry ($ tempDir .'/region ' , $ displayLocale , ['Countries ' , $ region ]));
185
173
}
186
174
187
175
if ($ extras ) {
0 commit comments