From 962a52546f94fc299263f6699321d777f9f22549 Mon Sep 17 00:00:00 2001 From: XinyueZ Date: Sat, 4 Sep 2021 00:55:54 +0200 Subject: [PATCH] Migrate to nullsafety --- as2f/src/main/resources/dart_i18n.mvel | 34 ++++++++++--------- as2f/src/main/resources/dart_i18n_class.mvel | 2 +- .../main/resources/dart_i18n_quantity.mvel | 7 ++-- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/as2f/src/main/resources/dart_i18n.mvel b/as2f/src/main/resources/dart_i18n.mvel index 99a3aa6..9167316 100644 --- a/as2f/src/main/resources/dart_i18n.mvel +++ b/as2f/src/main/resources/dart_i18n.mvel @@ -27,8 +27,8 @@ class GeneratedLocalizationsDelegate extends LocalizationsDelegate { ]; } - LocaleListResolutionCallback listResolution({Locale fallback}) { - return (List locales, Iterable supported) { + LocaleListResolutionCallback listResolution({Locale? fallback}) { + return (List? locales, Iterable supported) { if (locales == null || locales.isEmpty) { return fallback ?? supported.first; } else { @@ -37,13 +37,13 @@ class GeneratedLocalizationsDelegate extends LocalizationsDelegate { }; } - LocaleResolutionCallback resolution({Locale fallback}) { - return (Locale locale, Iterable supported) { + LocaleResolutionCallback resolution({Locale? fallback}) { + return (Locale? locale, Iterable supported) { return _resolve(locale, fallback, supported); }; } - Locale _resolve(Locale locale, Locale fallback, Iterable supported) { + Locale _resolve(Locale? locale, Locale? fallback, Iterable supported) { if (locale == null || !isSupported(locale)) { return fallback ?? supported.first; } @@ -60,28 +60,30 @@ class GeneratedLocalizationsDelegate extends LocalizationsDelegate { } @override - Future load(Locale locale) { - final String lang = getLang(locale); + Future load(Locale? locale) { + final String? lang = getLang(locale); if (lang != null) { switch (lang) { -@foreach{locale : locales} case "@{locale.value}": return SynchronousFuture(const @{'$'}@{locale.value}());@end{'\n'} + case "en": + return SynchronousFuture(const $en()); + case "de": + return SynchronousFuture(const $de()); default: - // NO-OP. + // NO-OP. } } return SynchronousFuture(const S()); } @override - bool isSupported(Locale locale) => - locale != null && supportedLocales.contains(locale); + bool isSupported(Locale? locale) => locale != null && supportedLocales.contains(locale); @override bool shouldReload(GeneratedLocalizationsDelegate old) => false; } -String getLang(Locale l) => l == null - ? null - : l.countryCode != null && l.countryCode.isEmpty - ? l.languageCode - : l.toString(); \ No newline at end of file +String? getLang(Locale? l) => l == null + ? null + : l.countryCode != null && l.countryCode!.isEmpty + ? l.languageCode + : l.toString(); \ No newline at end of file diff --git a/as2f/src/main/resources/dart_i18n_class.mvel b/as2f/src/main/resources/dart_i18n_class.mvel index 27af08e..69f6d22 100644 --- a/as2f/src/main/resources/dart_i18n_class.mvel +++ b/as2f/src/main/resources/dart_i18n_class.mvel @@ -5,7 +5,7 @@ class S implements WidgetsLocalizations { static const GeneratedLocalizationsDelegate delegate = GeneratedLocalizationsDelegate(); - static S of(BuildContext context) => Localizations.of(context, S); + static S of(BuildContext context) => Localizations.of(context, S) ?? S(); @override TextDirection get textDirection => TextDirection.@{textDirection.value}; diff --git a/as2f/src/main/resources/dart_i18n_quantity.mvel b/as2f/src/main/resources/dart_i18n_quantity.mvel index d8e5a9b..ab1ec03 100644 --- a/as2f/src/main/resources/dart_i18n_quantity.mvel +++ b/as2f/src/main/resources/dart_i18n_quantity.mvel @@ -1,7 +1,10 @@ String @{key}(@{emitArgs()}) { plural_rules.startRuleEvaluation(quantity); - final pluralCase = plural_rules.pluralRules["@{locale}"]() as plural_rules.PluralCase; - switch (pluralCase) { + + final pluralRulesLocale = plural_rules.pluralRules["@{locale}"]; + if (pluralRulesLocale == null) return ""; + + switch (pluralRulesLocale()) { @foreach{item : items} case plural_rules.PluralCase.@{item.quantity.value}: return "@{item.value}";@end{'\n'} default: return ""; }