]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - scripts/i18n/xliff2json.ts
Make build works
[github/Chocobozzz/PeerTube.git] / scripts / i18n / xliff2json.ts
index 17c73181beb6dd77621814cf23f3b0ab5210902a..d15039bb92a8e527799d132737bd0cda34543d48 100755 (executable)
@@ -1,12 +1,56 @@
+import { registerTSPaths } from '../../server/helpers/register-ts-paths'
+registerTSPaths()
+
 import * as xliff12ToJs from 'xliff/xliff12ToJs'
-import { readFile, readFileSync, unlink, writeFile } from 'fs'
+import { readFileSync, readJSON, unlink, writeFile, writeJSON, existsSync, exists, pathExists } from 'fs-extra'
 import { join } from 'path'
 import { buildFileLocale, I18N_LOCALES, isDefaultLocale } from '../../shared/models/i18n/i18n'
 import { eachSeries } from 'async'
 
 const sources: string[] = []
-const availableLocales = Object.keys(I18N_LOCALES)
-                               .filter(l => isDefaultLocale(l) === false)
+const l = [
+  'ar-001',
+  'ca-ES',
+  'cs-CZ',
+  'da-DK',
+  'de-DE',
+  'el-GR',
+  'en-GB',
+  'en-US',
+  'eo',
+  'es-ES',
+  'eu-ES',
+  'fa-IR',
+  'fi-FI',
+  'fr-FR',
+  'gd',
+  'gl-ES',
+  'hu-HU',
+  'it-IT',
+  'ja-JP',
+  'jbo',
+  'ko-KR',
+  'lt-LT',
+  'nb-NO',
+  'nl-NL',
+  'oc',
+  'pl-PL',
+  'pt-BR',
+  'pt-PT',
+  'ru-RU',
+  'sk-SK',
+  'sl-SI',
+  'sv-SE',
+  'ta',
+  'th-TH',
+  'tr-TR',
+  'uk-UA',
+  'vi-VN',
+  'zh-Hans-CN',
+  'zh-Hant-TW'
+]
+
+const availableLocales = l.filter(l => isDefaultLocale(l) === false)
                                .map(l => buildFileLocale(l))
 
 for (const file of [ 'player', 'server', 'iso639' ]) {
@@ -23,7 +67,7 @@ eachSeries(sources, (source, cb) => {
   mergeISO639InServer(err => {
     if (err) return handleError(err)
 
-    process.exit(0)
+    injectMissingTranslations().then(() => process.exit(0))
   })
 })
 
@@ -35,6 +79,11 @@ function handleError (err: any) {
 function xliffFile2JSON (filePath: string, cb) {
   const fileTarget = filePath.replace('.xml', '.json')
 
+  if (!existsSync(filePath)) {
+    console.log('No file %s exists.', filePath)
+    return cb()
+  }
+
   // Remove the two first lines our xliff module does not like
   let fileContent = readFileSync(filePath).toString()
   fileContent = removeFirstLine(fileContent)
@@ -57,6 +106,15 @@ function mergeISO639InServer (cb) {
     const serverPath = join(__dirname, '../../../client/src/locale/target/', `server_${locale}.json`)
     const iso639Path = join(__dirname, '../../../client/src/locale/target/', `iso639_${locale}.json`)
 
+    if (!existsSync(serverPath)) {
+      console.log('No file %s exists.', serverPath)
+      return cb()
+    }
+    if (!existsSync(iso639Path)) {
+      console.log('No file %s exists.', iso639Path)
+      return cb()
+    }
+
     const resServer = readFileSync(serverPath).toString()
     const resISO639 = readFileSync(iso639Path).toString()
 
@@ -86,3 +144,36 @@ function createJSONString (obj: any) {
 
   return JSON.stringify(res)
 }
+
+async function injectMissingTranslations () {
+  const baseServer = await readJSON(join(__dirname, '../../../client/src/locale/server.en-US.json'))
+  Object.keys(baseServer).forEach(k => baseServer[k] = '')
+
+  for (const locale of availableLocales) {
+    const serverPath = join(__dirname, '../../../client/src/locale/target/', `server_${locale}.json`)
+    if (!await pathExists(serverPath)) {
+      console.log('No file exists to inject missing translations: %s.', serverPath)
+      continue
+    }
+
+    let serverJSON = await readJSON(serverPath)
+
+    serverJSON = Object.assign({}, baseServer, serverJSON)
+    await writeJSON(serverPath, serverJSON)
+  }
+
+  const basePlayer = await readJSON(join(__dirname, '../../../client/src/locale/player.en-US.json'))
+  Object.keys(basePlayer).forEach(k => basePlayer[k] = '')
+  for (const locale of availableLocales) {
+    const serverPath = join(__dirname, '../../../client/src/locale/target/', `player_${locale}.json`)
+    if (!await pathExists(serverPath)) {
+      console.log('No file exists to inject missing translations: %s.', serverPath)
+      continue
+    }
+
+    let serverJSON = await readJSON(serverPath)
+
+    serverJSON = Object.assign({}, basePlayer, serverJSON)
+    await writeJSON(serverPath, serverJSON)
+  }
+}