diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-26 14:44:50 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-07-26 15:18:30 +0200 |
commit | d75db01f14138ea660c4c519e37ab05228b39d13 (patch) | |
tree | 85a3da315ea6e1501fec5b70790482504dd64793 /server/lib/plugins/plugin-manager.ts | |
parent | ee286591a5b740702bad66c55cc900740f749e9a (diff) | |
download | PeerTube-d75db01f14138ea660c4c519e37ab05228b39d13.tar.gz PeerTube-d75db01f14138ea660c4c519e37ab05228b39d13.tar.zst PeerTube-d75db01f14138ea660c4c519e37ab05228b39d13.zip |
Add plugin translation system
Diffstat (limited to 'server/lib/plugins/plugin-manager.ts')
-rw-r--r-- | server/lib/plugins/plugin-manager.ts | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts index 81554a09e..c9beae268 100644 --- a/server/lib/plugins/plugin-manager.ts +++ b/server/lib/plugins/plugin-manager.ts | |||
@@ -3,7 +3,11 @@ import { logger } from '../../helpers/logger' | |||
3 | import { basename, join } from 'path' | 3 | import { basename, join } from 'path' |
4 | import { CONFIG } from '../../initializers/config' | 4 | import { CONFIG } from '../../initializers/config' |
5 | import { isLibraryCodeValid, isPackageJSONValid } from '../../helpers/custom-validators/plugins' | 5 | import { isLibraryCodeValid, isPackageJSONValid } from '../../helpers/custom-validators/plugins' |
6 | import { ClientScript, PluginPackageJson } from '../../../shared/models/plugins/plugin-package-json.model' | 6 | import { |
7 | ClientScript, | ||
8 | PluginPackageJson, | ||
9 | PluginTranslationPaths as PackagePluginTranslations | ||
10 | } from '../../../shared/models/plugins/plugin-package-json.model' | ||
7 | import { createReadStream, createWriteStream } from 'fs' | 11 | import { createReadStream, createWriteStream } from 'fs' |
8 | import { PLUGIN_GLOBAL_CSS_PATH, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES } from '../../initializers/constants' | 12 | import { PLUGIN_GLOBAL_CSS_PATH, VIDEO_CATEGORIES, VIDEO_LANGUAGES, VIDEO_LICENCES } from '../../initializers/constants' |
9 | import { PluginType } from '../../../shared/models/plugins/plugin.type' | 13 | import { PluginType } from '../../../shared/models/plugins/plugin.type' |
@@ -21,6 +25,7 @@ import { RegisterServerSettingOptions } from '../../../shared/models/plugins/reg | |||
21 | import { PluginVideoLanguageManager } from '../../../shared/models/plugins/plugin-video-language-manager.model' | 25 | import { PluginVideoLanguageManager } from '../../../shared/models/plugins/plugin-video-language-manager.model' |
22 | import { PluginVideoCategoryManager } from '../../../shared/models/plugins/plugin-video-category-manager.model' | 26 | import { PluginVideoCategoryManager } from '../../../shared/models/plugins/plugin-video-category-manager.model' |
23 | import { PluginVideoLicenceManager } from '../../../shared/models/plugins/plugin-video-licence-manager.model' | 27 | import { PluginVideoLicenceManager } from '../../../shared/models/plugins/plugin-video-licence-manager.model' |
28 | import { PluginTranslation } from '../../../shared/models/plugins/plugin-translation.model' | ||
24 | 29 | ||
25 | export interface RegisteredPlugin { | 30 | export interface RegisteredPlugin { |
26 | npmName: string | 31 | npmName: string |
@@ -60,6 +65,10 @@ type UpdatedVideoConstant = { | |||
60 | } | 65 | } |
61 | } | 66 | } |
62 | 67 | ||
68 | type PluginLocalesTranslations = { | ||
69 | [ locale: string ]: PluginTranslation | ||
70 | } | ||
71 | |||
63 | export class PluginManager implements ServerHook { | 72 | export class PluginManager implements ServerHook { |
64 | 73 | ||
65 | private static instance: PluginManager | 74 | private static instance: PluginManager |
@@ -67,6 +76,7 @@ export class PluginManager implements ServerHook { | |||
67 | private registeredPlugins: { [ name: string ]: RegisteredPlugin } = {} | 76 | private registeredPlugins: { [ name: string ]: RegisteredPlugin } = {} |
68 | private settings: { [ name: string ]: RegisterServerSettingOptions[] } = {} | 77 | private settings: { [ name: string ]: RegisterServerSettingOptions[] } = {} |
69 | private hooks: { [ name: string ]: HookInformationValue[] } = {} | 78 | private hooks: { [ name: string ]: HookInformationValue[] } = {} |
79 | private translations: PluginLocalesTranslations = {} | ||
70 | 80 | ||
71 | private updatedVideoConstants: UpdatedVideoConstant = { | 81 | private updatedVideoConstants: UpdatedVideoConstant = { |
72 | language: {}, | 82 | language: {}, |
@@ -117,6 +127,10 @@ export class PluginManager implements ServerHook { | |||
117 | return this.settings[npmName] || [] | 127 | return this.settings[npmName] || [] |
118 | } | 128 | } |
119 | 129 | ||
130 | getTranslations (locale: string) { | ||
131 | return this.translations[locale] || {} | ||
132 | } | ||
133 | |||
120 | // ###################### Hooks ###################### | 134 | // ###################### Hooks ###################### |
121 | 135 | ||
122 | async runHook <T> (hookName: ServerHookName, result?: T, params?: any): Promise<T> { | 136 | async runHook <T> (hookName: ServerHookName, result?: T, params?: any): Promise<T> { |
@@ -173,6 +187,8 @@ export class PluginManager implements ServerHook { | |||
173 | delete this.registeredPlugins[plugin.npmName] | 187 | delete this.registeredPlugins[plugin.npmName] |
174 | delete this.settings[plugin.npmName] | 188 | delete this.settings[plugin.npmName] |
175 | 189 | ||
190 | this.deleteTranslations(plugin.npmName) | ||
191 | |||
176 | if (plugin.type === PluginType.PLUGIN) { | 192 | if (plugin.type === PluginType.PLUGIN) { |
177 | await plugin.unregister() | 193 | await plugin.unregister() |
178 | 194 | ||
@@ -312,6 +328,8 @@ export class PluginManager implements ServerHook { | |||
312 | css: packageJSON.css, | 328 | css: packageJSON.css, |
313 | unregister: library ? library.unregister : undefined | 329 | unregister: library ? library.unregister : undefined |
314 | } | 330 | } |
331 | |||
332 | await this.addTranslations(plugin, npmName, packageJSON.translations) | ||
315 | } | 333 | } |
316 | 334 | ||
317 | private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJson) { | 335 | private async registerPlugin (plugin: PluginModel, pluginPath: string, packageJSON: PluginPackageJson) { |
@@ -337,6 +355,28 @@ export class PluginManager implements ServerHook { | |||
337 | return library | 355 | return library |
338 | } | 356 | } |
339 | 357 | ||
358 | // ###################### Translations ###################### | ||
359 | |||
360 | private async addTranslations (plugin: PluginModel, npmName: string, translationPaths: PackagePluginTranslations) { | ||
361 | for (const locale of Object.keys(translationPaths)) { | ||
362 | const path = translationPaths[locale] | ||
363 | const json = await readJSON(join(this.getPluginPath(plugin.name, plugin.type), path)) | ||
364 | |||
365 | if (!this.translations[locale]) this.translations[locale] = {} | ||
366 | this.translations[locale][npmName] = json | ||
367 | |||
368 | logger.info('Added locale %s of plugin %s.', locale, npmName) | ||
369 | } | ||
370 | } | ||
371 | |||
372 | private deleteTranslations (npmName: string) { | ||
373 | for (const locale of Object.keys(this.translations)) { | ||
374 | delete this.translations[locale][npmName] | ||
375 | |||
376 | logger.info('Deleted locale %s of plugin %s.', locale, npmName) | ||
377 | } | ||
378 | } | ||
379 | |||
340 | // ###################### CSS ###################### | 380 | // ###################### CSS ###################### |
341 | 381 | ||
342 | private resetCSSGlobalFile () { | 382 | private resetCSSGlobalFile () { |
@@ -455,7 +495,7 @@ export class PluginManager implements ServerHook { | |||
455 | deleteLanguage: (key: string) => this.deleteConstant({ npmName, type: 'language', obj: VIDEO_LANGUAGES, key }) | 495 | deleteLanguage: (key: string) => this.deleteConstant({ npmName, type: 'language', obj: VIDEO_LANGUAGES, key }) |
456 | } | 496 | } |
457 | 497 | ||
458 | const videoCategoryManager: PluginVideoCategoryManager= { | 498 | const videoCategoryManager: PluginVideoCategoryManager = { |
459 | addCategory: (key: number, label: string) => this.addConstant({ npmName, type: 'category', obj: VIDEO_CATEGORIES, key, label }), | 499 | addCategory: (key: number, label: string) => this.addConstant({ npmName, type: 'category', obj: VIDEO_CATEGORIES, key, label }), |
460 | 500 | ||
461 | deleteCategory: (key: number) => this.deleteConstant({ npmName, type: 'category', obj: VIDEO_CATEGORIES, key }) | 501 | deleteCategory: (key: number) => this.deleteConstant({ npmName, type: 'category', obj: VIDEO_CATEGORIES, key }) |