-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmarktext_asar_translate.ts
224 lines (195 loc) · 6.92 KB
/
marktext_asar_translate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import * as fs from "fs"
import * as path from "path"
function readStringArrayFromFile(fileName:string) : string[] {
let ret_lines:string[] = [];
if (fs.existsSync(fileName)) {
let buffer = fs.readFileSync(fileName, "utf-8")
let lines = (buffer as string).split(/[\r\n]/)
for (let i = 0; i < lines.length; i++) {
if (lines[i].trim().length > 0) {
ret_lines.push(lines[i])
}
}
}
return ret_lines;
}
function writeStringArrayToFile( fileName:string, dict:string[]) : void {
fs.writeFileSync( fileName, dict.join('\n')+'\n' )
}
function searchLabelsFromFile( fileName:string, exclude: void|string[]) : string[] {
let labels:string[] = []
if (fs.existsSync(fileName)) {
let buffer = fs.readFileSync(fileName, "utf-8")
let pat = /(label):"(.*?)"/gms
if (Array.isArray(exclude) && exclude.length > 0) {
buffer.replace(pat, (match, key, value) => {
if (exclude.indexOf(value) == -1) {
if (labels.indexOf(value) == -1){
labels.push(value)
}
}
return match
})
} else {
buffer.replace(pat, (match, key, value) => {
if (labels.indexOf(value) == -1){
labels.push(value)
}
return match
})
}
}
return labels;
}
function replaceLabelsFromFile( fileName:string, toDict: string[]) : string {
let content:string = ""
let toKeys:string[] = []
let toVals:string[] = []
for (let i = 0; i < toDict.length; i++) {
if (toDict[i].trim().length > 0) {
const tow_cell = toDict[i].split('|')
if (tow_cell.length == 2 ){
toKeys.push(tow_cell[0])
toVals.push(tow_cell[1])
}
}
}
if (fs.existsSync(fileName)) {
let buffer = fs.readFileSync(fileName, "utf-8")
let pat = /(label):"(.*?)"/gms
content = buffer.replace(pat, (match, key, value) => {
const idx = toKeys.indexOf(value)
if (idx != -1) {
return key + ':"' + toVals[idx] + '"'
}
return match
})
}
return content
}
function replaceMainLabelDict(fileName:string, langRootPath:string, toDict:string[], toLang:string, outFileName:string) : boolean {
if (toDict.length == 0){
return false
}
const main_content = replaceLabelsFromFile( fileName, toDict);
if (main_content.length>0) {
fs.writeFileSync( outFileName, main_content )
return true
}else{
console.log("error in replaceMainLabelDict, replaceLabelsFromFile failed.")
}
return false
}
function replaceMain(fileName:string, langRootPath:string, toLang:string, outFileName:string): boolean {
const toDictFileName = path.join(langRootPath, "translate-resources/main_dict_" + toLang + ".txt")
const toDict = readStringArrayFromFile(toDictFileName)
if (toDict.length == 0){
console.log("error in replaceMain, readStringArrayFromFile failed, not found "+ toDictFileName +".")
return false
}
if (!fs.existsSync(fileName)) {
console.log("error in replaceMain, not found "+ fileName +".")
return false
}
let content = fs.readFileSync(fileName, "utf-8")
for (let i = 0; i < toDict.length; i++) {
if (toDict[i].trim().length > 0) {
const tow_cell = toDict[i].split('|')
if (tow_cell.length == 2 ){
content = content.replace(tow_cell[0], tow_cell[1])
}
}
}
if (content.length>0) {
fs.writeFileSync( outFileName, content )
return true
}else{
console.log("error in replaceMain, readFileSync or replace failed.")
}
return false
}
function replaceRenderer(fileName:string, langRootPath:string, toLang:string, outFileName:string): boolean {
const toDictFileName = path.join(langRootPath, "translate-resources/renderer_dict_" + toLang + ".txt")
const toDict = readStringArrayFromFile(toDictFileName)
if (toDict.length == 0){
console.log("error in replaceRenderer, readStringArrayFromFile failed, not found "+ toDictFileName +".")
return false
}
if (!fs.existsSync(fileName)) {
console.log("error in replaceRenderer, not found "+ fileName +".")
return false
}
let content = fs.readFileSync(fileName, "utf-8")
for (let i = 0; i < toDict.length; i++) {
if (toDict[i].trim().length > 0) {
const tow_cell = toDict[i].split('|')
if (tow_cell.length == 2 ){
content = content.replace(tow_cell[0], tow_cell[1])
}
}
}
if (content.length>0) {
fs.writeFileSync( outFileName, content )
return true
}else{
console.log("error in replaceRenderer, readFileSync or replace failed.")
}
return false
}
function initMainLabelDict(fileName:string, langRootPath:string, toLang:string) : string[] {
const toDictFileName = path.join(langRootPath, "translate-resources/main_label_dict_" + toLang + ".txt")
let toDict = readStringArrayFromFile(toDictFileName)
let toKeys:string[] = []
for (let i = 0; i < toDict.length; i++) {
if (toDict[i].trim().length > 0) {
const tow_cell = toDict[i].split('|')
if (tow_cell.length == 2 ){
toKeys.push(tow_cell[0])
}
}
}
const excludeDict = readStringArrayFromFile( path.join(langRootPath, "translate-resources/exclude.txt"))
let enDict = searchLabelsFromFile(fileName, excludeDict)
if (enDict.length>0){
let nAdd = 0
for (let i=0; i<enDict.length; i++){
if (toKeys.indexOf(enDict[i]) == -1){
toDict.push(enDict[i]+'|'+enDict[i])
toKeys.push(enDict[i])
nAdd++
}
}
if (nAdd!=0){
//UI Label change, need to rewrite.
writeStringArrayToFile(toDictFileName, toDict)
}
}
if (toDict.length==0){
console.log("warring in initMainLabelDict, toDict is Empty.")
}
return toDict
}
export function markTextAsarTranslate(langRootPath:string, toLang:string, mainJsFileName:string, outMainJsFileName:string, rendererJsFileName:string, outRendererJsFileName:string): boolean {
const toDict = initMainLabelDict(mainJsFileName, langRootPath, toLang)
if (toDict.length>0){
if (replaceMainLabelDict(mainJsFileName, langRootPath, toDict, toLang, outMainJsFileName)){
console.log("replaceMainLabelDict success.")
}else{
console.log("replaceMainLabelDict failed.")
return false
}
}
if (replaceMain(mainJsFileName, langRootPath, toLang, outMainJsFileName)){
console.log("replaceMain success.")
}else{
console.log("replaceMain failed.")
return false
}
if (replaceRenderer(rendererJsFileName, langRootPath, toLang, outRendererJsFileName)){
console.log("replaceRenderer success.")
}else{
console.log("replaceRenderer failed.")
return false
}
return true
}