Skip to content

Commit

Permalink
Add Locale.toLanguageTag() (#8421)
Browse files Browse the repository at this point in the history
Adds `Locale.toLanguageTag()`, which returns a valid Unicode Locale Identifier
using underscores as separator that can be reliably parsed.
  • Loading branch information
hugovdm authored and cbracken committed Apr 8, 2019
1 parent efb3b00 commit 78bd318
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
24 changes: 19 additions & 5 deletions lib/ui/window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
13 changes: 10 additions & 3 deletions testing/dart/locale_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
Expand All @@ -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');
});

Expand Down

0 comments on commit 78bd318

Please sign in to comment.