1 import * as xliff12ToJs from 'xliff/xliff12ToJs'
2 import { unlink, readFileSync, writeFile } from 'fs'
3 import { join } from 'path'
4 import { buildFileLocale, I18N_LOCALES, isDefaultLocale } from '../../shared/models/i18n/i18n'
5 import { eachSeries } from 'async'
7 const sources: string[] = []
8 const availableLocales = Object.keys(I18N_LOCALES)
9 .filter(l => isDefaultLocale(l) === false)
10 .map(l => buildFileLocale(l))
12 for (const file of [ 'server', 'player' ]) {
13 for (const locale of availableLocales) {
14 sources.push(join(__dirname, '../../../client/src/locale/target/', `${file}_${locale}.xml`))
18 eachSeries(sources, (source, cb) => {
19 xliffFile2JSON(source, cb)
21 if (err) return handleError(err)
26 function handleError (err: any) {
31 function xliffFile2JSON (filePath: string, cb) {
32 const fileTarget = filePath.replace('.xml', '.json')
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)
39 xliff12ToJs(fileContent, (err, res) => {
40 if (err) return cb(err)
42 const json = createJSONString(res)
43 writeFile(fileTarget, json, err => {
44 if (err) return cb(err)
46 return unlink(filePath, cb)
51 function removeFirstLine (str: string) {
52 return str.substring(str.indexOf('\n') + 1)
55 function createJSONString (obj: any) {
57 const strings = obj.resources['']
59 Object.keys(strings).forEach(k => res[k] = strings[k].target)
61 return JSON.stringify(res)