语言脚本
This commit is contained in:
46
tool/extract_chinese.dart
Normal file
46
tool/extract_chinese.dart
Normal file
@@ -0,0 +1,46 @@
|
||||
import 'dart:io';
|
||||
import 'dart:convert';
|
||||
|
||||
void main(List<String> args) async {
|
||||
final dirPath = args.isNotEmpty ? args[0] : Directory.current.path;
|
||||
|
||||
print('扫描目录: $dirPath');
|
||||
|
||||
final directory = Directory(dirPath);
|
||||
if (!directory.existsSync()) {
|
||||
print('目录不存在');
|
||||
return;
|
||||
}
|
||||
|
||||
final dartFiles = <File>[];
|
||||
await for (var entity
|
||||
in directory.list(recursive: true, followLinks: false)) {
|
||||
if (entity is File && entity.path.endsWith('.dart')) {
|
||||
dartFiles.add(entity);
|
||||
}
|
||||
}
|
||||
|
||||
final chineseReg = RegExp(r'[\u4e00-\u9fa5]+');
|
||||
final chineseSet = <String>{};
|
||||
|
||||
for (var file in dartFiles) {
|
||||
final content = await file.readAsString();
|
||||
final uncommented = removeComments(content);
|
||||
final matches = chineseReg.allMatches(uncommented);
|
||||
chineseSet.addAll(matches.map((m) => m.group(0)!));
|
||||
}
|
||||
|
||||
final resultMap = {for (var text in chineseSet) text: text};
|
||||
final jsonString = const JsonEncoder.withIndent(' ').convert(resultMap);
|
||||
|
||||
final outputFile = File('chinese_texts.json');
|
||||
await outputFile.writeAsString(jsonString);
|
||||
|
||||
print('中文文本已保存到: ${outputFile.path}');
|
||||
}
|
||||
|
||||
String removeComments(String source) {
|
||||
source = source.replaceAll(RegExp(r'\/\*[\s\S]*?\*\/'), '');
|
||||
source = source.replaceAll(RegExp(r'\/\/.*'), '');
|
||||
return source;
|
||||
}
|
||||
Reference in New Issue
Block a user