]>
Commit | Line | Data |
---|---|---|
1 | import * as xliff12ToJs from 'xliff/xliff12ToJs' | |
2 | import { readFileSync, unlink, writeFile } from 'fs' | |
3 | import { join } from 'path' | |
4 | import { buildFileLocale, I18N_LOCALES, isDefaultLocale } from '../../shared/models/i18n/i18n' | |
5 | import { eachSeries } from 'async' | |
6 | ||
7 | const sources: string[] = [] | |
8 | const availableLocales = Object.keys(I18N_LOCALES) | |
9 | .filter(l => isDefaultLocale(l) === false) | |
10 | .map(l => buildFileLocale(l)) | |
11 | ||
12 | for (const file of [ 'player', 'server', 'iso639' ]) { | |
13 | for (const locale of availableLocales) { | |
14 | sources.push(join(__dirname, '../../../client/src/locale/target/', `${file}_${locale}.xml`)) | |
15 | } | |
16 | } | |
17 | ||
18 | eachSeries(sources, (source, cb) => { | |
19 | xliffFile2JSON(source, cb) | |
20 | }, err => { | |
21 | if (err) return handleError(err) | |
22 | ||
23 | mergeISO639InServer(err => { | |
24 | if (err) return handleError(err) | |
25 | ||
26 | process.exit(0) | |
27 | }) | |
28 | }) | |
29 | ||
30 | function handleError (err: any) { | |
31 | console.error(err) | |
32 | process.exit(-1) | |
33 | } | |
34 | ||
35 | function xliffFile2JSON (filePath: string, cb) { | |
36 | const fileTarget = filePath.replace('.xml', '.json') | |
37 | ||
38 | // Remove the two first lines our xliff module does not like | |
39 | let fileContent = readFileSync(filePath).toString() | |
40 | fileContent = removeFirstLine(fileContent) | |
41 | fileContent = removeFirstLine(fileContent) | |
42 | ||
43 | xliff12ToJs(fileContent, (err, res) => { | |
44 | if (err) return cb(err) | |
45 | ||
46 | const json = createJSONString(res) | |
47 | writeFile(fileTarget, json, err => { | |
48 | if (err) return cb(err) | |
49 | ||
50 | return unlink(filePath, cb) | |
51 | }) | |
52 | }) | |
53 | } | |
54 | ||
55 | function mergeISO639InServer (cb) { | |
56 | eachSeries(availableLocales, (locale, eachCallback) => { | |
57 | const serverPath = join(__dirname, '../../../client/src/locale/target/', `server_${locale}.json`) | |
58 | const iso639Path = join(__dirname, '../../../client/src/locale/target/', `iso639_${locale}.json`) | |
59 | ||
60 | const resServer = readFileSync(serverPath).toString() | |
61 | const resISO639 = readFileSync(iso639Path).toString() | |
62 | ||
63 | const jsonServer = JSON.parse(resServer) | |
64 | const jsonISO639 = JSON.parse(resISO639) | |
65 | ||
66 | Object.assign(jsonServer, jsonISO639) | |
67 | const serverString = JSON.stringify(jsonServer) | |
68 | ||
69 | writeFile(serverPath, serverString, err => { | |
70 | if (err) return eachCallback(err) | |
71 | ||
72 | return unlink(iso639Path, eachCallback) | |
73 | }) | |
74 | }, cb) | |
75 | } | |
76 | ||
77 | function removeFirstLine (str: string) { | |
78 | return str.substring(str.indexOf('\n') + 1) | |
79 | } | |
80 | ||
81 | function createJSONString (obj: any) { | |
82 | const res: any = {} | |
83 | const strings = obj.resources[''] | |
84 | ||
85 | Object.keys(strings).forEach(k => res[k] = strings[k].target) | |
86 | ||
87 | return JSON.stringify(res) | |
88 | } |