@@ -128,10 +128,10 @@ String? computeMessageName(String? name, String? text, String? meaning) {
128
128
return meaning == null ? text : '${text }_$meaning ' ;
129
129
}
130
130
131
- /// Returns an index of a separator between language and region .
131
+ /// Returns an index of a separator between language and other subtags .
132
132
///
133
133
/// Assumes that language length can be only 2 or 3.
134
- int _separatorIndex (String locale) {
134
+ int _languageSeparatorIndex (String locale) {
135
135
if (locale.length < 3 ) {
136
136
return - 1 ;
137
137
}
@@ -147,6 +147,19 @@ int _separatorIndex(String locale) {
147
147
return - 1 ;
148
148
}
149
149
150
+ /// Returns an index of a separator between script and region.
151
+ ///
152
+ /// Assumes that script contains exactly 4 characters.
153
+ int _scriptSeparatorIndex (String region) {
154
+ if (region.length < 5 ) {
155
+ return - 1 ;
156
+ }
157
+ if (region[4 ] == '-' || region[4 ] == '_' ) {
158
+ return 4 ;
159
+ }
160
+ return - 1 ;
161
+ }
162
+
150
163
String canonicalizedLocale (String ? aLocale) {
151
164
// Locales of length < 5 are presumably two-letter forms, or else malformed.
152
165
// We return them unmodified and if correct they will be found.
@@ -159,7 +172,7 @@ String canonicalizedLocale(String? aLocale) {
159
172
if (aLocale == 'C' ) return 'en_ISO' ;
160
173
if (aLocale.length < 5 ) return aLocale;
161
174
162
- var separatorIndex = _separatorIndex (aLocale);
175
+ var separatorIndex = _languageSeparatorIndex (aLocale);
163
176
if (separatorIndex == - 1 ) {
164
177
return aLocale;
165
178
}
@@ -186,9 +199,10 @@ String? verifiedLocale(String? newLocale, bool Function(String) localeExists,
186
199
}
187
200
final fallbackOptions = [
188
201
canonicalizedLocale,
189
- shortLocale,
202
+ languageRegionOnlyLocale,
203
+ languageOnlyLocale,
190
204
deprecatedLocale,
191
- (locale) => deprecatedLocale (shortLocale (locale)),
205
+ (locale) => deprecatedLocale (languageOnlyLocale (locale)),
192
206
(locale) => deprecatedLocale (canonicalizedLocale (locale)),
193
207
(_) => 'fallback'
194
208
];
@@ -233,15 +247,15 @@ String deprecatedLocale(String aLocale) {
233
247
}
234
248
235
249
/// Return the short version of a locale name, e.g. 'en_US' => 'en'
236
- String shortLocale (String aLocale) {
250
+ String languageOnlyLocale (String aLocale) {
237
251
// TODO(b/241094372): Remove this check.
238
252
if (aLocale == 'invalid' ) {
239
253
return 'in' ;
240
254
}
241
255
if (aLocale.length < 2 ) {
242
256
return aLocale;
243
257
}
244
- var separatorIndex = _separatorIndex (aLocale);
258
+ var separatorIndex = _languageSeparatorIndex (aLocale);
245
259
if (separatorIndex == - 1 ) {
246
260
if (aLocale.length < 4 ) {
247
261
// aLocale is already only a language code.
@@ -253,3 +267,19 @@ String shortLocale(String aLocale) {
253
267
}
254
268
return aLocale.substring (0 , separatorIndex).toLowerCase ();
255
269
}
270
+
271
+ String languageRegionOnlyLocale (String aLocale) {
272
+ if (aLocale.length < 10 ) return aLocale;
273
+
274
+ var separatorIndex = _languageSeparatorIndex (aLocale);
275
+ if (separatorIndex == - 1 ) {
276
+ return aLocale;
277
+ }
278
+ var language = aLocale.substring (0 , separatorIndex);
279
+ var subtags = aLocale.substring (separatorIndex + 1 );
280
+ separatorIndex = _scriptSeparatorIndex (subtags);
281
+ var region = subtags.substring (separatorIndex + 1 );
282
+ // If it's longer than three it's something odd, so don't touch it.
283
+ if (region.length <= 3 ) region = region.toUpperCase ();
284
+ return '${language }_$region ' ;
285
+ }
0 commit comments