]>
Commit | Line | Data |
---|---|---|
2aaa1a3f C |
1 | import { registerTSPaths } from '../../server/helpers/register-ts-paths' |
2 | registerTSPaths() | |
3 | ||
e945b184 | 4 | import * as xliff12ToJs from 'xliff/xliff12ToJs' |
ad912c3d | 5 | import { readFileSync, readJSON, unlink, writeFile, writeJSON, existsSync, exists, pathExists } from 'fs-extra' |
e945b184 | 6 | import { join } from 'path' |
850c1bf7 | 7 | import { buildFileLocale, I18N_LOCALES, isDefaultLocale } from '../../shared/models/i18n/i18n' |
7ce44a74 | 8 | import { eachSeries } from 'async' |
e945b184 | 9 | |
7ce44a74 | 10 | const sources: string[] = [] |
ad912c3d C |
11 | const l = [ |
12 | 'ar-001', | |
13 | 'ca-ES', | |
14 | 'cs-CZ', | |
15 | 'da-DK', | |
16 | 'de-DE', | |
17 | 'el-GR', | |
18 | 'en-GB', | |
19 | 'en-US', | |
20 | 'eo', | |
21 | 'es-ES', | |
22 | 'eu-ES', | |
23 | 'fa-IR', | |
24 | 'fi-FI', | |
25 | 'fr-FR', | |
26 | 'gd', | |
27 | 'gl-ES', | |
28 | 'hu-HU', | |
29 | 'it-IT', | |
30 | 'ja-JP', | |
31 | 'jbo', | |
32 | 'ko-KR', | |
33 | 'lt-LT', | |
34 | 'nb-NO', | |
35 | 'nl-NL', | |
36 | 'oc', | |
37 | 'pl-PL', | |
38 | 'pt-BR', | |
39 | 'pt-PT', | |
40 | 'ru-RU', | |
41 | 'sk-SK', | |
42 | 'sl-SI', | |
43 | 'sv-SE', | |
44 | 'ta', | |
45 | 'th-TH', | |
46 | 'tr-TR', | |
47 | 'uk-UA', | |
48 | 'vi-VN', | |
49 | 'zh-Hans-CN', | |
50 | 'zh-Hant-TW' | |
51 | ] | |
52 | ||
53 | const availableLocales = l.filter(l => isDefaultLocale(l) === false) | |
7ce44a74 | 54 | .map(l => buildFileLocale(l)) |
e945b184 | 55 | |
850c1bf7 | 56 | for (const file of [ 'player', 'server', 'iso639' ]) { |
7ce44a74 C |
57 | for (const locale of availableLocales) { |
58 | sources.push(join(__dirname, '../../../client/src/locale/target/', `${file}_${locale}.xml`)) | |
e945b184 | 59 | } |
7ce44a74 | 60 | } |
e945b184 | 61 | |
7ce44a74 C |
62 | eachSeries(sources, (source, cb) => { |
63 | xliffFile2JSON(source, cb) | |
64 | }, err => { | |
65 | if (err) return handleError(err) | |
e945b184 | 66 | |
850c1bf7 C |
67 | mergeISO639InServer(err => { |
68 | if (err) return handleError(err) | |
69 | ||
ad912c3d | 70 | injectMissingTranslations().then(() => process.exit(0)) |
850c1bf7 | 71 | }) |
e945b184 C |
72 | }) |
73 | ||
7ce44a74 C |
74 | function handleError (err: any) { |
75 | console.error(err) | |
76 | process.exit(-1) | |
77 | } | |
78 | ||
79 | function xliffFile2JSON (filePath: string, cb) { | |
80 | const fileTarget = filePath.replace('.xml', '.json') | |
81 | ||
ad912c3d C |
82 | if (!existsSync(filePath)) { |
83 | console.log('No file %s exists.', filePath) | |
84 | return cb() | |
85 | } | |
86 | ||
7ce44a74 C |
87 | // Remove the two first lines our xliff module does not like |
88 | let fileContent = readFileSync(filePath).toString() | |
89 | fileContent = removeFirstLine(fileContent) | |
90 | fileContent = removeFirstLine(fileContent) | |
91 | ||
92 | xliff12ToJs(fileContent, (err, res) => { | |
93 | if (err) return cb(err) | |
94 | ||
95 | const json = createJSONString(res) | |
96 | writeFile(fileTarget, json, err => { | |
97 | if (err) return cb(err) | |
98 | ||
99 | return unlink(filePath, cb) | |
100 | }) | |
101 | }) | |
102 | } | |
103 | ||
850c1bf7 C |
104 | function mergeISO639InServer (cb) { |
105 | eachSeries(availableLocales, (locale, eachCallback) => { | |
106 | const serverPath = join(__dirname, '../../../client/src/locale/target/', `server_${locale}.json`) | |
107 | const iso639Path = join(__dirname, '../../../client/src/locale/target/', `iso639_${locale}.json`) | |
108 | ||
ad912c3d C |
109 | if (!existsSync(serverPath)) { |
110 | console.log('No file %s exists.', serverPath) | |
111 | return cb() | |
112 | } | |
113 | if (!existsSync(iso639Path)) { | |
114 | console.log('No file %s exists.', iso639Path) | |
115 | return cb() | |
116 | } | |
117 | ||
850c1bf7 C |
118 | const resServer = readFileSync(serverPath).toString() |
119 | const resISO639 = readFileSync(iso639Path).toString() | |
120 | ||
121 | const jsonServer = JSON.parse(resServer) | |
122 | const jsonISO639 = JSON.parse(resISO639) | |
123 | ||
124 | Object.assign(jsonServer, jsonISO639) | |
125 | const serverString = JSON.stringify(jsonServer) | |
126 | ||
127 | writeFile(serverPath, serverString, err => { | |
128 | if (err) return eachCallback(err) | |
129 | ||
130 | return unlink(iso639Path, eachCallback) | |
131 | }) | |
132 | }, cb) | |
133 | } | |
134 | ||
e945b184 C |
135 | function removeFirstLine (str: string) { |
136 | return str.substring(str.indexOf('\n') + 1) | |
137 | } | |
138 | ||
139 | function createJSONString (obj: any) { | |
140 | const res: any = {} | |
141 | const strings = obj.resources[''] | |
142 | ||
143 | Object.keys(strings).forEach(k => res[k] = strings[k].target) | |
144 | ||
145 | return JSON.stringify(res) | |
146 | } | |
ad912c3d C |
147 | |
148 | async function injectMissingTranslations () { | |
149 | const baseServer = await readJSON(join(__dirname, '../../../client/src/locale/server.en-US.json')) | |
150 | Object.keys(baseServer).forEach(k => baseServer[k] = '') | |
151 | ||
152 | for (const locale of availableLocales) { | |
153 | const serverPath = join(__dirname, '../../../client/src/locale/target/', `server_${locale}.json`) | |
154 | if (!await pathExists(serverPath)) { | |
155 | console.log('No file exists to inject missing translations: %s.', serverPath) | |
156 | continue | |
157 | } | |
158 | ||
159 | let serverJSON = await readJSON(serverPath) | |
160 | ||
161 | serverJSON = Object.assign({}, baseServer, serverJSON) | |
162 | await writeJSON(serverPath, serverJSON) | |
163 | } | |
164 | ||
165 | const basePlayer = await readJSON(join(__dirname, '../../../client/src/locale/player.en-US.json')) | |
166 | Object.keys(basePlayer).forEach(k => basePlayer[k] = '') | |
167 | for (const locale of availableLocales) { | |
168 | const serverPath = join(__dirname, '../../../client/src/locale/target/', `player_${locale}.json`) | |
169 | if (!await pathExists(serverPath)) { | |
170 | console.log('No file exists to inject missing translations: %s.', serverPath) | |
171 | continue | |
172 | } | |
173 | ||
174 | let serverJSON = await readJSON(serverPath) | |
175 | ||
176 | serverJSON = Object.assign({}, basePlayer, serverJSON) | |
177 | await writeJSON(serverPath, serverJSON) | |
178 | } | |
179 | } |