Skip to content

Commit

Permalink
Improve locale utility classes
Browse files Browse the repository at this point in the history
  • Loading branch information
alesimula committed Jan 11, 2022
1 parent 21a8f00 commit 50aebfe
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/utils/locale_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import 'string_utils.dart';


extension LocaleUtils on Locale {
_NamedLocale get _asNamedLocale => _NamedLocale(languageCode, countryCode);
_NamedLocale _asNamedLocale([int? lcid]) => lcid == null ? _NamedLocale(languageCode, countryCode) : _NamedLocaleLCID(lcid, languageCode, countryCode);
_SystemLocale get _asSystemLocale => _SystemLocale(languageCode, countryCode);
static late final NamedLocale _DEFAULT_SYSTEM_LOCALE = _SystemLocale("en");
static late final _DEFAULT_LOCALIZATION = lookupAppLocalizations(_DEFAULT_SYSTEM_LOCALE);
Expand Down Expand Up @@ -60,7 +60,7 @@ class _WinLocale {
LPWSTR? lpName = malloc<WCHAR>(LOCALE_NAME_MAX_LENGTH).cast<Utf16>();
try {
int result = _LCIDToLocaleName(lcid, lpName, LOCALE_NAME_MAX_LENGTH, 0);
return result != 0 ? lpName.toDartString().asLocale?._asNamedLocale : null;
return result != 0 ? lpName.toDartString().asLocale?._asNamedLocale(lcid) : null;
}
finally {free(lpName);}
}
Expand All @@ -78,7 +78,13 @@ class _NamedLocale extends NamedLocale {
@override late final String name = lookupAppLocalizations(this).locale_desc;
@override late final int lcid = toLCID() ?? (){throw ArgumentError("Unknown language tag: ${toLanguageTag()}");}();
@override int get hashCode => lcid;
@override bool operator ==(Object other) => other is! _SystemLocale && ((other is NamedLocale && other.languageCode == languageCode && other.lcid == lcid) || super==other);
@override bool operator ==(Object other) => other is! _SystemLocale && (other is NamedLocale ? other.languageCode == languageCode && other.lcid == lcid : super==other);
}

class _NamedLocaleLCID extends _NamedLocale {
_NamedLocaleLCID(this.lcid, String _languageCode, [String? _countryCode]) : super(_languageCode, _countryCode);
// ignore: overridden_fields
@override final int lcid;
}

class _SystemLocale extends NamedLocale {
Expand Down

0 comments on commit 50aebfe

Please sign in to comment.