]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - scripts/i18n/xliff2json.ts
Little i18n refractoring
[github/Chocobozzz/PeerTube.git] / scripts / i18n / xliff2json.ts
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, LOCALE_FILES } 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 LOCALE_FILES) {
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 process.exit(0)
24 })
25
26 function handleError (err: any) {
27 console.error(err)
28 process.exit(-1)
29 }
30
31 function xliffFile2JSON (filePath: string, cb) {
32 const fileTarget = filePath.replace('.xml', '.json')
33
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)
38
39 xliff12ToJs(fileContent, (err, res) => {
40 if (err) return cb(err)
41
42 const json = createJSONString(res)
43 writeFile(fileTarget, json, err => {
44 if (err) return cb(err)
45
46 return unlink(filePath, cb)
47 })
48 })
49 }
50
51 function removeFirstLine (str: string) {
52 return str.substring(str.indexOf('\n') + 1)
53 }
54
55 function createJSONString (obj: any) {
56 const res: any = {}
57 const strings = obj.resources['']
58
59 Object.keys(strings).forEach(k => res[k] = strings[k].target)
60
61 return JSON.stringify(res)
62 }