import * as xliff12ToJs from 'xliff/xliff12ToJs'
-import { readFileSync, writeFile } from 'fs'
+import { unlink, readFileSync, writeFile } from 'fs'
import { join } from 'path'
+import { buildFileLocale, I18N_LOCALES, isDefaultLocale, LOCALE_FILES } from '../../shared/models/i18n/i18n'
+import { eachSeries } from 'async'
-// First, the player
-const playerSource = join(__dirname, '../../../client/src/locale/target/player_fr.xml')
-const playerTarget = join(__dirname, '../../../client/src/locale/target/player_fr.json')
+const sources: string[] = []
+const availableLocales = Object.keys(I18N_LOCALES)
+ .filter(l => isDefaultLocale(l) === false)
+ .map(l => buildFileLocale(l))
-// Remove the two first lines our xliff module does not like
-let playerFile = readFileSync(playerSource).toString()
-playerFile = removeFirstLine(playerFile)
-playerFile = removeFirstLine(playerFile)
-
-xliff12ToJs(playerFile, (err, res) => {
- if (err) {
- console.error(err)
- process.exit(-1)
+for (const file of LOCALE_FILES) {
+ for (const locale of availableLocales) {
+ sources.push(join(__dirname, '../../../client/src/locale/target/', `${file}_${locale}.xml`))
}
+}
- const json = createJSONString(res)
- writeFile(playerTarget, json, err => {
- if (err) {
- console.error(err)
- process.exit(-1)
- }
+eachSeries(sources, (source, cb) => {
+ xliffFile2JSON(source, cb)
+}, err => {
+ if (err) return handleError(err)
- process.exit(0)
- })
+ process.exit(0)
})
+function handleError (err: any) {
+ console.error(err)
+ process.exit(-1)
+}
+
+function xliffFile2JSON (filePath: string, cb) {
+ const fileTarget = filePath.replace('.xml', '.json')
+
+ // Remove the two first lines our xliff module does not like
+ let fileContent = readFileSync(filePath).toString()
+ fileContent = removeFirstLine(fileContent)
+ fileContent = removeFirstLine(fileContent)
+
+ xliff12ToJs(fileContent, (err, res) => {
+ if (err) return cb(err)
+
+ const json = createJSONString(res)
+ writeFile(fileTarget, json, err => {
+ if (err) return cb(err)
+
+ return unlink(filePath, cb)
+ })
+ })
+}
+
function removeFirstLine (str: string) {
return str.substring(str.indexOf('\n') + 1)
}