l10: update

This commit is contained in:
2024-03-17 12:44:50 +08:00
parent d382484739
commit ead05516aa
13 changed files with 1606 additions and 1740 deletions

View File

@ -4,6 +4,7 @@ import 'package:analyzer/dart/analysis/utilities.dart';
import 'package:analyzer/dart/analysis/features.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:translator/translator.dart';
import 'package:uuid/v4.dart';
final stringResult = <String>[];
@ -49,13 +50,60 @@ class AutoL10nTools {
// sort map with value length
final newMap = Map.fromEntries(
jsonMap.entries.toList()
..sort((a, b) => (b.value as String).length.compareTo((a.value as String).length)),
..sort((a, b) => (b.value as String)
.length
.compareTo((a.value as String).length)),
);
_replaceDartFile(entity, newMap);
}
}
}
Future<void> autoTranslate({required String form, required String to}) async {
final formFile = File("./lib/l10n/intl_$form.arb");
final toFile = File("./lib/l10n/intl_$to.arb");
final translator = GoogleTranslator();
final formMap =
json.decode(formFile.readAsStringSync()) as Map<String, dynamic>;
final toMap =
json.decode(toFile.readAsStringSync()) as Map<String, dynamic>;
final formLocaleCode = formMap["@auto_translate_locale"] as String;
final toLocaleCode = toMap["@auto_translate_locale"] as String;
print("formLocaleCode: $formLocaleCode, toLocaleCode: $toLocaleCode");
final newMap = <String, dynamic>{};
try {
for (var key in formMap.keys) {
if (toMap.keys.contains(key)) {
newMap[key] = toMap[key];
continue;
}
if (key.toString().startsWith("@") || key == "locale_code") {
continue;
}
final value = formMap[key] as String;
final result = await translator.translate(value,
from: formLocaleCode, to: toLocaleCode);
var resultValue = result.text;
// 如果目标语言是英文,则首字母大写
if (toLocaleCode == "en") {
resultValue = resultValue[0].toUpperCase() + resultValue.substring(1);
}
print("translate $key: $value -> $resultValue");
newMap[key] = resultValue;
await Future.delayed(Duration(milliseconds: 500));
}
} catch (e) {
print(e);
toMap.addAll(newMap);
toFile.writeAsStringSync(json.encode(toMap));
}
toMap.addAll(newMap);
toFile.writeAsStringSync(json.encode(toMap));
}
void _processDartFile(File file) {
final parseResult = parseFile(
path: file.path, featureSet: FeatureSet.latestLanguageVersion());
@ -67,7 +115,7 @@ class AutoL10nTools {
for (var key in jsonMap.keys) {
if (key == "@@locale") continue;
final mapValue = jsonMap[key] as String;
if (mapValue.contains("{{") && mapValue.contains("}}")) {
if (mapValue.contains("{") && mapValue.contains("}")) {
print("skipping args value === $mapValue");
continue;
}

View File

@ -1,11 +1,18 @@
import 'auto_l10n.dart';
void main(List<String> args) {
void main(List<String> args) async {
switch (args.elementAtOrNull(0)) {
case "l10n_gen":
return AutoL10nTools().genL10nFiles();
case "l10n_replace":
return AutoL10nTools().replaceL10nFiles();
case "l10n_auto_translate":
final form = args.elementAtOrNull(1);
final to = args.elementAtOrNull(2);
if (form == null || to == null) {
throw Exception("form or to is null");
}
return await AutoL10nTools().autoTranslate(form: form, to: to);
default:
throw Exception("cmd not found");
}

View File

@ -11,6 +11,7 @@ dependencies:
# path: ^1.8.0
analyzer: ^6.4.1
uuid: ^4.3.3
translator: ^1.0.0
dev_dependencies:
lints: ^3.0.0