]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - scripts/i18n/xliff2json.ts
Little i18n refractoring
[github/Chocobozzz/PeerTube.git] / scripts / i18n / xliff2json.ts
CommitLineData
e945b184 1import * as xliff12ToJs from 'xliff/xliff12ToJs'
7ce44a74 2import { unlink, readFileSync, writeFile } from 'fs'
e945b184 3import { join } from 'path'
74b7c6d4 4import { buildFileLocale, I18N_LOCALES, isDefaultLocale, LOCALE_FILES } 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
74b7c6d4 12for (const file of LOCALE_FILES) {
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
7ce44a74 23 process.exit(0)
e945b184
C
24})
25
7ce44a74
C
26function handleError (err: any) {
27 console.error(err)
28 process.exit(-1)
29}
30
31function 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
e945b184
C
51function removeFirstLine (str: string) {
52 return str.substring(str.indexOf('\n') + 1)
53}
54
55function 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}