diff options
author | Chocobozzz <me@florianbigard.com> | 2018-06-06 16:46:42 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-06-06 16:48:41 +0200 |
commit | 7ce44a74a3b052190cfacd4bd5ee6b92cfc620ac (patch) | |
tree | 6f178426c165f9136eb08354efa4a06c24725f87 /scripts/i18n/xliff2json.ts | |
parent | f07d6385b4b46c3254898292a8a53ed415b8d49b (diff) | |
download | PeerTube-7ce44a74a3b052190cfacd4bd5ee6b92cfc620ac.tar.gz PeerTube-7ce44a74a3b052190cfacd4bd5ee6b92cfc620ac.tar.zst PeerTube-7ce44a74a3b052190cfacd4bd5ee6b92cfc620ac.zip |
Add server localization
Diffstat (limited to 'scripts/i18n/xliff2json.ts')
-rwxr-xr-x | scripts/i18n/xliff2json.ts | 62 |
1 files changed, 41 insertions, 21 deletions
diff --git a/scripts/i18n/xliff2json.ts b/scripts/i18n/xliff2json.ts index 34784ac11..fa5a71d65 100755 --- a/scripts/i18n/xliff2json.ts +++ b/scripts/i18n/xliff2json.ts | |||
@@ -1,33 +1,53 @@ | |||
1 | import * as xliff12ToJs from 'xliff/xliff12ToJs' | 1 | import * as xliff12ToJs from 'xliff/xliff12ToJs' |
2 | import { readFileSync, writeFile } from 'fs' | 2 | import { unlink, readFileSync, writeFile } from 'fs' |
3 | import { join } from 'path' | 3 | import { join } from 'path' |
4 | import { buildFileLocale, I18N_LOCALES, isDefaultLocale } from '../../shared/models/i18n/i18n' | ||
5 | import { eachSeries } from 'async' | ||
4 | 6 | ||
5 | // First, the player | 7 | const sources: string[] = [] |
6 | const playerSource = join(__dirname, '../../../client/src/locale/target/player_fr.xml') | 8 | const availableLocales = Object.keys(I18N_LOCALES) |
7 | const playerTarget = join(__dirname, '../../../client/src/locale/target/player_fr.json') | 9 | .filter(l => isDefaultLocale(l) === false) |
10 | .map(l => buildFileLocale(l)) | ||
8 | 11 | ||
9 | // Remove the two first lines our xliff module does not like | 12 | for (const file of [ 'server', 'player' ]) { |
10 | let playerFile = readFileSync(playerSource).toString() | 13 | for (const locale of availableLocales) { |
11 | playerFile = removeFirstLine(playerFile) | 14 | sources.push(join(__dirname, '../../../client/src/locale/target/', `${file}_${locale}.xml`)) |
12 | playerFile = removeFirstLine(playerFile) | ||
13 | |||
14 | xliff12ToJs(playerFile, (err, res) => { | ||
15 | if (err) { | ||
16 | console.error(err) | ||
17 | process.exit(-1) | ||
18 | } | 15 | } |
16 | } | ||
19 | 17 | ||
20 | const json = createJSONString(res) | 18 | eachSeries(sources, (source, cb) => { |
21 | writeFile(playerTarget, json, err => { | 19 | xliffFile2JSON(source, cb) |
22 | if (err) { | 20 | }, err => { |
23 | console.error(err) | 21 | if (err) return handleError(err) |
24 | process.exit(-1) | ||
25 | } | ||
26 | 22 | ||
27 | process.exit(0) | 23 | process.exit(0) |
28 | }) | ||
29 | }) | 24 | }) |
30 | 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 | |||
31 | function removeFirstLine (str: string) { | 51 | function removeFirstLine (str: string) { |
32 | return str.substring(str.indexOf('\n') + 1) | 52 | return str.substring(str.indexOf('\n') + 1) |
33 | } | 53 | } |