Merge branch 'feature/correctly-send-activities' into develop
[oweals/peertube.git] / scripts / i18n / create-custom-files.ts
1 import * as jsToXliff12 from 'xliff/jsToXliff12'
2 import { writeFile } from 'fs-extra'
3 import { join } from 'path'
4 import {
5   buildLanguages,
6   VIDEO_CATEGORIES,
7   VIDEO_IMPORT_STATES,
8   VIDEO_LICENCES,
9   VIDEO_PRIVACIES,
10   VIDEO_STATES
11 } from '../../server/initializers/constants'
12 import { values } from 'lodash'
13
14 type TranslationType = {
15   target: string
16   data: { [id: string]: string }
17 }
18
19 const videojs = require(join(__dirname, '../../../client/src/locale/source/videojs_en_US.json'))
20 const playerKeys = {
21   'Quality': 'Quality',
22   'Auto': 'Auto',
23   'Speed': 'Speed',
24   'Subtitles/CC': 'Subtitles/CC',
25   'peers': 'peers',
26   'peer': 'peer',
27   'Go to the video page': 'Go to the video page',
28   'Settings': 'Settings',
29   'Uses P2P, others may know you are watching this video.': 'Uses P2P, others may know you are watching this video.',
30   'Copy the video URL': 'Copy the video URL',
31   'Copy the video URL at the current time': 'Copy the video URL at the current time',
32   'Copy embed code': 'Copy embed code',
33   'Total downloaded: ': 'Total downloaded: ',
34   'Total uploaded: ': 'Total uploaded: '
35 }
36 const playerTranslations = {
37   target: join(__dirname, '../../../client/src/locale/source/player_en_US.xml'),
38   data: Object.assign({}, videojs, playerKeys)
39 }
40
41 // Server keys
42 const serverKeys: any = {}
43 values(VIDEO_CATEGORIES)
44   .concat(values(VIDEO_LICENCES))
45   .concat(values(VIDEO_PRIVACIES))
46   .concat(values(VIDEO_STATES))
47   .concat(values(VIDEO_IMPORT_STATES))
48   .concat([
49     'This video does not exist.',
50     'We cannot fetch the video. Please try again later.',
51     'Sorry',
52     'This video is not available because the remote instance is not responding.'
53   ])
54   .forEach(v => serverKeys[v] = v)
55
56 // More keys
57 Object.assign(serverKeys, {
58   'Misc': 'Misc',
59   'Unknown': 'Unknown'
60 })
61
62 const serverTranslations = {
63   target: join(__dirname, '../../../client/src/locale/source/server_en_US.xml'),
64   data: serverKeys
65 }
66
67 // ISO 639 keys
68 const languageKeys: any = {}
69 const languages = buildLanguages()
70 Object.keys(languages).forEach(k => languageKeys[languages[k]] = languages[k])
71
72 const iso639Translations = {
73   target: join(__dirname, '../../../client/src/locale/source/iso639_en_US.xml'),
74   data: languageKeys
75 }
76
77 saveToXliffFile(playerTranslations, err => {
78   if (err) return handleError(err)
79
80   saveToXliffFile(serverTranslations, err => {
81     if (err) return handleError(err)
82
83     saveToXliffFile(iso639Translations, err => {
84       if (err) return handleError(err)
85
86       process.exit(0)
87     })
88   })
89 })
90
91 // Then, the server strings
92
93 function saveToXliffFile (jsonTranslations: TranslationType, cb: Function) {
94   const obj = {
95     resources: {
96       namespace1: {}
97     }
98   }
99   Object.keys(jsonTranslations.data).forEach(k => obj.resources.namespace1[ k ] = { source: jsonTranslations.data[ k ] })
100
101   jsToXliff12(obj, (err, res) => {
102     if (err) return cb(err)
103
104     writeFile(jsonTranslations.target, res, err => {
105       if (err) return cb(err)
106
107       return cb(null)
108     })
109   })
110 }
111
112 function handleError (err: any) {
113   console.error(err)
114   process.exit(-1)
115 }