generate method
Implementation
String generate(List<String> locales, List<String> messages) {
final canonicalizedLocales = locales.map<Locale>(
(it) => Locale.parse(it),
);
final msg = messages.map((it) => "$it;".indent(2)).join("\n");
final loadLocalizationSwitchCase = """
switch (localeName) {
${canonicalizedLocales.map((it) => Intl.canonicalizedLocale(it.toLanguageTag())).map((it) => 'case "$it":\n return new $it();'.indent(2)).join("\n")}
default:
throw Exception('Could not find the locale: ' + localeName);
}"""
.indent(4);
final supportLocale = canonicalizedLocales
.map((it) => _convertLocaleToSubtagFormat(it))
.map((it) => "$it,".indent(4))
.join('\n');
return """
class Localized {
static const delegate = LocalizedDelegate();
static Localized of(BuildContext context) {
final localized = Localizations.of<Localized>(context, Localized);
if(localized == null) throw Exception('Could not find a Localization with the given context.');
return localized;
}
$msg
}
class LocalizedDelegate extends LocalizationsDelegate<Localized> {
List<Locale> get supportedLocales => [
$supportLocale
];
const LocalizedDelegate();
@override
bool isSupported(Locale locale) {
return supportedLocales.contains(locale);
}
@override
Future<Localized> load(Locale locale) async {
final String localeName = Intl.canonicalizedLocale(locale.toString());
Intl.defaultLocale = localeName;
$loadLocalizationSwitchCase
}
@override
bool shouldReload(LocalizationsDelegate<Localized> old) {
return true;
}
}""";
}