Skip to content
This repository was archived by the owner on Aug 30, 2023. It is now read-only.

Commit dda8ade

Browse files
authored
GitHub Sync (#501)
* Project import generated by Copybara. PiperOrigin-RevId: 479539369 * Keep gitignore to avoid name clashes * Keep readme fix * Merge rest
1 parent 7639a15 commit dda8ade

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ OWNERS
1010
README.google.md
1111
copy.bara.sky
1212
test/number_format_compact_google3_icu_test.dart
13-
update_from_cldr_data.sh
13+
update_from_cldr_data.sh

lib/intl.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ class Intl {
222222
/// The [other] named argument is mandatory.
223223
/// The [precision] is the number of fractional digits that would be rendered
224224
/// when [howMany] is formatted. In some cases just knowing the numeric value
225-
/// of [howMany] itsef is not enough, for example "1 mile" vs "1.00 miles"
225+
/// of [howMany] itself is not enough, for example "1 mile" vs "1.00 miles"
226226
///
227227
/// For an explanation of plurals and the [zero], [one], [two], [few], [many]
228228
/// categories see http://cldr.unicode.org/index/cldr-spec/plural-rules

lib/src/intl_helpers.dart

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,25 @@ String? computeMessageName(String? name, String? text, String? meaning) {
130130
return meaning == null ? text : '${text}_$meaning';
131131
}
132132

133+
/// Returns an index of a separator between language and region.
134+
///
135+
/// Assumes that language length can be only 2 or 3.
136+
int _separatorIndex(String locale) {
137+
if (locale.length < 3) {
138+
return -1;
139+
}
140+
if (locale[2] == '-' || locale[2] == '_') {
141+
return 2;
142+
}
143+
if (locale.length < 4) {
144+
return -1;
145+
}
146+
if (locale[3] == '-' || locale[3] == '_') {
147+
return 3;
148+
}
149+
return -1;
150+
}
151+
133152
String canonicalizedLocale(String? aLocale) {
134153
// Locales of length < 5 are presumably two-letter forms, or else malformed.
135154
// We return them unmodified and if correct they will be found.
@@ -141,11 +160,16 @@ String canonicalizedLocale(String? aLocale) {
141160
if (aLocale == null) return global_state.getCurrentLocale();
142161
if (aLocale == 'C') return 'en_ISO';
143162
if (aLocale.length < 5) return aLocale;
144-
if (aLocale[2] != '-' && (aLocale[2] != '_')) return aLocale;
145-
var region = aLocale.substring(3);
146-
// If it's longer than three it's something odd, so don't touch it.
163+
164+
var separatorIndex = _separatorIndex(aLocale);
165+
if (separatorIndex == -1) {
166+
return aLocale;
167+
}
168+
var language = aLocale.substring(0, separatorIndex);
169+
var region = aLocale.substring(separatorIndex + 1);
170+
// If it's longer than three it's something odd, so don't touch it.
147171
if (region.length <= 3) region = region.toUpperCase();
148-
return '${aLocale[0]}${aLocale[1]}_$region';
172+
return '${language}_$region';
149173
}
150174

151175
String? verifiedLocale(String? newLocale, bool Function(String) localeExists,
@@ -182,6 +206,22 @@ String _throwLocaleError(String localeName) {
182206

183207
/// Return the short version of a locale name, e.g. 'en_US' => 'en'
184208
String shortLocale(String aLocale) {
185-
if (aLocale.length < 2) return aLocale;
186-
return aLocale.substring(0, 2).toLowerCase();
209+
// TODO(b/241094372): Remove this check.
210+
if (aLocale == 'invalid') {
211+
return 'in';
212+
}
213+
if (aLocale.length < 2) {
214+
return aLocale;
215+
}
216+
var separatorIndex = _separatorIndex(aLocale);
217+
if (separatorIndex == -1) {
218+
if (aLocale.length < 4) {
219+
// aLocale is already only a language code.
220+
return aLocale.toLowerCase();
221+
} else {
222+
// Something weird, returning as is.
223+
return aLocale;
224+
}
225+
}
226+
return aLocale.substring(0, separatorIndex).toLowerCase();
187227
}

test/intl_test.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ void main() {
3333
expect(Intl.canonicalizedLocale('xx-yyy'), 'xx_YYY');
3434
expect(Intl.canonicalizedLocale('xx_YYY'), 'xx_YYY');
3535
expect(Intl.canonicalizedLocale('C'), 'en_ISO');
36+
expect(Intl.canonicalizedLocale('gsw-ch'), 'gsw_CH');
37+
expect(Intl.canonicalizedLocale('test-locale'), 'test-locale');
38+
});
39+
40+
test('Shortening locales', () {
41+
expect(Intl.shortLocale('en_US'), 'en');
42+
expect(Intl.shortLocale('gsw_CH'), 'gsw');
43+
expect(Intl.shortLocale('C'), 'C');
44+
expect(Intl.shortLocale('test-locale'), 'test-locale');
45+
// TODO(b/241094372): Remove this check.
46+
expect(Intl.shortLocale('invalid'), 'in');
3647
});
3748

3849
test('Verifying locale fallback for numbers', () {
@@ -41,6 +52,7 @@ void main() {
4152
expect(Intl.verifiedLocale('es-419', NumberFormat.localeExists), 'es_419');
4253
expect(Intl.verifiedLocale('en-ZZ', NumberFormat.localeExists), 'en');
4354
expect(Intl.verifiedLocale('es-999', NumberFormat.localeExists), 'es');
55+
expect(Intl.verifiedLocale('gsw-CH', NumberFormat.localeExists), 'gsw');
4456

4557
void checkAsNumberDefault(String locale, String expected) {
4658
var oldDefault = Intl.defaultLocale;
@@ -63,6 +75,9 @@ void main() {
6375
expect(Intl.verifiedLocale('es-419', DateFormat.localeExists), 'es_419');
6476
expect(Intl.verifiedLocale('en-ZZ', DateFormat.localeExists), 'en');
6577
expect(Intl.verifiedLocale('es-999', DateFormat.localeExists), 'es');
78+
expect(Intl.verifiedLocale('gsw-CH', DateFormat.localeExists), 'gsw');
79+
// TODO(b/241094372): Remove this check.
80+
expect(Intl.verifiedLocale('invalid', DateFormat.localeExists), 'in');
6681

6782
void checkAsDateDefault(String locale, String expected) {
6883
var oldDefault = Intl.defaultLocale;

0 commit comments

Comments
 (0)