l10: update common & Lang settings

This commit is contained in:
2024-03-17 16:54:09 +08:00
parent 9d97dfc786
commit 17588dfab8
31 changed files with 1203 additions and 500 deletions

View File

@ -10,9 +10,19 @@ import 'package:uuid/v4.dart';
final stringResult = <String>[];
class AutoL10nTools {
final skipPaths = ["lib/generated"];
void genL10nFiles() {
final dir = Directory('lib/ui');
final dir = Directory('lib/');
for (var entity in dir.listSync(recursive: true)) {
bool shouldSkip = false;
for (var path in skipPaths) {
if (entity.path.replaceAll("\\", "/").startsWith(path)) {
shouldSkip = true;
}
break;
}
if (shouldSkip) continue;
if (entity is File && entity.path.endsWith('.dart')) {
print('Processing ${entity.path}...');
_processDartFile(entity);
@ -24,14 +34,22 @@ class AutoL10nTools {
if (outputMap.containsValue(value)) {
continue;
}
final key = UuidV4().generate();
var key = UuidV4().generate();
final l10nFile = File("./lib/l10n/intl_zh_CN.arb");
final cnJsonMap = json.decode(l10nFile.readAsStringSync()) as Map;
for (var element in cnJsonMap.entries) {
if (element.value == value) {
key = element.key;
}
}
outputMap[key] = value;
}
// output to json
final j = json.encode(outputMap);
File("./lib/generated/l10n_temp.json").writeAsStringSync(j);
print(
"output to json file (length: ${outputMap.length}): ./lib/generated/l10n_temp.json");
"output to json file (length: ${outputMap
.length}): ./lib/generated/l10n_temp.json");
}
}
@ -40,19 +58,28 @@ class AutoL10nTools {
// readToJsonMap
final jsonMap = json.decode(l10nFile.readAsStringSync());
// read all dart File
final dir = Directory('lib/ui');
final dir = Directory('lib/');
for (var entity in dir.listSync(recursive: true)) {
bool shouldSkip = false;
for (var path in skipPaths) {
if (entity.path.replaceAll("\\", "/").startsWith(path)) {
shouldSkip = true;
}
break;
}
if (shouldSkip) continue;
if (entity is File &&
entity.path.endsWith('.dart') &&
!(entity.path.endsWith(".g.dart") &&
entity.path.endsWith(".freezed.dart"))) {
print('Processing ${entity.path}...');
// sort map with value length
final newMap = Map.fromEntries(
final newMap = Map<String, dynamic>.fromEntries(
jsonMap.entries.toList()
..sort((a, b) => (b.value as String)
.length
.compareTo((a.value as String).length)),
..sort((a, b) =>
(b.value.toString())
.length
.compareTo((a.value.toString()).length)),
);
_replaceDartFile(entity, newMap);
}
@ -64,12 +91,12 @@ class AutoL10nTools {
final toFile = File("./lib/l10n/intl_$to.arb");
final translator = GoogleTranslator();
final formMap =
json.decode(formFile.readAsStringSync()) as Map<String, dynamic>;
json.decode(formFile.readAsStringSync()) as Map<String, dynamic>;
final toMap =
json.decode(toFile.readAsStringSync()) as Map<String, dynamic>;
json.decode(toFile.readAsStringSync()) as Map<String, dynamic>;
final formLocaleCode = formMap["@auto_translate_locale"] as String;
final toLocaleCode = toMap["@auto_translate_locale"] as String;
final formLocaleCode = formMap["@auto_translate_locale"].toString();
final toLocaleCode = toMap["@auto_translate_locale"].toString();
print("formLocaleCode: $formLocaleCode, toLocaleCode: $toLocaleCode");
final newMap = <String, dynamic>{};
@ -114,7 +141,7 @@ class AutoL10nTools {
void _replaceDartFile(File entity, jsonMap) {
for (var key in jsonMap.keys) {
if (key == "@@locale") continue;
final mapValue = jsonMap[key] as String;
final mapValue = jsonMap[key].toString();
if (mapValue.contains("{") && mapValue.contains("}")) {
print("skipping args value === $mapValue");
continue;