65 lines
1.9 KiB
Dart
65 lines
1.9 KiB
Dart
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 stringWithChineseReg = RegExp(r'''(['"])(?:(?!\1).)*[\u4e00-\u9fa5]+(?:(?!\1).)*\1''');
|
|
final chineseSet = <String>{};
|
|
|
|
for (var file in dartFiles) {
|
|
final content = await file.readAsString();
|
|
final uncommented = removeComments(content);
|
|
|
|
final matches = stringWithChineseReg.allMatches(uncommented);
|
|
for (var match in matches) {
|
|
final fullString = match.group(0)!;
|
|
final pure = fullString.substring(1, fullString.length - 1).trim(); // 去掉引号
|
|
|
|
// 跳过包含$的字符串(模板字符串)
|
|
if (pure.contains('\$')) continue;
|
|
|
|
// 处理带点的字符串
|
|
var processedText = pure;
|
|
if (pure.contains('.')) {
|
|
processedText = pure.substring(pure.lastIndexOf('.') + 1);
|
|
}
|
|
|
|
if (processedText.isNotEmpty) {
|
|
chineseSet.add(processedText);
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
} |