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