Add Locale.toLanguageTag() (flutter/engine#8421)
Adds `Locale.toLanguageTag()`, which returns a valid Unicode Locale Identifier using underscores as separator that can be reliably parsed.
This commit is contained in:
@@ -374,21 +374,35 @@ class Locale {
|
||||
static Locale cachedLocale;
|
||||
static String cachedLocaleString;
|
||||
|
||||
/// Returns a string representing the locale.
|
||||
///
|
||||
/// This identifier happens to be a valid Unicode Locale Identifier using
|
||||
/// underscores as separator, however it is intended to be used for debugging
|
||||
/// purposes only. For parseable results, use [toLanguageTag] instead.
|
||||
@override
|
||||
String toString() {
|
||||
String toString() => _toLanguageTag('_');
|
||||
|
||||
/// Returns a syntactically valid Unicode BCP47 Locale Identifier.
|
||||
///
|
||||
/// Some examples of such identifiers: "en", "es-419", "hi-Deva-IN" and
|
||||
/// "zh-Hans-CN". See http://www.unicode.org/reports/tr35/ for technical
|
||||
/// details.
|
||||
String toLanguageTag() => _toLanguageTag();
|
||||
|
||||
String _toLanguageTag([String separator = '-']) {
|
||||
if (!identical(cachedLocale, this)) {
|
||||
cachedLocale = this;
|
||||
cachedLocaleString = _rawToString();
|
||||
cachedLocaleString = _rawToString(separator);
|
||||
}
|
||||
return cachedLocaleString;
|
||||
}
|
||||
|
||||
String _rawToString() {
|
||||
String _rawToString(String separator) {
|
||||
final StringBuffer out = StringBuffer(languageCode);
|
||||
if (scriptCode != null)
|
||||
out.write('_$scriptCode');
|
||||
out.write('$separator$scriptCode');
|
||||
if (_countryCode != null)
|
||||
out.write('_$countryCode');
|
||||
out.write('$separator$countryCode');
|
||||
return out.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,13 +9,15 @@ import 'package:test/test.dart';
|
||||
void main() {
|
||||
test('Locale', () {
|
||||
const Null $null = null;
|
||||
expect(const Locale('en').toString(), 'en');
|
||||
expect(const Locale('en').toLanguageTag(), 'en');
|
||||
expect(const Locale('en'), const Locale('en', $null));
|
||||
expect(const Locale('en').hashCode, const Locale('en', $null).hashCode);
|
||||
expect(const Locale('en'), isNot(const Locale('en', '')));
|
||||
expect(const Locale('en').hashCode, isNot(const Locale('en', '').hashCode));
|
||||
expect(const Locale('en', 'US').toLanguageTag(), 'en-US');
|
||||
expect(const Locale('en', 'US').toString(), 'en_US');
|
||||
expect(const Locale('iw').toString(), 'he');
|
||||
expect(const Locale('iw').toLanguageTag(), 'he');
|
||||
expect(const Locale('iw', 'DD').toLanguageTag(), 'he-DE');
|
||||
expect(const Locale('iw', 'DD').toString(), 'he_DE');
|
||||
expect(const Locale('iw', 'DD'), const Locale('he', 'DE'));
|
||||
});
|
||||
@@ -25,17 +27,22 @@ void main() {
|
||||
expect(const Locale.fromSubtags().scriptCode, null);
|
||||
expect(const Locale.fromSubtags().countryCode, null);
|
||||
|
||||
expect(const Locale.fromSubtags(languageCode: 'en').toString(), 'en');
|
||||
expect(const Locale.fromSubtags(languageCode: 'en').toLanguageTag(), 'en');
|
||||
expect(const Locale.fromSubtags(languageCode: 'en').languageCode, 'en');
|
||||
expect(const Locale.fromSubtags(scriptCode: 'Latn').toLanguageTag(), 'und-Latn');
|
||||
expect(const Locale.fromSubtags(scriptCode: 'Latn').toString(), 'und_Latn');
|
||||
expect(const Locale.fromSubtags(scriptCode: 'Latn').scriptCode, 'Latn');
|
||||
expect(const Locale.fromSubtags(countryCode: 'US').toLanguageTag(), 'und-US');
|
||||
expect(const Locale.fromSubtags(countryCode: 'US').toString(), 'und_US');
|
||||
expect(const Locale.fromSubtags(countryCode: 'US').countryCode, 'US');
|
||||
|
||||
expect(const Locale.fromSubtags(languageCode: 'es', countryCode: '419').toLanguageTag(), 'es-419');
|
||||
expect(const Locale.fromSubtags(languageCode: 'es', countryCode: '419').toString(), 'es_419');
|
||||
expect(const Locale.fromSubtags(languageCode: 'es', countryCode: '419').languageCode, 'es');
|
||||
expect(const Locale.fromSubtags(languageCode: 'es', countryCode: '419').scriptCode, null);
|
||||
expect(const Locale.fromSubtags(languageCode: 'es', countryCode: '419').countryCode, '419');
|
||||
|
||||
expect(const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans', countryCode: 'CN').toLanguageTag(), 'zh-Hans-CN');
|
||||
expect(const Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans', countryCode: 'CN').toString(), 'zh_Hans_CN');
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user