Skip to content

Commit

Permalink
Merge 4c46cd3 into e39a452
Browse files Browse the repository at this point in the history
  • Loading branch information
herre committed May 7, 2021
2 parents e39a452 + 4c46cd3 commit 947371f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 38 deletions.
15 changes: 6 additions & 9 deletions example/lib/util/locale/localization_delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,8 @@ typedef LocaleFilter = bool Function(String languageCode);

class LocalizationDelegate extends LocalizationsDelegate<Localization> {
static LocaleFilter? localeFilter;
static const defaultLocale = Locale('en');
static const _supportedLanguages = [
'en',
'nl',
'zh',
'fi',
];
static const defaultLocale = Locale.fromSubtags(
languageCode: 'en', scriptCode: null, countryCode: null);

static const _supportedLocales = [
Locale.fromSubtags(languageCode: 'en', scriptCode: null, countryCode: null),
Expand All @@ -29,8 +24,10 @@ class LocalizationDelegate extends LocalizationsDelegate<Localization> {
];

static List<String> get supportedLanguages {
if (localeFilter == null) return _supportedLanguages;
return _supportedLanguages
final supportedLanguageTags =
_supportedLocales.map((e) => e.toLanguageTag()).toList(growable: false);
if (localeFilter == null) return supportedLanguageTags;
return supportedLanguageTags
.where((element) => localeFilter?.call(element) ?? true)
.toList();
}
Expand Down
14 changes: 8 additions & 6 deletions lib/src/locale_gen_parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@ import 'package:intl/locale.dart';
class LocaleGenParser {
const LocaleGenParser._();

static String parseSupportedLanguage(String language) {
static String parseSupportedLocale(String language) {
try {
final locale = Locale.tryParse(language);
final languageCode = locale?.languageCode;
return " '$languageCode',";
final scriptCode = locale?.scriptCode;
final countryCode = locale?.countryCode;
return " Locale.fromSubtags(languageCode: ${languageCode != null ? "'$languageCode'" : null}, scriptCode: ${scriptCode != null ? "'$scriptCode'" : null}, countryCode: ${countryCode != null ? "'$countryCode'" : null}),";
} catch (_) {
return " '$language',";
return " Locale('$language'),";
}
}

static String parseSupportedLocale(String language) {
static String parseDefaultLanguageLocale(String language) {
try {
final locale = Locale.tryParse(language);
final languageCode = locale?.languageCode;
final scriptCode = locale?.scriptCode;
final countryCode = locale?.countryCode;
return " Locale.fromSubtags(languageCode: ${languageCode != null ? "'$languageCode'" : null}, scriptCode: ${scriptCode != null ? "'$scriptCode'" : null}, countryCode: ${countryCode != null ? "'$countryCode'" : null}),";
return " static const defaultLocale = Locale.fromSubtags(languageCode: ${languageCode != null ? "'$languageCode'" : null}, scriptCode: ${scriptCode != null ? "'$scriptCode'" : null}, countryCode: ${countryCode != null ? "'$countryCode'" : null});";
} catch (_) {
return " Locale('$language'),";
return " static const defaultLocale = Locale('$language');";
}
}
}
13 changes: 4 additions & 9 deletions lib/src/locale_gen_writer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,7 @@ class LocaleGenWriter {
..writeln(
'class LocalizationDelegate extends LocalizationsDelegate<Localization> {')
..writeln(' static LocaleFilter? localeFilter;')
..writeln(
" static const defaultLocale = Locale('${params.defaultLanguage}');")
..writeln(' static const _supportedLanguages = [');
params.languages.forEach((language) =>
sb.writeln(LocaleGenParser.parseSupportedLanguage(language)));
sb
..writeln(' ];')
..writeln(LocaleGenParser.parseDefaultLanguageLocale(params.defaultLanguage))
..writeln()
..writeln(' static const _supportedLocales = [');
params.languages.forEach((language) =>
Expand All @@ -199,9 +193,10 @@ class LocaleGenWriter {
..writeln(' ];')
..writeln()
..writeln(' static List<String> get supportedLanguages {')
..writeln(' if (localeFilter == null) return _supportedLanguages;')
..writeln(' final supportedLanguageTags = _supportedLocales.map((e) => e.toLanguageTag()).toList(growable: false);')
..writeln(' if (localeFilter == null) return supportedLanguageTags;')
..writeln(
' return _supportedLanguages.where((element) => localeFilter?.call(element) ?? true).toList();')
' return supportedLanguageTags.where((element) => localeFilter?.call(element) ?? true).toList();')
..writeln(' }')
..writeln()
..writeln(' static List<Locale> get supportedLocales {')
Expand Down
31 changes: 17 additions & 14 deletions test/locale_gen_parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@ import 'package:locale_gen/src/locale_gen_parser.dart';
import 'package:test/test.dart';

void main() {
group('LocaleGen parseSupportedLanguage', () {
test('parses nl locale', () {
final result = LocaleGenParser.parseSupportedLanguage('nl');
expect(result, " 'nl',");
});
test('parses zh-Hans-CN locale', () {
final result = LocaleGenParser.parseSupportedLanguage('zh-Hans-CN');
expect(result, " 'zh',");
});
test('parses fi-FI locale', () {
final result = LocaleGenParser.parseSupportedLanguage('fi-FI');
expect(result, " 'fi',");
});
});
group('LocaleGen parseSupportedLocale', () {
test('parses nl locale', () {
final result = LocaleGenParser.parseSupportedLocale('nl');
Expand All @@ -33,4 +19,21 @@ void main() {
" Locale.fromSubtags(languageCode: 'fi', scriptCode: null, countryCode: 'FI'),");
});
});
group('LocaleGen parseDefaultLanguageLocale', () {
test('parses nl locale', () {
final result = LocaleGenParser.parseDefaultLanguageLocale('nl');
expect(result,
" static const defaultLocale = Locale.fromSubtags(languageCode: 'nl', scriptCode: null, countryCode: null);");
});
test('parses zh-Hans-CN locale', () {
final result = LocaleGenParser.parseDefaultLanguageLocale('zh-Hans-CN');
expect(result,
" static const defaultLocale = Locale.fromSubtags(languageCode: 'zh', scriptCode: 'Hans', countryCode: 'CN');");
});
test('parses fi-FI locale', () {
final result = LocaleGenParser.parseDefaultLanguageLocale('fi-FI');
expect(result,
" static const defaultLocale = Locale.fromSubtags(languageCode: 'fi', scriptCode: null, countryCode: 'FI');");
});
});
}

0 comments on commit 947371f

Please sign in to comment.