68 lines
1.8 KiB
Dart
68 lines
1.8 KiB
Dart
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);
|
|
}
|
|
|
|
// 读取并解析 yaml
|
|
final yamlMap = loadYaml(inputFile.readAsStringSync());
|
|
|
|
Map<String, Map<String, dynamic>> arbMaps = {};
|
|
|
|
// 递归处理嵌套的 key
|
|
void processYaml(Map yaml, {String? prefix}) {
|
|
yaml.forEach((key, value) {
|
|
if (value is Map && value.values.any((v) => v is Map)) {
|
|
// 说明还有子层级,递归处理
|
|
processYaml(value, prefix: prefix == null ? key : "${prefix}_$key");
|
|
} else if (value is Map) {
|
|
// 叶子节点:包含 zh/en/description
|
|
final description = value['description'] ?? '';
|
|
|
|
value.forEach((lang, text) {
|
|
if (lang == 'description') return;
|
|
|
|
final arbKey = prefix == null ? key : "${prefix}_$key";
|
|
|
|
arbMaps.putIfAbsent(lang, () => {});
|
|
arbMaps[lang]![arbKey] = text;
|
|
|
|
if (description.toString().isNotEmpty) {
|
|
arbMaps[lang]!['@$arbKey'] = {'description': description};
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
processYaml(yamlMap);
|
|
|
|
// 创建 arb 文件
|
|
arbMaps.forEach((lang, json) {
|
|
final file = File("${outDir.path}/app_$lang.arb");
|
|
final encoder = JsonEncoder.withIndent(' ');
|
|
file.writeAsStringSync(encoder.convert(json));
|
|
print('✅ 生成 ${file.path}');
|
|
});
|
|
|
|
// 执行 flutter gen-l10n
|
|
final result = Process.runSync(
|
|
"flutter",
|
|
["gen-l10n"],
|
|
runInShell: true, // 确保跨平台可用
|
|
);
|
|
if (result.exitCode == 0) {
|
|
print('✅ flutter gen-l10n 成功');
|
|
} else {
|
|
print('❌ flutter gen-l10n 失败:${result.stderr}');
|
|
}
|
|
}
|