1 import { registerTSPaths } from '../../server/helpers/register-ts-paths'
4 import * as xliff12ToJs from 'xliff/xliff12ToJs'
5 import { readFileSync, unlink, writeFile } from 'fs-extra'
6 import { join } from 'path'
7 import { buildFileLocale, I18N_LOCALES, isDefaultLocale } from '../../shared/models/i18n/i18n'
8 import { eachSeries } from 'async'
10 const sources: string[] = []
11 const availableLocales = Object.keys(I18N_LOCALES)
12 .filter(l => isDefaultLocale(l) === false)
13 .map(l => buildFileLocale(l))
15 for (const file of [ 'player', 'server', 'iso639' ]) {
16 for (const locale of availableLocales) {
17 sources.push(join(__dirname, '../../../client/src/locale/target/', `${file}_${locale}.xml`))
21 eachSeries(sources, (source, cb) => {
22 xliffFile2JSON(source, cb)
24 if (err) return handleError(err)
26 mergeISO639InServer(err => {
27 if (err) return handleError(err)
33 function handleError (err: any) {
38 function xliffFile2JSON (filePath: string, cb) {
39 const fileTarget = filePath.replace('.xml', '.json')
41 // Remove the two first lines our xliff module does not like
42 let fileContent = readFileSync(filePath).toString()
43 fileContent = removeFirstLine(fileContent)
44 fileContent = removeFirstLine(fileContent)
46 xliff12ToJs(fileContent, (err, res) => {
47 if (err) return cb(err)
49 const json = createJSONString(res)
50 writeFile(fileTarget, json, err => {
51 if (err) return cb(err)
53 return unlink(filePath, cb)
58 function mergeISO639InServer (cb) {
59 eachSeries(availableLocales, (locale, eachCallback) => {
60 const serverPath = join(__dirname, '../../../client/src/locale/target/', `server_${locale}.json`)
61 const iso639Path = join(__dirname, '../../../client/src/locale/target/', `iso639_${locale}.json`)
63 const resServer = readFileSync(serverPath).toString()
64 const resISO639 = readFileSync(iso639Path).toString()
66 const jsonServer = JSON.parse(resServer)
67 const jsonISO639 = JSON.parse(resISO639)
69 Object.assign(jsonServer, jsonISO639)
70 const serverString = JSON.stringify(jsonServer)
72 writeFile(serverPath, serverString, err => {
73 if (err) return eachCallback(err)
75 return unlink(iso639Path, eachCallback)
80 function removeFirstLine (str: string) {
81 return str.substring(str.indexOf('\n') + 1)
84 function createJSONString (obj: any) {
86 const strings = obj.resources['']
88 Object.keys(strings).forEach(k => res[k] = strings[k].target)
90 return JSON.stringify(res)