diff --git a/tool/generate_localization.dart b/tool/generate_localization.dart index de1c6361..9362f1db 100644 --- a/tool/generate_localization.dart +++ b/tool/generate_localization.dart @@ -1,144 +1,31 @@ import 'dart:io'; import 'dart:convert'; +import 'localization/locale_list.dart'; +import 'localization/localization_constants.dart'; const inputPath = 'res/values/'; const outputPath = 'lib/generated/i18n.dart'; -const locales = ['en', 'de', 'es', 'hi', - 'ja', 'ko', 'nl', 'pl', - 'pt', 'ru', 'uk', 'zh']; -const header = """ -import \'dart:async\'; -import \'package:flutter/foundation.dart\'; -import \'package:flutter/material.dart\'; - -class S implements WidgetsLocalizations { - const S(); - - static S current; - - static const GeneratedLocalizationsDelegate delegate = - GeneratedLocalizationsDelegate(); - - static S of(BuildContext context) => Localizations.of(context, S); -"""; - -const textDirectionDeclaration = """ - - @override - TextDirection get textDirection => TextDirection.ltr; - -"""; - -const classDeclaration = """ -class GeneratedLocalizationsDelegate extends LocalizationsDelegate { - const GeneratedLocalizationsDelegate(); - - List get supportedLocales { - return const [ -"""; - -const middle = """ - ]; - } - - LocaleListResolutionCallback listResolution({Locale fallback, bool withCountry = true}) { - return (List locales, Iterable supported) { - if (locales == null || locales.isEmpty) { - return fallback ?? supported.first; - } else { - return _resolve(locales.first, fallback, supported, withCountry); - } - }; - } - - LocaleResolutionCallback resolution({Locale fallback, bool withCountry = true}) { - return (Locale locale, Iterable supported) { - return _resolve(locale, fallback, supported, withCountry); - }; - } - - @override - Future load(Locale locale) { - final String lang = getLang(locale); - if (lang != null) { - switch (lang) { -"""; - -const end = """ - default: - } - } - S.current = const S(); - return SynchronousFuture(S.current); - } - - @override - bool isSupported(Locale locale) => _isSupported(locale, true); - - @override - bool shouldReload(GeneratedLocalizationsDelegate old) => false; - - Locale _resolve(Locale locale, Locale fallback, Iterable supported, bool withCountry) { - if (locale == null || !_isSupported(locale, withCountry)) { - return fallback ?? supported.first; - } - - final Locale languageLocale = Locale(locale.languageCode, ""); - if (supported.contains(locale)) { - return locale; - } else if (supported.contains(languageLocale)) { - return languageLocale; - } else { - final Locale fallbackLocale = fallback ?? supported.first; - return fallbackLocale; - } - } - - bool _isSupported(Locale locale, bool withCountry) { - if (locale != null) { - for (Locale supportedLocale in supportedLocales) { - if (supportedLocale.languageCode != locale.languageCode) { - continue; - } - if (supportedLocale.countryCode == locale.countryCode) { - return true; - } - if (true != withCountry && (supportedLocale.countryCode == null || supportedLocale.countryCode.isEmpty)) { - return true; - } - } - } - return false; - } -} - -String getLang(Locale l) => l == null - ? null - : l.countryCode != null && l.countryCode.isEmpty - ? l.languageCode - : l.toString(); -"""; Future main() async { - var inputContent = File(inputPath + 'strings_en.arb').readAsStringSync(); - var config = json.decode(inputContent) as Map; var output = ''; - output += header; + output += part1; output += textDirectionDeclaration; - output += localizedStrings(config: config, hasOverride: false); - output += '}' + '\n\n'; for (var locale in locales) { + final inputContent = File(inputPath + 'strings_$locale.arb').readAsStringSync(); + final config = json.decode(inputContent) as Map; + + if (locale == locales.first) { + output += localizedStrings(config: config, hasOverride: false); + output += '}' + '\n\n'; + } + output += 'class \$$locale extends S {' + '\n'; output += ' const \$$locale();' + '\n'; if (locale != locales.first) { output += textDirectionDeclaration; - - inputContent = File(inputPath + 'strings_$locale.arb').readAsStringSync(); - config = json.decode(inputContent) as Map; - output += localizedStrings(config: config, hasOverride: true); } @@ -151,7 +38,7 @@ Future main() async { output += ' Locale("$locale", ""),' + '\n'; } - output += middle; + output += part2; for (var locale in locales) { output += ' case "$locale":' + '\n'; @@ -159,7 +46,7 @@ Future main() async { output += ' return SynchronousFuture(S.current);' + '\n'; } - output += end; + output += part3; await File(outputPath).writeAsString(output); } diff --git a/tool/localization/locale_list.dart b/tool/localization/locale_list.dart new file mode 100644 index 00000000..69200ae9 --- /dev/null +++ b/tool/localization/locale_list.dart @@ -0,0 +1,3 @@ +const locales = ['en', 'de', 'es', 'hi', + 'ja', 'ko', 'nl', 'pl', + 'pt', 'ru', 'uk', 'zh']; \ No newline at end of file diff --git a/tool/localization/localization_constants.dart b/tool/localization/localization_constants.dart new file mode 100644 index 00000000..7ec2d67b --- /dev/null +++ b/tool/localization/localization_constants.dart @@ -0,0 +1,112 @@ +const textDirectionDeclaration = """ + + @override + TextDirection get textDirection => TextDirection.ltr; + +"""; + +const classDeclaration = """ +class GeneratedLocalizationsDelegate extends LocalizationsDelegate { + const GeneratedLocalizationsDelegate(); + + List get supportedLocales { + return const [ +"""; + +const part1 = """ +import \'dart:async\'; +import \'package:flutter/foundation.dart\'; +import \'package:flutter/material.dart\'; + +class S implements WidgetsLocalizations { + const S(); + + static S current; + + static const GeneratedLocalizationsDelegate delegate = + GeneratedLocalizationsDelegate(); + + static S of(BuildContext context) => Localizations.of(context, S); +"""; + +const part2 = """ + ]; + } + + LocaleListResolutionCallback listResolution({Locale fallback, bool withCountry = true}) { + return (List locales, Iterable supported) { + if (locales == null || locales.isEmpty) { + return fallback ?? supported.first; + } else { + return _resolve(locales.first, fallback, supported, withCountry); + } + }; + } + + LocaleResolutionCallback resolution({Locale fallback, bool withCountry = true}) { + return (Locale locale, Iterable supported) { + return _resolve(locale, fallback, supported, withCountry); + }; + } + + @override + Future load(Locale locale) { + final String lang = getLang(locale); + if (lang != null) { + switch (lang) { +"""; + +const part3 = """ + default: + } + } + S.current = const S(); + return SynchronousFuture(S.current); + } + + @override + bool isSupported(Locale locale) => _isSupported(locale, true); + + @override + bool shouldReload(GeneratedLocalizationsDelegate old) => false; + + Locale _resolve(Locale locale, Locale fallback, Iterable supported, bool withCountry) { + if (locale == null || !_isSupported(locale, withCountry)) { + return fallback ?? supported.first; + } + + final Locale languageLocale = Locale(locale.languageCode, ""); + if (supported.contains(locale)) { + return locale; + } else if (supported.contains(languageLocale)) { + return languageLocale; + } else { + final Locale fallbackLocale = fallback ?? supported.first; + return fallbackLocale; + } + } + + bool _isSupported(Locale locale, bool withCountry) { + if (locale != null) { + for (Locale supportedLocale in supportedLocales) { + if (supportedLocale.languageCode != locale.languageCode) { + continue; + } + if (supportedLocale.countryCode == locale.countryCode) { + return true; + } + if (true != withCountry && (supportedLocale.countryCode == null || supportedLocale.countryCode.isEmpty)) { + return true; + } + } + } + return false; + } +} + +String getLang(Locale l) => l == null + ? null + : l.countryCode != null && l.countryCode.isEmpty + ? l.languageCode + : l.toString(); +"""; \ No newline at end of file