1
1
using System . Collections . Concurrent ;
2
2
using System . Text . Json ;
3
3
using System . Text . Json . Nodes ;
4
+ using System . Text . RegularExpressions ;
4
5
using System . Web ;
5
6
6
7
namespace I18nResourceTranslator ;
@@ -12,6 +13,7 @@ public class Translator
12
13
private List < Task > editTasks = new ( ) ;
13
14
private IDictionary < string , string > translatorCache = new ConcurrentDictionary < string , string > ( ) ;
14
15
private readonly string cachePath ;
16
+ private static readonly Regex tokensRegex = new Regex ( @"\{[\w\-_\+\d]+\}" ) ;
15
17
16
18
public Translator ( string from , string to )
17
19
{
@@ -26,7 +28,8 @@ public async Task StartEditJsonAndTranslation(JsonNode doc)
26
28
{
27
29
Console . WriteLine ( "加载已翻译的缓存内容..." ) ;
28
30
var cachedJson = await File . ReadAllTextAsync ( cachePath ) ;
29
- translatorCache = JsonSerializer . Deserialize < ConcurrentDictionary < string , string > > ( cachedJson ) ?? translatorCache ;
31
+ translatorCache = JsonSerializer . Deserialize < ConcurrentDictionary < string , string > > ( cachedJson ) ??
32
+ translatorCache ;
30
33
Console . WriteLine ( "已翻译缓存已加载" ) ;
31
34
}
32
35
@@ -40,13 +43,15 @@ public async Task StartEditJsonAndTranslation(JsonNode doc)
40
43
Console . WriteLine ( "重试翻译任务: {0}" , task . Id ) ;
41
44
task . Start ( ) ;
42
45
}
46
+
43
47
Task . WaitAll ( failedTasks . ToArray ( ) ) ;
44
48
}
45
49
catch ( Exception e )
46
50
{
47
51
Console . WriteLine ( "翻译失败" ) ;
48
52
Console . WriteLine ( e ) ;
49
53
}
54
+
50
55
Console . WriteLine ( "翻译已完成" ) ;
51
56
await File . WriteAllTextAsync ( cachePath , JsonSerializer . Serialize ( translatorCache ) ) ;
52
57
Console . WriteLine ( "翻译缓存已保存" ) ;
@@ -66,11 +71,11 @@ private async Task EditJson(JsonNode doc)
66
71
editTasks . Add ( EditJson ( pro . Value ) ) ;
67
72
break ;
68
73
case JsonValue value :
69
- dic . Add ( pro . Key , JsonValue . Create ( await Translate ( value . GetValue < string > ( ) ) ) ! ) ;
74
+ dic . Add ( pro . Key , JsonValue . Create ( await Translate ( value . GetValue < string > ( ) ) ) ! ) ;
70
75
break ;
71
76
}
72
77
}
73
-
78
+
74
79
foreach ( var node in dic )
75
80
{
76
81
json [ node . Key ] = node . Value ;
@@ -83,6 +88,7 @@ private async Task<string> Translate(string toTrans)
83
88
{
84
89
return translatorCache [ toTrans ] ;
85
90
}
91
+
86
92
var httpClient = new HttpClient ( ) ;
87
93
var url =
88
94
$ "http://translate.google.cn/translate_a/single?client=gtx&dt=t&dj=1&ie=UTF-8&sl={ from } &tl={ to } &q={ HttpUtility . UrlEncode ( toTrans ) } ";
@@ -105,7 +111,36 @@ private async Task<string> Translate(string toTrans)
105
111
// }
106
112
// }
107
113
var jsonObj = JsonNode . Parse ( json ) ;
108
- var tran = jsonObj ! [ "sentences" ] ! . AsArray ( ) . Select ( node => node ! [ "trans" ] ! . GetValue < string > ( ) ) . Aggregate ( ( s , s1 ) => s + s1 ) ;
114
+ var tran = jsonObj ! [ "sentences" ] ! . AsArray ( ) . Select ( node => node ! [ "trans" ] ! . GetValue < string > ( ) )
115
+ . Aggregate ( ( s , s1 ) => s + s1 ) ;
116
+ var toTransTokens = tokensRegex . Matches ( toTrans ) ;
117
+ var tranTokens = tokensRegex . Matches ( tran ) ;
118
+ if ( toTransTokens . Count != tranTokens . Count )
119
+ {
120
+ var bgC = Console . BackgroundColor ;
121
+ Console . BackgroundColor = ConsoleColor . Magenta ;
122
+ Console . WriteLine ( "原Token与译文中的Token数目不一致,请比对" ) ;
123
+ Console . WriteLine ( "\t {0} | {1}" , toTransTokens . Count ,
124
+ toTransTokens . Any ( ) ? toTransTokens . Select ( m => m . Value ) . Aggregate ( ( s , s1 ) => s + " " + s1 ) : "-" ) ;
125
+ Console . WriteLine ( "\t {0} | {1}" , tranTokens . Count ,
126
+ tranTokens . Any ( ) ? tranTokens . Select ( m => m . Value ) . Aggregate ( ( s , s1 ) => s + " " + s1 ) : "-" ) ;
127
+ Console . BackgroundColor = bgC ;
128
+ }
129
+ else
130
+ {
131
+ for ( var i = 0 ; i < toTransTokens . Count ; i ++ )
132
+ {
133
+ var t = toTransTokens [ i ] ;
134
+ var t1 = tranTokens [ i ] ;
135
+ if ( t . Value == t1 . Value )
136
+ {
137
+ continue ;
138
+ }
139
+
140
+ tran = tran . Replace ( t1 . Value , t . Value ) ;
141
+ }
142
+ }
143
+
109
144
translatorCache [ toTrans ] = tran ;
110
145
Console . WriteLine ( "已翻译:" ) ;
111
146
Console . WriteLine ( $ "\t { url } ") ;
0 commit comments