@@ -26,6 +26,18 @@ function traverseDirectory(dir) {
26
26
} ) ;
27
27
}
28
28
29
+ // 计算翻译估计用时
30
+ function calculateTranslationTime ( content ) {
31
+ const wordCount = content . split ( / \s + / ) . length ;
32
+ const hours = Math . ceil ( wordCount / 500 ) ; // 假设平均翻译速度为每小时500词
33
+
34
+ if ( hours <= 1 ) return '⭐️' ;
35
+ if ( hours <= 2 ) return '⭐️⭐️' ;
36
+ if ( hours <= 4 ) return '⭐️⭐️⭐️' ;
37
+ if ( hours <= 6 ) return '⭐️⭐️⭐️⭐️' ;
38
+ return '⭐️⭐️⭐️⭐️⭐️' ;
39
+ }
40
+
29
41
// 处理 Markdown 文件
30
42
function processMarkdownFile ( filePath ) {
31
43
const relativePath = path . relative ( rootDir , filePath ) ;
@@ -47,23 +59,28 @@ function processMarkdownFile(filePath) {
47
59
}
48
60
49
61
if ( matchingUrl ) {
50
- const content = fs . readFileSync ( filePath , 'utf8' ) ;
51
-
52
- // 检查文件是否已经包含了注释
53
- if ( content . includes ( '要翻译的文件:' ) && content . includes ( 'Swift 文档源文件地址:' ) ) {
54
- console . log ( `Already updated: ${ filePath } ` ) ;
55
- return ;
56
- }
62
+ let content = fs . readFileSync ( filePath , 'utf8' ) ;
63
+ const translationTime = calculateTranslationTime ( content ) ;
57
64
58
- const newContent = `<!--
65
+ const newComment = `<!--
59
66
要翻译的文件:${ githubBaseUrl } /${ relativePath . replace ( / \\ / g, '/' ) }
60
67
Swift 文档源文件地址:${ matchingUrl }
61
- -->
68
+ 翻译估计用时:${ translationTime }
69
+ -->` ;
62
70
63
- ${ content } `;
71
+ // 检查文件是否已经包含了注释
72
+ const commentRegex = / < ! - - [ \s \S ] * ?- - > / ;
73
+ if ( commentRegex . test ( content ) ) {
74
+ // 更新已存在的注释
75
+ content = content . replace ( commentRegex , newComment ) ;
76
+ console . log ( `Updated existing comment: ${ filePath } ` ) ;
77
+ } else {
78
+ // 添加新的注释
79
+ content = `${ newComment } \n\n${ content } ` ;
80
+ console . log ( `Added new comment: ${ filePath } ` ) ;
81
+ }
64
82
65
- fs . writeFileSync ( filePath , newContent ) ;
66
- console . log ( `Updated: ${ filePath } ` ) ;
83
+ fs . writeFileSync ( filePath , content ) ;
67
84
} else {
68
85
console . log ( `No matching URL found for: ${ filePath } ` ) ;
69
86
}
0 commit comments