2021-03-19 18:19:14 +00:00
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
const inputPath = 'res/values/';
|
|
|
|
const outputPath = 'lib/generated/i18n.dart';
|
|
|
|
const locales = ['en', 'de', 'es', 'hi',
|
|
|
|
'ja', 'ko', 'nl', 'pl',
|
|
|
|
'pt', 'ru', 'uk', 'zh'];
|
2021-03-19 19:52:07 +00:00
|
|
|
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<S>(context, S);
|
|
|
|
""";
|
|
|
|
|
|
|
|
const textDirectionDeclaration = """
|
|
|
|
|
|
|
|
@override
|
|
|
|
TextDirection get textDirection => TextDirection.ltr;
|
|
|
|
|
|
|
|
""";
|
|
|
|
|
|
|
|
const classDeclaration = """
|
|
|
|
class GeneratedLocalizationsDelegate extends LocalizationsDelegate<S> {
|
|
|
|
const GeneratedLocalizationsDelegate();
|
|
|
|
|
|
|
|
List<Locale> get supportedLocales {
|
|
|
|
return const <Locale>[
|
|
|
|
""";
|
|
|
|
|
|
|
|
const middle = """
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
LocaleListResolutionCallback listResolution({Locale fallback, bool withCountry = true}) {
|
|
|
|
return (List<Locale> locales, Iterable<Locale> 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<Locale> supported) {
|
|
|
|
return _resolve(locale, fallback, supported, withCountry);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<S> load(Locale locale) {
|
|
|
|
final String lang = getLang(locale);
|
|
|
|
if (lang != null) {
|
|
|
|
switch (lang) {
|
|
|
|
""";
|
|
|
|
|
|
|
|
const end = """
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
S.current = const S();
|
|
|
|
return SynchronousFuture<S>(S.current);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool isSupported(Locale locale) => _isSupported(locale, true);
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool shouldReload(GeneratedLocalizationsDelegate old) => false;
|
|
|
|
|
|
|
|
Locale _resolve(Locale locale, Locale fallback, Iterable<Locale> 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();
|
|
|
|
""";
|
2021-03-19 18:19:14 +00:00
|
|
|
|
|
|
|
Future<void> main() async {
|
|
|
|
var inputContent = File(inputPath + 'strings_en.arb').readAsStringSync();
|
|
|
|
var config = json.decode(inputContent) as Map<String, dynamic>;
|
|
|
|
var output = '';
|
|
|
|
|
2021-03-19 19:52:07 +00:00
|
|
|
output += header;
|
|
|
|
output += textDirectionDeclaration;
|
2021-03-19 18:19:14 +00:00
|
|
|
output += localizedStrings(config: config, hasOverride: false);
|
|
|
|
output += '}' + '\n\n';
|
|
|
|
|
|
|
|
for (var locale in locales) {
|
|
|
|
output += 'class \$$locale extends S {' + '\n';
|
|
|
|
output += ' const \$$locale();' + '\n';
|
|
|
|
|
|
|
|
if (locale != locales.first) {
|
2021-03-19 19:52:07 +00:00
|
|
|
output += textDirectionDeclaration;
|
2021-03-19 18:19:14 +00:00
|
|
|
|
|
|
|
inputContent = File(inputPath + 'strings_$locale.arb').readAsStringSync();
|
|
|
|
config = json.decode(inputContent) as Map<String, dynamic>;
|
|
|
|
|
|
|
|
output += localizedStrings(config: config, hasOverride: true);
|
|
|
|
}
|
|
|
|
|
|
|
|
output += '}' + '\n\n';
|
|
|
|
}
|
|
|
|
|
2021-03-19 19:52:07 +00:00
|
|
|
output += classDeclaration;
|
2021-03-19 18:19:14 +00:00
|
|
|
|
|
|
|
for (var locale in locales) {
|
|
|
|
output += ' Locale("$locale", ""),' + '\n';
|
|
|
|
}
|
|
|
|
|
2021-03-19 19:52:07 +00:00
|
|
|
output += middle;
|
2021-03-19 18:19:14 +00:00
|
|
|
|
|
|
|
for (var locale in locales) {
|
|
|
|
output += ' case "$locale":' + '\n';
|
|
|
|
output += ' S.current = const \$$locale();' + '\n';
|
|
|
|
output += ' return SynchronousFuture<S>(S.current);' + '\n';
|
|
|
|
}
|
|
|
|
|
2021-03-19 19:52:07 +00:00
|
|
|
output += end;
|
2021-03-19 18:19:14 +00:00
|
|
|
|
|
|
|
await File(outputPath).writeAsString(output);
|
|
|
|
}
|
|
|
|
|
|
|
|
String localizedStrings({Map<String, dynamic> config, bool hasOverride}) {
|
|
|
|
var output = '';
|
|
|
|
|
|
|
|
final pattern = RegExp('[\$]{(.*?)}');
|
|
|
|
|
2021-03-19 19:52:07 +00:00
|
|
|
config.forEach((key, dynamic value) {
|
|
|
|
final matches = pattern.allMatches(value as String);
|
2021-03-19 18:19:14 +00:00
|
|
|
|
|
|
|
if (hasOverride) {
|
|
|
|
output += ' @override' + '\n';
|
|
|
|
}
|
|
|
|
|
2021-03-19 19:52:07 +00:00
|
|
|
if (matches.isEmpty) {
|
|
|
|
output += ' String get ${key} => \"\"\"${value}\"\"\";' + '\n';
|
2021-03-19 18:19:14 +00:00
|
|
|
} else {
|
2021-03-19 19:52:07 +00:00
|
|
|
final set = matches.map((elem) => elem.group(1)).toSet().toList();
|
|
|
|
|
2021-03-19 18:19:14 +00:00
|
|
|
output += ' String ${key}(';
|
2021-03-19 19:52:07 +00:00
|
|
|
|
|
|
|
for (var elem in set) {
|
|
|
|
if (elem == set.last) {
|
|
|
|
output += 'String ${elem}';
|
2021-03-19 18:19:14 +00:00
|
|
|
} else {
|
2021-03-19 19:52:07 +00:00
|
|
|
output += 'String ${elem}, ';
|
2021-03-19 18:19:14 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-19 19:52:07 +00:00
|
|
|
output += ') => \"\"\"${value}\"\"\";' + '\n';
|
2021-03-19 18:19:14 +00:00
|
|
|
}
|
2021-03-19 19:52:07 +00:00
|
|
|
});
|
2021-03-19 18:19:14 +00:00
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|