create-custom-files.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. type TranslationType = {
  14. target: string
  15. data: { [id: string]: string }
  16. }
  17. const videojs = require(join(__dirname, '../../../client/src/locale/source/videojs_en_US.json'))
  18. const playerKeys = {
  19. 'Quality': 'Quality',
  20. 'Auto': 'Auto',
  21. 'Speed': 'Speed',
  22. 'Subtitles/CC': 'Subtitles/CC',
  23. 'peers': 'peers',
  24. 'Go to the video page': 'Go to the video page',
  25. 'Settings': 'Settings',
  26. 'Uses P2P, others may know you are watching this video.': 'Uses P2P, others may know you are watching this video.',
  27. 'Copy the video URL': 'Copy the video URL',
  28. 'Copy the video URL at the current time': 'Copy the video URL at the current time',
  29. 'Copy embed code': 'Copy embed code'
  30. }
  31. const playerTranslations = {
  32. target: join(__dirname, '../../../client/src/locale/source/player_en_US.xml'),
  33. data: Object.assign({}, videojs, playerKeys)
  34. }
  35. // Server keys
  36. const serverKeys: any = {}
  37. values(VIDEO_CATEGORIES)
  38. .concat(values(VIDEO_LICENCES))
  39. .concat(values(VIDEO_PRIVACIES))
  40. .concat(values(VIDEO_STATES))
  41. .concat(values(VIDEO_IMPORT_STATES))
  42. .forEach(v => serverKeys[v] = v)
  43. // More keys
  44. Object.assign(serverKeys, {
  45. 'Misc': 'Misc',
  46. 'Unknown': 'Unknown'
  47. })
  48. const serverTranslations = {
  49. target: join(__dirname, '../../../client/src/locale/source/server_en_US.xml'),
  50. data: serverKeys
  51. }
  52. // ISO 639 keys
  53. const languageKeys: any = {}
  54. const languages = buildLanguages()
  55. Object.keys(languages).forEach(k => languageKeys[languages[k]] = languages[k])
  56. const iso639Translations = {
  57. target: join(__dirname, '../../../client/src/locale/source/iso639_en_US.xml'),
  58. data: languageKeys
  59. }
  60. saveToXliffFile(playerTranslations, err => {
  61. if (err) return handleError(err)
  62. saveToXliffFile(serverTranslations, err => {
  63. if (err) return handleError(err)
  64. saveToXliffFile(iso639Translations, err => {
  65. if (err) return handleError(err)
  66. process.exit(0)
  67. })
  68. })
  69. })
  70. // Then, the server strings
  71. function saveToXliffFile (jsonTranslations: TranslationType, cb: Function) {
  72. const obj = {
  73. resources: {
  74. namespace1: {}
  75. }
  76. }
  77. Object.keys(jsonTranslations.data).forEach(k => obj.resources.namespace1[ k ] = { source: jsonTranslations.data[ k ] })
  78. jsToXliff12(obj, (err, res) => {
  79. if (err) return cb(err)
  80. writeFile(jsonTranslations.target, res, err => {
  81. if (err) return cb(err)
  82. return cb(null)
  83. })
  84. })
  85. }
  86. function handleError (err: any) {
  87. console.error(err)
  88. process.exit(-1)
  89. }