]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - scripts/i18n/xliff2json.ts
Move iso639 strings in its own translation file
[github/Chocobozzz/PeerTube.git] / scripts / i18n / xliff2json.ts
CommitLineData
e945b184 1import * as xliff12ToJs from 'xliff/xliff12ToJs'
850c1bf7 2import { readFile, readFileSync, unlink, writeFile } from 'fs'
e945b184 3import { join } from 'path'
850c1bf7 4import { buildFileLocale, I18N_LOCALES, isDefaultLocale } from '../../shared/models/i18n/i18n'
7ce44a74 5import { eachSeries } from 'async'
e945b184 6
7ce44a74
C
7const sources: string[] = []
8const availableLocales = Object.keys(I18N_LOCALES)
9 .filter(l => isDefaultLocale(l) === false)
10 .map(l => buildFileLocale(l))
e945b184 11
850c1bf7 12for (const file of [ 'player', 'server', 'iso639' ]) {
7ce44a74
C
13 for (const locale of availableLocales) {
14 sources.push(join(__dirname, '../../../client/src/locale/target/', `${file}_${locale}.xml`))
e945b184 15 }
7ce44a74 16}
e945b184 17
7ce44a74
C
18eachSeries(sources, (source, cb) => {
19 xliffFile2JSON(source, cb)
20}, err => {
21 if (err) return handleError(err)
e945b184 22
850c1bf7
C
23 mergeISO639InServer(err => {
24 if (err) return handleError(err)
25
26 process.exit(0)
27 })
e945b184
C
28})
29
7ce44a74
C
30function handleError (err: any) {
31 console.error(err)
32 process.exit(-1)
33}
34
35function 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
850c1bf7
C
55function 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
e945b184
C
77function removeFirstLine (str: string) {
78 return str.substring(str.indexOf('\n') + 1)
79}
80
81function 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}