diff options
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/i18n/create-custom-files.ts | 70 | ||||
-rwxr-xr-x | scripts/i18n/xliff2json.ts | 62 |
2 files changed, 92 insertions, 40 deletions
diff --git a/scripts/i18n/create-custom-files.ts b/scripts/i18n/create-custom-files.ts index d8a87f291..3519afd47 100755 --- a/scripts/i18n/create-custom-files.ts +++ b/scripts/i18n/create-custom-files.ts | |||
@@ -1,12 +1,15 @@ | |||
1 | import * as jsToXliff12 from 'xliff/jsToXliff12' | 1 | import * as jsToXliff12 from 'xliff/jsToXliff12' |
2 | import { writeFile } from 'fs' | 2 | import { writeFile } from 'fs' |
3 | import { join } from 'path' | 3 | import { join } from 'path' |
4 | import { buildLanguages, VIDEO_CATEGORIES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../server/initializers/constants' | ||
5 | import { values } from 'lodash' | ||
4 | 6 | ||
5 | // First, the player | 7 | type TranslationType = { |
6 | const playerSource = join(__dirname, '../../../client/src/locale/source/videojs_en_US.json') | 8 | target: string |
7 | const playerTarget = join(__dirname, '../../../client/src/locale/source/player_en_US.xml') | 9 | data: { [id: string]: string } |
10 | } | ||
8 | 11 | ||
9 | const videojs = require(playerSource) | 12 | const videojs = require(join(__dirname, '../../../client/src/locale/source/videojs_en_US.json')) |
10 | const playerKeys = { | 13 | const playerKeys = { |
11 | 'Quality': 'Quality', | 14 | 'Quality': 'Quality', |
12 | 'Auto': 'Auto', | 15 | 'Auto': 'Auto', |
@@ -19,36 +22,65 @@ const playerKeys = { | |||
19 | 'Copy the video URL at the current time': 'Copy the video URL at the current time', | 22 | 'Copy the video URL at the current time': 'Copy the video URL at the current time', |
20 | 'Copy embed code': 'Copy embed code' | 23 | 'Copy embed code': 'Copy embed code' |
21 | } | 24 | } |
22 | 25 | const playerTranslations = { | |
23 | const obj = { | 26 | target: join(__dirname, '../../../client/src/locale/source/player_en_US.xml'), |
24 | resources: { | 27 | data: Object.assign({}, videojs, playerKeys) |
25 | namespace1: {} | ||
26 | } | ||
27 | } | 28 | } |
28 | 29 | ||
29 | for (const sourceObject of [ videojs, playerKeys ]) { | 30 | // Server keys |
30 | Object.keys(sourceObject).forEach(k => obj.resources.namespace1[ k ] = { source: sourceObject[ k ] }) | 31 | const serverKeys: any = {} |
32 | values(VIDEO_CATEGORIES) | ||
33 | .concat(values(VIDEO_LICENCES)) | ||
34 | .concat(values(VIDEO_PRIVACIES)) | ||
35 | .forEach(v => serverKeys[v] = v) | ||
36 | |||
37 | // ISO 639 keys | ||
38 | const languages = buildLanguages() | ||
39 | Object.keys(languages).forEach(k => serverKeys[languages[k]] = languages[k]) | ||
40 | |||
41 | // More keys | ||
42 | Object.assign(serverKeys, { | ||
43 | 'Misc': 'Misc', | ||
44 | 'Unknown': 'Unknown' | ||
45 | }) | ||
46 | |||
47 | const serverTranslations = { | ||
48 | target: join(__dirname, '../../../client/src/locale/source/server_en_US.xml'), | ||
49 | data: serverKeys | ||
31 | } | 50 | } |
32 | 51 | ||
33 | saveToXliffFile(playerTarget, obj, err => { | 52 | saveToXliffFile(playerTranslations, err => { |
34 | if (err) { | 53 | if (err) return handleError(err) |
35 | console.error(err) | 54 | |
36 | process.exit(-1) | 55 | saveToXliffFile(serverTranslations, err => { |
37 | } | 56 | if (err) return handleError(err) |
38 | 57 | ||
39 | process.exit(0) | 58 | process.exit(0) |
59 | }) | ||
40 | }) | 60 | }) |
41 | 61 | ||
42 | // Then, the server strings | 62 | // Then, the server strings |
43 | 63 | ||
44 | function saveToXliffFile (targetPath: string, obj: any, cb: Function) { | 64 | function saveToXliffFile (jsonTranslations: TranslationType, cb: Function) { |
65 | const obj = { | ||
66 | resources: { | ||
67 | namespace1: {} | ||
68 | } | ||
69 | } | ||
70 | Object.keys(jsonTranslations.data).forEach(k => obj.resources.namespace1[ k ] = { source: jsonTranslations.data[ k ] }) | ||
71 | |||
45 | jsToXliff12(obj, (err, res) => { | 72 | jsToXliff12(obj, (err, res) => { |
46 | if (err) return cb(err) | 73 | if (err) return cb(err) |
47 | 74 | ||
48 | writeFile(playerTarget, res, err => { | 75 | writeFile(jsonTranslations.target, res, err => { |
49 | if (err) return cb(err) | 76 | if (err) return cb(err) |
50 | 77 | ||
51 | return cb(null) | 78 | return cb(null) |
52 | }) | 79 | }) |
53 | }) | 80 | }) |
54 | } | 81 | } |
82 | |||
83 | function handleError (err: any) { | ||
84 | console.error(err) | ||
85 | process.exit(-1) | ||
86 | } \ No newline at end of file | ||
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 | } |