diff options
Diffstat (limited to 'scripts/i18n/xliff2json.ts')
-rwxr-xr-x | scripts/i18n/xliff2json.ts | 62 |
1 files changed, 41 insertions, 21 deletions
diff --git a/scripts/i18n/xliff2json.ts b/scripts/i18n/xliff2json.ts index 34784ac11..fa5a71d65 100755 --- a/scripts/i18n/xliff2json.ts +++ b/scripts/i18n/xliff2json.ts | |||
@@ -1,33 +1,53 @@ | |||
1 | import * as xliff12ToJs from 'xliff/xliff12ToJs' | 1 | import * as xliff12ToJs from 'xliff/xliff12ToJs' |
2 | import { readFileSync, writeFile } from 'fs' | 2 | import { unlink, readFileSync, writeFile } from 'fs' |
3 | import { join } from 'path' | 3 | import { join } from 'path' |
4 | import { buildFileLocale, I18N_LOCALES, isDefaultLocale } from '../../shared/models/i18n/i18n' | ||
5 | import { eachSeries } from 'async' | ||
4 | 6 | ||
5 | // First, the player | 7 | const sources: string[] = [] |
6 | const playerSource = join(__dirname, '../../../client/src/locale/target/player_fr.xml') | 8 | const availableLocales = Object.keys(I18N_LOCALES) |
7 | const playerTarget = join(__dirname, '../../../client/src/locale/target/player_fr.json') | 9 | .filter(l => isDefaultLocale(l) === false) |
10 | .map(l => buildFileLocale(l)) | ||
8 | 11 | ||
9 | // Remove the two first lines our xliff module does not like | 12 | for (const file of [ 'server', 'player' ]) { |
10 | let playerFile = readFileSync(playerSource).toString() | 13 | for (const locale of availableLocales) { |
11 | playerFile = removeFirstLine(playerFile) | 14 | sources.push(join(__dirname, '../../../client/src/locale/target/', `${file}_${locale}.xml`)) |
12 | playerFile = removeFirstLine(playerFile) | ||
13 | |||
14 | xliff12ToJs(playerFile, (err, res) => { | ||
15 | if (err) { | ||
16 | console.error(err) | ||
17 | process.exit(-1) | ||
18 | } | 15 | } |
16 | } | ||
19 | 17 | ||
20 | const json = createJSONString(res) | 18 | eachSeries(sources, (source, cb) => { |
21 | writeFile(playerTarget, json, err => { | 19 | xliffFile2JSON(source, cb) |
22 | if (err) { | 20 | }, err => { |
23 | console.error(err) | 21 | if (err) return handleError(err) |
24 | process.exit(-1) | ||
25 | } | ||
26 | 22 | ||
27 | process.exit(0) | 23 | process.exit(0) |
28 | }) | ||
29 | }) | 24 | }) |
30 | 25 | ||
26 | function handleError (err: any) { | ||
27 | console.error(err) | ||
28 | process.exit(-1) | ||
29 | } | ||
30 | |||
31 | function xliffFile2JSON (filePath: string, cb) { | ||
32 | const fileTarget = filePath.replace('.xml', '.json') | ||
33 | |||
34 | // Remove the two first lines our xliff module does not like | ||
35 | let fileContent = readFileSync(filePath).toString() | ||
36 | fileContent = removeFirstLine(fileContent) | ||
37 | fileContent = removeFirstLine(fileContent) | ||
38 | |||
39 | xliff12ToJs(fileContent, (err, res) => { | ||
40 | if (err) return cb(err) | ||
41 | |||
42 | const json = createJSONString(res) | ||
43 | writeFile(fileTarget, json, err => { | ||
44 | if (err) return cb(err) | ||
45 | |||
46 | return unlink(filePath, cb) | ||
47 | }) | ||
48 | }) | ||
49 | } | ||
50 | |||
31 | function removeFirstLine (str: string) { | 51 | function removeFirstLine (str: string) { |
32 | return str.substring(str.indexOf('\n') + 1) | 52 | return str.substring(str.indexOf('\n') + 1) |
33 | } | 53 | } |