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 $ fallbackMapping ;
40
34
private $ fallbackCache = [];
41
35
42
- public function __construct (BundleCompilerInterface $ compiler , $ dirName , LanguageDataProvider $ languageDataProvider , ScriptDataProvider $ scriptDataProvider , RegionDataProvider $ regionDataProvider )
43
- {
44
- parent ::__construct ($ compiler , $ dirName );
45
-
46
- $ this ->languageDataProvider = $ languageDataProvider ;
47
- $ this ->scriptDataProvider = $ scriptDataProvider ;
48
- $ this ->regionDataProvider = $ regionDataProvider ;
49
- }
50
-
51
36
/**
52
37
* {@inheritdoc}
53
38
*/
@@ -66,8 +51,12 @@ protected function scanLocales(LocaleScanner $scanner, $sourceDir)
66
51
protected function compileTemporaryBundles (BundleCompilerInterface $ compiler , $ sourceDir , $ tempDir )
67
52
{
68
53
$ filesystem = new Filesystem ();
69
- $ filesystem ->mkdir ($ tempDir .'/lang ' );
54
+ $ filesystem ->mkdir ([
55
+ $ tempDir .'/lang ' ,
56
+ $ tempDir .'/region ' ,
57
+ ]);
70
58
$ compiler ->compile ($ sourceDir .'/lang ' , $ tempDir .'/lang ' );
59
+ $ compiler ->compile ($ sourceDir .'/region ' , $ tempDir .'/region ' );
71
60
}
72
61
73
62
/**
@@ -83,19 +72,14 @@ protected function preGenerate()
83
72
*/
84
73
protected function generateDataForLocale (BundleEntryReaderInterface $ reader , $ tempDir , $ displayLocale )
85
74
{
86
- // Generate aliases, needed to enable proper fallback from alias to its
87
- // target
75
+ // Don't generate aliases, as they are resolved during runtime
88
76
if (isset ($ this ->localeAliases [$ displayLocale ])) {
89
- return [ ' %%ALIAS ' => $ this -> localeAliases [ $ displayLocale ]] ;
77
+ return ;
90
78
}
91
79
92
80
// Generate locale names for all locales that have translations in
93
81
// at least the language or the region bundle
94
- try {
95
- $ displayFormat = $ reader ->readEntry ($ tempDir .'/lang ' , $ displayLocale , ['localeDisplayPattern ' ]);
96
- } catch (MissingResourceException $ e ) {
97
- $ displayFormat = $ reader ->readEntry ($ tempDir .'/lang ' , 'root ' , ['localeDisplayPattern ' ]);
98
- }
82
+ $ displayFormat = $ reader ->readEntry ($ tempDir .'/lang ' , $ displayLocale , ['localeDisplayPattern ' ]);
99
83
$ pattern = $ displayFormat ['pattern ' ] ?? '{0} ({1}) ' ;
100
84
$ separator = $ displayFormat ['separator ' ] ?? '{0}, {1} ' ;
101
85
$ localeNames = [];
@@ -110,7 +94,7 @@ protected function generateDataForLocale(BundleEntryReaderInterface $reader, $te
110
94
// Each locale name has the form: "Language (Script, Region, Variant1, ...)
111
95
// Script, Region and Variants are optional. If none of them is
112
96
// available, the braces are not printed.
113
- $ localeNames [$ locale ] = $ this ->generateLocaleName ($ locale , $ displayLocale , $ pattern , $ separator );
97
+ $ localeNames [$ locale ] = $ this ->generateLocaleName ($ reader , $ tempDir , $ locale , $ displayLocale , $ pattern , $ separator );
114
98
} catch (MissingResourceException $ e ) {
115
99
// Silently ignore incomplete locale names
116
100
// In this case one should configure at least one fallback locale that is complete (e.g. English) during
@@ -158,22 +142,26 @@ protected function generateDataForMeta(BundleEntryReaderInterface $reader, $temp
158
142
/**
159
143
* @return string
160
144
*/
161
- private function generateLocaleName ($ locale , $ displayLocale , $ pattern , $ separator )
145
+ private function generateLocaleName (BundleEntryReaderInterface $ reader , $ tempDir , $ locale , $ displayLocale , $ pattern , $ separator )
162
146
{
163
147
// Apply generic notation using square brackets as described per http://cldr.unicode.org/translation/language-names
164
- $ name = str_replace (['( ' , ') ' ], ['[ ' , '] ' ], $ this -> languageDataProvider -> getName ( \Locale::getPrimaryLanguage ($ locale ), $ displayLocale ));
148
+ $ name = str_replace (['( ' , ') ' ], ['[ ' , '] ' ], $ reader -> readEntry ( $ tempDir . ' /lang ' , $ displayLocale , [ ' Languages ' , \Locale::getPrimaryLanguage ($ locale )] ));
165
149
$ extras = [];
166
150
167
151
// Discover the name of the script part of the locale
168
152
// i.e. in zh_Hans_MO, "Hans" is the script
169
153
if ($ script = \Locale::getScript ($ locale )) {
170
- $ extras [] = str_replace (['( ' , ') ' ], ['[ ' , '] ' ], $ this -> scriptDataProvider -> getName ( $ script , $ displayLocale ));
154
+ $ extras [] = str_replace (['( ' , ') ' ], ['[ ' , '] ' ], $ reader -> readEntry ( $ tempDir . ' /lang ' , $ displayLocale, [ ' Scripts ' , $ script ] ));
171
155
}
172
156
173
157
// Discover the name of the region part of the locale
174
158
// i.e. in de_AT, "AT" is the region
175
159
if ($ region = \Locale::getRegion ($ locale )) {
176
- $ extras [] = str_replace (['( ' , ') ' ], ['[ ' , '] ' ], $ this ->regionDataProvider ->getName ($ region , $ displayLocale ));
160
+ if (!RegionDataGenerator::isValidCountryCode ($ region )) {
161
+ throw new MissingResourceException ('Skipping " ' .$ locale .'" due an invalid country. ' );
162
+ }
163
+
164
+ $ extras [] = str_replace (['( ' , ') ' ], ['[ ' , '] ' ], $ reader ->readEntry ($ tempDir .'/region ' , $ displayLocale , ['Countries ' , $ region ]));
177
165
}
178
166
179
167
if ($ extras ) {
0 commit comments