From 7ce44a74a3b052190cfacd4bd5ee6b92cfc620ac Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 6 Jun 2018 16:46:42 +0200 Subject: Add server localization --- scripts/i18n/create-custom-files.ts | 70 +++++++++++++++++++++++++++---------- scripts/i18n/xliff2json.ts | 62 +++++++++++++++++++++----------- 2 files changed, 92 insertions(+), 40 deletions(-) (limited to 'scripts') 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 @@ import * as jsToXliff12 from 'xliff/jsToXliff12' import { writeFile } from 'fs' import { join } from 'path' +import { buildLanguages, VIDEO_CATEGORIES, VIDEO_LICENCES, VIDEO_PRIVACIES } from '../../server/initializers/constants' +import { values } from 'lodash' -// First, the player -const playerSource = join(__dirname, '../../../client/src/locale/source/videojs_en_US.json') -const playerTarget = join(__dirname, '../../../client/src/locale/source/player_en_US.xml') +type TranslationType = { + target: string + data: { [id: string]: string } +} -const videojs = require(playerSource) +const videojs = require(join(__dirname, '../../../client/src/locale/source/videojs_en_US.json')) const playerKeys = { 'Quality': 'Quality', 'Auto': 'Auto', @@ -19,36 +22,65 @@ const playerKeys = { 'Copy the video URL at the current time': 'Copy the video URL at the current time', 'Copy embed code': 'Copy embed code' } - -const obj = { - resources: { - namespace1: {} - } +const playerTranslations = { + target: join(__dirname, '../../../client/src/locale/source/player_en_US.xml'), + data: Object.assign({}, videojs, playerKeys) } -for (const sourceObject of [ videojs, playerKeys ]) { - Object.keys(sourceObject).forEach(k => obj.resources.namespace1[ k ] = { source: sourceObject[ k ] }) +// Server keys +const serverKeys: any = {} +values(VIDEO_CATEGORIES) + .concat(values(VIDEO_LICENCES)) + .concat(values(VIDEO_PRIVACIES)) + .forEach(v => serverKeys[v] = v) + +// ISO 639 keys +const languages = buildLanguages() +Object.keys(languages).forEach(k => serverKeys[languages[k]] = languages[k]) + +// More keys +Object.assign(serverKeys, { + 'Misc': 'Misc', + 'Unknown': 'Unknown' +}) + +const serverTranslations = { + target: join(__dirname, '../../../client/src/locale/source/server_en_US.xml'), + data: serverKeys } -saveToXliffFile(playerTarget, obj, err => { - if (err) { - console.error(err) - process.exit(-1) - } +saveToXliffFile(playerTranslations, err => { + if (err) return handleError(err) + + saveToXliffFile(serverTranslations, err => { + if (err) return handleError(err) - process.exit(0) + process.exit(0) + }) }) // Then, the server strings -function saveToXliffFile (targetPath: string, obj: any, cb: Function) { +function saveToXliffFile (jsonTranslations: TranslationType, cb: Function) { + const obj = { + resources: { + namespace1: {} + } + } + Object.keys(jsonTranslations.data).forEach(k => obj.resources.namespace1[ k ] = { source: jsonTranslations.data[ k ] }) + jsToXliff12(obj, (err, res) => { if (err) return cb(err) - writeFile(playerTarget, res, err => { + writeFile(jsonTranslations.target, res, err => { if (err) return cb(err) return cb(null) }) }) } + +function handleError (err: any) { + console.error(err) + process.exit(-1) +} \ 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 @@ import * as xliff12ToJs from 'xliff/xliff12ToJs' -import { readFileSync, writeFile } from 'fs' +import { unlink, readFileSync, writeFile } from 'fs' import { join } from 'path' +import { buildFileLocale, I18N_LOCALES, isDefaultLocale } from '../../shared/models/i18n/i18n' +import { eachSeries } from 'async' -// First, the player -const playerSource = join(__dirname, '../../../client/src/locale/target/player_fr.xml') -const playerTarget = join(__dirname, '../../../client/src/locale/target/player_fr.json') +const sources: string[] = [] +const availableLocales = Object.keys(I18N_LOCALES) + .filter(l => isDefaultLocale(l) === false) + .map(l => buildFileLocale(l)) -// Remove the two first lines our xliff module does not like -let playerFile = readFileSync(playerSource).toString() -playerFile = removeFirstLine(playerFile) -playerFile = removeFirstLine(playerFile) - -xliff12ToJs(playerFile, (err, res) => { - if (err) { - console.error(err) - process.exit(-1) +for (const file of [ 'server', 'player' ]) { + for (const locale of availableLocales) { + sources.push(join(__dirname, '../../../client/src/locale/target/', `${file}_${locale}.xml`)) } +} - const json = createJSONString(res) - writeFile(playerTarget, json, err => { - if (err) { - console.error(err) - process.exit(-1) - } +eachSeries(sources, (source, cb) => { + xliffFile2JSON(source, cb) +}, err => { + if (err) return handleError(err) - process.exit(0) - }) + process.exit(0) }) +function handleError (err: any) { + console.error(err) + process.exit(-1) +} + +function xliffFile2JSON (filePath: string, cb) { + const fileTarget = filePath.replace('.xml', '.json') + + // Remove the two first lines our xliff module does not like + let fileContent = readFileSync(filePath).toString() + fileContent = removeFirstLine(fileContent) + fileContent = removeFirstLine(fileContent) + + xliff12ToJs(fileContent, (err, res) => { + if (err) return cb(err) + + const json = createJSONString(res) + writeFile(fileTarget, json, err => { + if (err) return cb(err) + + return unlink(filePath, cb) + }) + }) +} + function removeFirstLine (str: string) { return str.substring(str.indexOf('\n') + 1) } -- cgit v1.2.3