语言脚本
This commit is contained in:
@@ -13,21 +13,37 @@ void main(List<String> args) async {
|
||||
}
|
||||
|
||||
final dartFiles = <File>[];
|
||||
await for (var entity
|
||||
in directory.list(recursive: true, followLinks: false)) {
|
||||
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 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 = chineseReg.allMatches(uncommented);
|
||||
chineseSet.addAll(matches.map((m) => m.group(0)!));
|
||||
|
||||
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};
|
||||
@@ -39,8 +55,11 @@ void main(List<String> args) async {
|
||||
print('中文文本已保存到: ${outputFile.path}');
|
||||
}
|
||||
|
||||
// 移除注释内容
|
||||
String removeComments(String source) {
|
||||
source = source.replaceAll(RegExp(r'\/\*[\s\S]*?\*\/'), '');
|
||||
source = source.replaceAll(RegExp(r'\/\/.*'), '');
|
||||
// 移除多行注释 /* ... */
|
||||
source = source.replaceAll(RegExp(r'/\*[\s\S]*?\*/'), '');
|
||||
// 移除单行注释 //
|
||||
source = source.replaceAll(RegExp(r'//.*'), '');
|
||||
return source;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user