2021-03-19 18:19:14 +00:00
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:convert';
|
2021-03-22 18:08:10 +00:00
|
|
|
import 'localization/localization_constants.dart';
|
2021-03-24 16:52:30 +00:00
|
|
|
import 'utils/utils.dart';
|
2021-03-19 18:19:14 +00:00
|
|
|
|
|
|
|
const inputPath = 'res/values/';
|
2021-03-24 16:52:30 +00:00
|
|
|
const outputPath = 'lib/generated/';
|
|
|
|
const localizationFileName = 'i18n.dart';
|
|
|
|
const localeListFileName = 'locales.dart';
|
|
|
|
const srcDir = 'srcDir';
|
|
|
|
const defaultLocale = 'en';
|
|
|
|
|
|
|
|
Future<void> main(List<String> args) async {
|
|
|
|
final extraInfo = args.isNotEmpty ?
|
|
|
|
args.fold(<String, dynamic>{}, (Map<String, dynamic> acc, String arg) {
|
|
|
|
final parts = arg.split('=');
|
|
|
|
var key = normalizeKeyName(parts[0]);
|
|
|
|
if (key.contains('--')) {
|
|
|
|
key = key.substring(2);
|
2021-03-22 18:08:10 +00:00
|
|
|
}
|
2021-03-24 16:52:30 +00:00
|
|
|
acc[key] = parts.length > 1
|
|
|
|
? parts[1].isNotEmpty
|
|
|
|
? parts[1]
|
|
|
|
: inputPath
|
|
|
|
: inputPath;
|
|
|
|
return acc;
|
|
|
|
})
|
|
|
|
: <String, dynamic> {srcDir : inputPath};
|
|
|
|
|
2021-03-31 09:09:30 +00:00
|
|
|
final outputDir = Directory(outputPath);
|
|
|
|
|
|
|
|
if (!outputDir.existsSync()) {
|
|
|
|
await outputDir.create();
|
|
|
|
}
|
|
|
|
|
2021-03-24 16:52:30 +00:00
|
|
|
extraInfo.forEach((key, dynamic value) async {
|
2021-03-24 17:55:45 +00:00
|
|
|
if (key != srcDir) {
|
|
|
|
print('Wrong key: $key');
|
|
|
|
return;
|
|
|
|
}
|
2021-03-24 16:52:30 +00:00
|
|
|
|
2021-03-24 17:55:45 +00:00
|
|
|
final dirPath = value as String;
|
|
|
|
final dir = Directory(dirPath);
|
2021-03-24 16:52:30 +00:00
|
|
|
|
2021-03-24 17:55:45 +00:00
|
|
|
if (!await dir.exists()) {
|
|
|
|
print('Wrong directory path: $dirPath');
|
|
|
|
return;
|
|
|
|
}
|
2021-03-24 16:52:30 +00:00
|
|
|
|
2021-03-24 17:55:45 +00:00
|
|
|
final localePath = <String, dynamic>{};
|
|
|
|
await dir.list(recursive: false).forEach((element) {
|
|
|
|
try {
|
|
|
|
final shortLocale = element.path.split('_',)[1].split('.')[0];
|
|
|
|
localePath[shortLocale] = element.path;
|
|
|
|
} catch (e) {
|
|
|
|
print('Wrong file: ${element.path}');
|
|
|
|
}
|
|
|
|
});
|
2021-03-24 16:52:30 +00:00
|
|
|
|
2021-03-24 17:55:45 +00:00
|
|
|
if (!localePath.keys.contains(defaultLocale)) {
|
|
|
|
print("Locale list doesn't contain $defaultLocale");
|
|
|
|
return;
|
|
|
|
}
|
2021-03-24 16:52:30 +00:00
|
|
|
|
2021-03-24 17:55:45 +00:00
|
|
|
try {
|
|
|
|
var output = '';
|
|
|
|
var locales = 'const locales = [';
|
2021-03-24 16:52:30 +00:00
|
|
|
|
2021-03-24 17:55:45 +00:00
|
|
|
output += part1;
|
|
|
|
output += textDirectionDeclaration;
|
2021-03-24 16:52:30 +00:00
|
|
|
|
2021-03-24 17:55:45 +00:00
|
|
|
var inputContent =
|
|
|
|
File(localePath[defaultLocale].toString()).readAsStringSync();
|
|
|
|
var config = json.decode(inputContent) as Map<String, dynamic>;
|
2021-03-24 16:52:30 +00:00
|
|
|
|
2021-03-24 17:55:45 +00:00
|
|
|
output += localizedStrings(config: config, hasOverride: false);
|
|
|
|
output += '}' + '\n\n';
|
2021-03-24 16:52:30 +00:00
|
|
|
|
2021-03-24 17:55:45 +00:00
|
|
|
localePath.forEach((key, dynamic value) {
|
|
|
|
inputContent = File(localePath[key].toString()).readAsStringSync();
|
|
|
|
config = json.decode(inputContent) as Map<String, dynamic>;
|
2021-03-24 16:52:30 +00:00
|
|
|
|
2021-03-24 17:55:45 +00:00
|
|
|
locales += "'$key', ";
|
2021-03-24 16:52:30 +00:00
|
|
|
|
2021-03-24 17:55:45 +00:00
|
|
|
output += 'class \$$key extends S {' + '\n';
|
|
|
|
output += ' const \$$key();' + '\n';
|
2021-03-24 16:52:30 +00:00
|
|
|
|
2021-03-24 17:55:45 +00:00
|
|
|
if (key != defaultLocale) {
|
|
|
|
output += textDirectionDeclaration;
|
|
|
|
output += localizedStrings(config: config, hasOverride: true);
|
|
|
|
}
|
2021-03-24 16:52:30 +00:00
|
|
|
|
2021-03-24 17:55:45 +00:00
|
|
|
output += '}' + '\n\n';
|
|
|
|
});
|
2021-03-24 16:52:30 +00:00
|
|
|
|
2021-03-24 17:55:45 +00:00
|
|
|
output += classDeclaration;
|
2021-03-24 16:52:30 +00:00
|
|
|
|
2021-03-24 17:55:45 +00:00
|
|
|
localePath.keys.forEach((key) {
|
|
|
|
output += ' Locale("$key", ""),' + '\n';
|
|
|
|
});
|
2021-03-24 16:52:30 +00:00
|
|
|
|
2021-03-24 17:55:45 +00:00
|
|
|
output += part2;
|
2021-03-24 16:52:30 +00:00
|
|
|
|
2021-03-24 17:55:45 +00:00
|
|
|
localePath.keys.forEach((key) {
|
|
|
|
output += ' case "$key":' + '\n';
|
|
|
|
output += ' S.current = const \$$key();' + '\n';
|
|
|
|
output += ' return SynchronousFuture<S>(S.current);' + '\n';
|
|
|
|
});
|
2021-03-24 16:52:30 +00:00
|
|
|
|
2021-03-24 17:55:45 +00:00
|
|
|
output += part3;
|
|
|
|
|
|
|
|
await File(outputPath + localizationFileName).writeAsString(output);
|
|
|
|
|
|
|
|
locales += '];';
|
|
|
|
|
|
|
|
await File(outputPath + localeListFileName).writeAsString(locales);
|
|
|
|
} catch (e) {
|
|
|
|
print(e.toString());
|
2021-03-19 18:19:14 +00:00
|
|
|
}
|
2021-03-24 16:52:30 +00:00
|
|
|
});
|
2021-03-19 18:19:14 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 17:09:57 +00:00
|
|
|
String localizedStrings({required Map<String, dynamic> config, required bool hasOverride}) {
|
2021-03-19 18:19:14 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|