This repository was archived by the owner on Jan 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathtranslate.ts
More file actions
360 lines (321 loc) · 14.1 KB
/
translate.ts
File metadata and controls
360 lines (321 loc) · 14.1 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
* @module @microsoft/bf-lg-cli
*/
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import {Command, flags, CLIError} from '@microsoft/bf-cli-command'
import {Helper, TranslateLine, PARSERCONSTS, Block, TranslateOption, TranslateParts} from '../../utils'
import * as txtfile from 'read-text-file'
import * as path from 'path'
import * as fs from 'fs-extra'
import * as os from 'os'
export default class TranslateCommand extends Command {
static description = 'Machine translate .lg files using Microsoft Translator Text API.'
private readonly MAX_TRANSLATE_BATCH_SIZE = 25
private readonly MAX_CHAR_IN_REQUEST = 4990
private readonly NEWLINE = os.EOL
private readonly DEFAULT_SOURCE_LANG = 'en'
private readonly ImportRegex = /^\[.+\]\(.+\)$/g
private readonly ConditionRegex = /^(if|(else\s*if)|else|switch|case|default)\s*:.*/gi
private readonly ExpressionRegex = /(?<!\\)\$\{(('[^'\r\n]*')|("[^"\r\n]*")|(`(\\`|[^`])*`)|([^\r\n{}'"`]))+\}?/g
static flags: flags.Input<any> = {
in: flags.string({char: 'i', description: 'Folder that contains .lg file.', required: true}),
tgtlang: flags.string({description: 'Comma separated list of target languages.', required: true}),
translatekey: flags.string({description: 'Machine translation endpoint key.', required: true}),
recurse: flags.boolean({char: 'r', description: 'Consider sub-folders to find .lg file(s)'}),
out: flags.string({char: 'o', description: 'Output file or folder name. If not specified stdout will be used as output'}),
force: flags.boolean({char: 'f', description: 'If --out flag is provided with the path to an existing file, overwrites that file'}),
srclang: flags.string({description: 'Source lang code. Auto detect if missing.'}),
translate_comments: flags.boolean({description: 'Machine translate all comments found in .lg file'}),
translate_link_text: flags.boolean({description: 'Machine translate link description in .lg file'}),
region: flags.string({description: 'The sub region.', required: true}),
help: flags.help({char: 'h', description: 'lg:translate help'}),
}
async run() {
const {flags} = this.parse(TranslateCommand)
const lgFilePaths = Helper.findLGFiles(flags.in, flags.recurse)
Helper.checkInputAndOutput(lgFilePaths, flags.out)
for (const filePath of lgFilePaths) {
await this.translateLGFile(filePath, flags)
}
}
private async translateLGFile(filePath: string, flags: any) {
let src_lang = this.DEFAULT_SOURCE_LANG
if (flags.srclang) {
src_lang = flags.srclang
}
const translateParts = new TranslateParts(Boolean(flags.translate_comments), Boolean(flags.translate_link_text))
// Support multi-language specification for targets.
// Accepted formats are space or comma separated list of target language codes.
// Tokenize to_lang
const toLangs = flags.tgtlang.split(/[, ]/g)
for (const toLang of toLangs) {
const tgt_lang = toLang.trim()
if (tgt_lang !== '') {
const translateOption = new TranslateOption(flags.translatekey, toLang, src_lang, flags.region)
const result = await this.translateLGFileToSpecificLang(filePath, translateOption, translateParts)
const outputFilePath = this.getOutputFile(filePath, tgt_lang, flags.out)
if (!outputFilePath) {
this.log(`Translate result of file ${filePath} to ${tgt_lang}:`)
this.log(result)
} else {
Helper.writeContentIntoFile(outputFilePath, result, flags.force)
this.log(`Translate result of file ${filePath} to ${tgt_lang} has been written to ${outputFilePath}`)
}
}
}
}
private getOutputFile(filePath: string, language: string, out: string|undefined): string | undefined {
if (filePath === undefined || filePath === '' || out === undefined) {
return undefined
}
const base = Helper.normalizePath(path.resolve(out))
const root = path.dirname(base)
if (!fs.existsSync(root)) {
throw new Error(`folder ${root} not exist`)
}
const extension = path.extname(base)
if (extension) {
// file
return base
}
// folder
// // a.lg -> a.en-us.lg
const newFileName = path.basename(filePath).replace('.lg', '') + '.' + language + '.lg'
return path.join(base, newFileName)
}
private async translateLGFileToSpecificLang(
filePath: string,
translateOption: TranslateOption,
translateParts: TranslateParts
): Promise<string> {
const fileContent = await txtfile.read(filePath)
if (!fileContent) {
throw new CLIError('unable to read file: ' + filePath)
}
const parsedLocContent = await this.parseAndTranslate(fileContent, translateOption, translateParts)
if (!parsedLocContent) {
throw (new CLIError('Sorry, file : ' + filePath + ' had invalid content'))
}
return parsedLocContent
}
// eslint-disable-next-line complexity
private async parseAndTranslate(
fileContent: string,
translateOption: TranslateOption,
translateParts: TranslateParts): Promise<string> {
fileContent = Helper.sanitizeNewLines(fileContent)
const linesInFile = fileContent.split(this.NEWLINE)
let linesToTranslate: TranslateLine[] = []
let localizedContent = ''
let lineCtr = 0
let inMultiLine = false
let inStructure = false
for (const currentLine of linesInFile) {
lineCtr++
if (inStructure) {
if (currentLine.trim() === PARSERCONSTS.RIGHT_SQUARE_BRACKET) {
this.addSegment(linesToTranslate, currentLine, false)
inStructure = false
} else {
// structure mode
const equalIndex = currentLine.indexOf('=')
if (equalIndex >= 0) {
this.addSegment(linesToTranslate, currentLine.substr(0, equalIndex + 1), false)
const valueString = currentLine.substr(equalIndex + 1)
// handle list value
const valueList = valueString.split('|')
// eslint-disable-next-line max-depth
for (let i = 0; i < valueList.length; i++) {
this.addSegmentWithExpression(linesToTranslate, valueList[i])
// eslint-disable-next-line max-depth
if (i < valueList.length - 1) {
this.addSegment(linesToTranslate, '|', false)
}
}
} else {
this.addSegment(linesToTranslate, currentLine, false)
}
}
} else if (inMultiLine) {
if (currentLine.trim().endsWith(PARSERCONSTS.MULTILINE)) {
inMultiLine = false
}
// multiline
this.addSegment(linesToTranslate, currentLine, false)
} else if (this.ImportRegex.test(currentLine.trim())) {
// [abc](def) -> transfer abc
if (translateParts.link) {
const linkValueRegEx = new RegExp(/\(.*?\)/g)
let linkValue = ''
const linkValueList = currentLine.trim().match(linkValueRegEx)
if (linkValueList && linkValueList.length > 0 && linkValueList[0]) {
linkValue = linkValueList[0].replace('(', '').replace(')', '')
}
const linkTextRegEx = new RegExp(/\[.*\]/g)
let linkTextValue = ''
const linkTextList = currentLine.trim().match(linkTextRegEx)
if (linkTextList && linkTextList.length > 0 && linkTextList[0]) {
linkTextValue = linkTextList[0].replace('[', '').replace(']', '')
}
this.addSegment(linesToTranslate, '[', false)
this.addSegment(linesToTranslate, linkTextValue, true)
this.addSegment(linesToTranslate, ']', false)
this.addSegment(linesToTranslate, '(' + linkValue + ')', false)
} else {
this.addSegment(linesToTranslate, currentLine, false)
}
// > abc -> translate abc
} else if (currentLine.trim().startsWith(PARSERCONSTS.DASH) && currentLine.trim().substr(PARSERCONSTS.DASH.length).trim().startsWith(PARSERCONSTS.MULTILINE)) {
// meet - ``` abc, start multi line
this.addSegment(linesToTranslate, currentLine, false)
inMultiLine = true
} else if (currentLine.trim().startsWith(PARSERCONSTS.LEFT_SQUARE_BRACKET)) {
// enter structure
this.addSegment(linesToTranslate, currentLine, false)
inStructure = true
} else if (currentLine.trim().startsWith(PARSERCONSTS.COMMENT)) {
// comments. > abc
if (translateParts.comments) {
const commentIndex = currentLine.trim().indexOf(PARSERCONSTS.COMMENT)
this.addSegment(linesToTranslate, currentLine.substr(0, commentIndex + 1) + ' ', false)
this.addSegment(linesToTranslate, currentLine.trim().substr(commentIndex + 1), true)
} else {
this.addSegment(linesToTranslate, currentLine, false)
}
} else if (currentLine.trim().startsWith(PARSERCONSTS.DASH)) {
// - abc dash
// 1. normal template string
// 2. condition/switch expression template string
// 3. condition/swich template string
// 4. structure value
const dashIndex = currentLine.indexOf(PARSERCONSTS.DASH)
this.addSegment(linesToTranslate, currentLine.substr(0, dashIndex + 1) + ' ', false)
const content = currentLine.substr(dashIndex + 1).trim()
if (this.ConditionRegex.test(content)) {
// condition/switch expression template string
this.addSegment(linesToTranslate, content, false)
} else {
// other template string
this.addSegmentWithExpression(linesToTranslate, content)
}
} else if (currentLine.trim() === '') {
// do nothing
} else {
// add default value
this.addSegment(linesToTranslate, currentLine, false)
}
// add new line for each line
this.addSegment(linesToTranslate, this.NEWLINE, false)
if ((linesToTranslate.length !== 0) && (lineCtr % this.MAX_TRANSLATE_BATCH_SIZE === 0)) {
localizedContent += await this.batchTranslateText(linesToTranslate, translateOption)
linesToTranslate = []
}
}
if ((linesToTranslate.length !== 0)) {
localizedContent += await this.batchTranslateText(linesToTranslate, translateOption)
linesToTranslate = []
}
return localizedContent
}
private addSegmentWithExpression(linesToTranslate: TranslateLine[], content: string) {
const blockList: Block[] = []
const expressionsFound = content.match(this.ExpressionRegex)
if (expressionsFound) {
for (const expression of expressionsFound) {
const eStartIndex = content.indexOf(expression)
const eEndIndex = eStartIndex + expression.length - 1
blockList.push(new Block(expression, eStartIndex, eEndIndex))
}
}
let offset = 0
let candidateText = ''
// Tokenize the input utterance.
for (const block of blockList) {
if (block.start !== offset) {
candidateText = content.substring(offset, block.start)
if (candidateText.trim() !== '') {
this.addSegment(linesToTranslate, candidateText, true)
} else {
this.addSegment(linesToTranslate, candidateText, false)
}
}
this.addSegment(linesToTranslate, block.block, false)
offset = block.end + 1
}
if (offset !== content.length) {
candidateText = content.substring(offset)
if (candidateText.trim() !== '') {
this.addSegment(linesToTranslate, candidateText, true)
} else {
this.addSegment(linesToTranslate, candidateText, false)
}
}
}
private addSegment(linesToTranslate: TranslateLine[], text: string, localize: boolean) {
if (text.length >= this.MAX_CHAR_IN_REQUEST) {
// break it up into smaller segments and add it to the batchRequest payload
const splitRegExp = new RegExp(`(.{${this.MAX_CHAR_IN_REQUEST}})`)
const splitLine = text.split(splitRegExp).filter(O => O)
splitLine.forEach(item => {
linesToTranslate.push(new TranslateLine(item, localize))
})
} else {
linesToTranslate.push(new TranslateLine(text, localize))
}
}
private async batchTranslateText(linesToTranslate: TranslateLine[], translateOption: TranslateOption) {
// responsible for breaking localizable text into chunks that are
// - not more than 5000 characters in combined length
// - not more than 25 segments in one chunk
let retValue = ''
if (!Array.isArray(linesToTranslate) || linesToTranslate.length === 0) return retValue
let charCountInChunk = 0
let batchTranslate: any[] = []
for (const idx in linesToTranslate) {
const item = linesToTranslate[idx]
if (item.text.length + charCountInChunk >= this.MAX_CHAR_IN_REQUEST) {
await this.translateAndMap(batchTranslate, translateOption, linesToTranslate)
batchTranslate = []
charCountInChunk = 0
}
const currentBatchSize = batchTranslate.length > 0 ? batchTranslate.length : 1
if (currentBatchSize % this.MAX_TRANSLATE_BATCH_SIZE === 0) {
await this.translateAndMap(batchTranslate, translateOption, linesToTranslate)
batchTranslate = []
charCountInChunk = 0
}
if (item.localize) {
item.idx = batchTranslate.length
const textItem = {
Text: item.text,
}
batchTranslate.push(textItem)
charCountInChunk += item.text.length
}
}
if (batchTranslate.length !== 0) {
await this.translateAndMap(batchTranslate, translateOption, linesToTranslate)
batchTranslate = []
charCountInChunk = 0
}
for (const item of linesToTranslate) {
retValue += item.text
}
return retValue
}
private async translateAndMap(batchRequest: any, translateOption: TranslateOption, linesToTranslateCopy: TranslateLine[]) {
if (batchRequest.length === 0) return
const data = await Helper.translateText(batchRequest, translateOption)
data.forEach((item: any, idx: number) => {
// find the correponding item in linesToTranslate
const itemInLine = linesToTranslateCopy.find(item => item.idx === idx)
if (itemInLine) {
itemInLine.text = item.translations[0].text
itemInLine.idx = -1
}
})
}
}