语言脚本
This commit is contained in:
@@ -1,46 +1,173 @@
|
||||
// import 'dart:io';
|
||||
|
||||
// void main(List<String> args) async {
|
||||
// final targetPath = args.isNotEmpty ? args[0] : Directory.current.path;
|
||||
// final type = FileSystemEntity.typeSync(targetPath);
|
||||
|
||||
// if (type == FileSystemEntityType.notFound) {
|
||||
// print('路径不存在: $targetPath');
|
||||
// return;
|
||||
// }
|
||||
|
||||
// if (type == FileSystemEntityType.file && targetPath.endsWith('.dart')) {
|
||||
// await processDartFile(File(targetPath));
|
||||
// } else if (type == FileSystemEntityType.directory) {
|
||||
// final directory = Directory(targetPath);
|
||||
// print('扫描目录: $targetPath');
|
||||
|
||||
// await for (final entity
|
||||
// in directory.list(recursive: true, followLinks: false)) {
|
||||
// if (entity is File && entity.path.endsWith('.dart')) {
|
||||
// await processDartFile(entity);
|
||||
// }
|
||||
// }
|
||||
// print('处理完成');
|
||||
// } else {
|
||||
// print('不支持的路径类型: $targetPath');
|
||||
// }
|
||||
// }
|
||||
|
||||
// Future<void> processDartFile(File file) async {
|
||||
// final content = await file.readAsString();
|
||||
|
||||
// // 获取注释范围
|
||||
// final commentRanges = getCommentRanges(content);
|
||||
|
||||
// // 匹配带中文的引号字符串(单/双引号)
|
||||
// final regex = RegExp(r'''(['"])([^'"]*?[\u4e00-\u9fa5][^'"]*?)\1''');
|
||||
|
||||
// final matches = regex.allMatches(content).toList();
|
||||
// if (matches.isEmpty) return;
|
||||
|
||||
// final sb = StringBuffer();
|
||||
// int lastIndex = 0;
|
||||
|
||||
// for (final match in matches) {
|
||||
// final start = match.start;
|
||||
// final end = match.end;
|
||||
|
||||
// // 跳过注释中的内容
|
||||
// if (commentRanges.any((range) => start >= range[0] && start < range[1])) {
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// // 插入上次结束到当前匹配之间的代码
|
||||
// sb.write(content.substring(lastIndex, start));
|
||||
|
||||
// final fullMatch = match.group(0)!;
|
||||
|
||||
// // 判断是否已处于 .tr 表达式中
|
||||
// if (isAlreadyTrCall(content, start, end)) {
|
||||
// sb.write(fullMatch); // 不修改
|
||||
// } else {
|
||||
// sb.write('$fullMatch.tr'); // 添加 .tr
|
||||
// }
|
||||
|
||||
// lastIndex = end;
|
||||
// }
|
||||
|
||||
// sb.write(content.substring(lastIndex));
|
||||
|
||||
// final result = sb.toString();
|
||||
// if (result != content) {
|
||||
// await file.writeAsString(result);
|
||||
// print('✅ 修改完成: ${file.path}');
|
||||
// } else {
|
||||
// print('✔ 无需修改: ${file.path}');
|
||||
// }
|
||||
// }
|
||||
|
||||
// /// 判断该字符串是否已是 .tr 调用的一部分
|
||||
// bool isAlreadyTrCall(String content, int start, int end) {
|
||||
// final after = content.substring(end);
|
||||
// final before = content.substring(0, start);
|
||||
|
||||
// // 检查匹配后是否直接跟 .tr (允许空格)
|
||||
// final hasTrAfter = RegExp(r'^\s*\.tr\b').hasMatch(after);
|
||||
|
||||
// // 检查匹配前是否已经出现 .tr (形如 .tr'xxx' 也考虑在内)
|
||||
// final hasTrBefore = RegExp(r'\.tr\s*$').hasMatch(before);
|
||||
|
||||
// return hasTrAfter || hasTrBefore;
|
||||
// }
|
||||
|
||||
// /// 找出所有注释区间 (start, end)
|
||||
// List<List<int>> getCommentRanges(String content) {
|
||||
// final ranges = <List<int>>[];
|
||||
|
||||
// final multiLine = RegExp(r'/\*[\s\S]*?\*/');
|
||||
// final singleLine = RegExp(r'//.*');
|
||||
|
||||
// for (final m in multiLine.allMatches(content)) {
|
||||
// ranges.add([m.start, m.end]);
|
||||
// }
|
||||
// for (final m in singleLine.allMatches(content)) {
|
||||
// ranges.add([m.start, m.end]);
|
||||
// }
|
||||
// return ranges;
|
||||
// }
|
||||
import 'dart:io';
|
||||
import 'dart:convert';
|
||||
|
||||
void main(List<String> args) async {
|
||||
final targetPath = args.isNotEmpty ? args[0] : Directory.current.path;
|
||||
if (args.length < 2) {
|
||||
print('Usage: dart script.dart <target_path> <json_file_path>');
|
||||
return;
|
||||
}
|
||||
|
||||
final targetPath = args[0];
|
||||
final jsonFilePath = args[1];
|
||||
|
||||
// 加载JSON文件
|
||||
final jsonFile = File(jsonFilePath);
|
||||
if (!jsonFile.existsSync()) {
|
||||
print('JSON file not found: $jsonFilePath');
|
||||
return;
|
||||
}
|
||||
|
||||
final jsonContent = await jsonFile.readAsString();
|
||||
final jsonMap = json.decode(jsonContent) as Map<String, dynamic>;
|
||||
final targetKeys = jsonMap.keys.toSet();
|
||||
|
||||
final type = FileSystemEntity.typeSync(targetPath);
|
||||
|
||||
if (type == FileSystemEntityType.notFound) {
|
||||
print('路径不存在: $targetPath');
|
||||
print('Path not found: $targetPath');
|
||||
return;
|
||||
}
|
||||
|
||||
if (type == FileSystemEntityType.file && targetPath.endsWith('.dart')) {
|
||||
await processDartFile(File(targetPath));
|
||||
await processDartFile(File(targetPath), targetKeys);
|
||||
} else if (type == FileSystemEntityType.directory) {
|
||||
final directory = Directory(targetPath);
|
||||
print('扫描目录: $targetPath');
|
||||
print('Scanning directory: $targetPath');
|
||||
|
||||
await for (final entity
|
||||
in directory.list(recursive: true, followLinks: false)) {
|
||||
if (entity is File && entity.path.endsWith('.dart')) {
|
||||
await processDartFile(entity);
|
||||
await processDartFile(entity, targetKeys);
|
||||
}
|
||||
}
|
||||
print('处理完成');
|
||||
print('Processing completed');
|
||||
} else {
|
||||
print('不支持的路径类型: $targetPath');
|
||||
print('Unsupported path type: $targetPath');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> processDartFile(File file) async {
|
||||
Future<void> processDartFile(File file, Set<String> targetKeys) async {
|
||||
final content = await file.readAsString();
|
||||
|
||||
// 获取注释范围
|
||||
final commentRanges = getCommentRanges(content);
|
||||
|
||||
// 匹配带中文的引号字符串(单/双引号)
|
||||
final regex = RegExp(r'''(['"])([^'"]*?[\u4e00-\u9fa5][^'"]*?)\1''');
|
||||
|
||||
final matches = regex.allMatches(content).toList();
|
||||
if (matches.isEmpty) return;
|
||||
if (matches.isEmpty) {
|
||||
print('✓ No matches in: ${file.path}');
|
||||
return;
|
||||
}
|
||||
|
||||
final sb = StringBuffer();
|
||||
int lastIndex = 0;
|
||||
bool hasChanges = false;
|
||||
|
||||
for (final match in matches) {
|
||||
final start = match.start;
|
||||
@@ -48,53 +175,50 @@ Future<void> processDartFile(File file) async {
|
||||
|
||||
// 跳过注释中的内容
|
||||
if (commentRanges.any((range) => start >= range[0] && start < range[1])) {
|
||||
sb.write(content.substring(lastIndex, end));
|
||||
lastIndex = end;
|
||||
continue;
|
||||
}
|
||||
|
||||
// 插入上次结束到当前匹配之间的代码
|
||||
sb.write(content.substring(lastIndex, start));
|
||||
|
||||
final fullMatch = match.group(0)!;
|
||||
final textContent = match.group(2)!;
|
||||
|
||||
// 判断是否已处于 .tr 表达式中
|
||||
if (isAlreadyTrCall(content, start, end)) {
|
||||
sb.write(fullMatch); // 不修改
|
||||
// 检查是否在目标keys中
|
||||
if (targetKeys.contains(textContent)) {
|
||||
// 检查是否已经包含.tr
|
||||
if (!isAlreadyTrCall(content, start, end)) {
|
||||
sb.write(content.substring(lastIndex, start));
|
||||
sb.write('$fullMatch.tr');
|
||||
hasChanges = true;
|
||||
lastIndex = end;
|
||||
} else {
|
||||
sb.write(content.substring(lastIndex, end));
|
||||
lastIndex = end;
|
||||
}
|
||||
} else {
|
||||
sb.write('$fullMatch.tr'); // 添加 .tr
|
||||
sb.write(content.substring(lastIndex, end));
|
||||
lastIndex = end;
|
||||
}
|
||||
|
||||
lastIndex = end;
|
||||
}
|
||||
|
||||
sb.write(content.substring(lastIndex));
|
||||
|
||||
final result = sb.toString();
|
||||
if (result != content) {
|
||||
await file.writeAsString(result);
|
||||
print('✅ 修改完成: ${file.path}');
|
||||
if (hasChanges) {
|
||||
await file.writeAsString(sb.toString());
|
||||
print('✓ Modified: ${file.path}');
|
||||
} else {
|
||||
print('✔ 无需修改: ${file.path}');
|
||||
print('✓ No changes needed: ${file.path}');
|
||||
}
|
||||
}
|
||||
|
||||
/// 判断该字符串是否已是 .tr 调用的一部分
|
||||
bool isAlreadyTrCall(String content, int start, int end) {
|
||||
final after = content.substring(end);
|
||||
final before = content.substring(0, start);
|
||||
|
||||
// 检查匹配后是否直接跟 .tr (允许空格)
|
||||
final hasTrAfter = RegExp(r'^\s*\.tr\b').hasMatch(after);
|
||||
|
||||
// 检查匹配前是否已经出现 .tr (形如 .tr'xxx' 也考虑在内)
|
||||
final hasTrBefore = RegExp(r'\.tr\s*$').hasMatch(before);
|
||||
|
||||
return hasTrAfter || hasTrBefore;
|
||||
// 检查字符串后面是否紧跟.tr
|
||||
final afterString = content.substring(end);
|
||||
return RegExp(r'^\s*\.tr\b').hasMatch(afterString);
|
||||
}
|
||||
|
||||
/// 找出所有注释区间 (start, end)
|
||||
List<List<int>> getCommentRanges(String content) {
|
||||
final ranges = <List<int>>[];
|
||||
|
||||
final multiLine = RegExp(r'/\*[\s\S]*?\*/');
|
||||
final singleLine = RegExp(r'//.*');
|
||||
|
||||
|
||||
@@ -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