Files
flutter_init_template/tool/gen_arb.dart
2026-03-10 13:36:40 +08:00

43 lines
1.1 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:convert';
import 'dart:io';
import "package:yaml/yaml.dart";
void main() {
//输如文件,和输出目录
var inputFile = File("lib/l10n/arb/i18n.yaml");
var outDir = Directory("lib/l10n/arb");
if (!inputFile.existsSync()) {
print('❌ i18n.yaml 文件不存在:$inputFile');
exit(1);
}
//获取数据转为map
final yamlMap = loadYaml(inputFile.readAsStringSync());
Map<String, Map<String, dynamic>> arbMaps = {};
yamlMap.forEach((key, mapValue) {
final description = mapValue['description'] ?? '';
//循环语言
mapValue.forEach((lang, text) {
//跳过description
if (lang == 'description') return;
if (!arbMaps.containsKey(lang)) {
arbMaps[lang] = {};
}
arbMaps[lang]?[key] = text;
if (description != "") {
arbMaps[lang]?['@$key'] = {'description': description};
}
});
});
//创建文件
arbMaps.forEach((lang,json){
final file = File("${outDir.path}/app_$lang.arb");
final encoder = JsonEncoder.withIndent(' ');
file.writeAsStringSync(encoder.convert(json));
});
}