]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/src/app/core/plugins/plugin.service.ts
Translated using Weblate (German)
[github/Chocobozzz/PeerTube.git] / client / src / app / core / plugins / plugin.service.ts
index fdbbd2d56dcfcc7940122ff947a39e0fb0d608f7..dadc2a41d3d9a719e2f35cacb4bed93d19bac212 100644 (file)
@@ -11,6 +11,7 @@ import { ServerService } from '@app/core/server/server.service'
 import { getDevLocale, isOnDevLocale } from '@app/helpers'
 import { CustomModalComponent } from '@app/modal/custom-modal.component'
 import { PluginInfo, PluginsManager } from '@root-helpers/plugins-manager'
+import { getKeys } from '@shared/core-utils'
 import { getCompleteLocale, isDefaultLocale, peertubeTranslate } from '@shared/core-utils/i18n'
 import {
   ClientHook,
@@ -20,8 +21,8 @@ import {
   PluginType,
   PublicServerSetting,
   RegisterClientFormFieldOptions,
-  RegisterClientSettingsScriptOptions,
   RegisterClientRouteOptions,
+  RegisterClientSettingsScriptOptions,
   RegisterClientVideoFieldOptions,
   ServerConfigPlugin
 } from '@shared/models'
@@ -30,6 +31,7 @@ import { RegisterClientHelpers } from '../../../types/register-client-option.mod
 
 type FormFields = {
   video: {
+    pluginInfo: PluginInfo
     commonOptions: RegisterClientFormFieldOptions
     videoFormOptions: RegisterClientVideoFieldOptions
   }[]
@@ -44,8 +46,6 @@ export class PluginService implements ClientHook {
 
   customModal: CustomModalComponent
 
-  private helpers: { [ npmName: string ]: RegisterClientHelpers } = {}
-
   private formFields: FormFields = {
     video: []
   }
@@ -134,27 +134,49 @@ export class PluginService implements ClientHook {
     return Object.keys(this.clientRoutes)
   }
 
-  translateBy (npmName: string, toTranslate: string) {
-    const helpers = this.helpers[npmName]
-    if (!helpers) {
-      console.error('Unknown helpers to translate %s from %s.', toTranslate, npmName)
-      return toTranslate
+  async translateSetting (npmName: string, setting: RegisterClientFormFieldOptions) {
+    for (const key of getKeys(setting, [ 'label', 'html', 'descriptionHTML' ])) {
+      if (setting[key]) setting[key] = await this.translateBy(npmName, setting[key])
     }
 
-    return helpers.translate(toTranslate)
+    if (Array.isArray(setting.options)) {
+      const newOptions = []
+
+      for (const o of setting.options) {
+        newOptions.push({
+          value: o.value,
+          label: await this.translateBy(npmName, o.label)
+        })
+      }
+
+      setting.options = newOptions
+    }
   }
 
-  private onFormFields (commonOptions: RegisterClientFormFieldOptions, videoFormOptions: RegisterClientVideoFieldOptions) {
+  translateBy (npmName: string, toTranslate: string) {
+    const obs = this.translationsObservable
+        .pipe(
+          map(allTranslations => allTranslations[npmName]),
+          map(translations => peertubeTranslate(toTranslate, translations))
+        )
+
+    return firstValueFrom(obs)
+  }
+
+  private onFormFields (
+    pluginInfo: PluginInfo,
+    commonOptions: RegisterClientFormFieldOptions,
+    videoFormOptions: RegisterClientVideoFieldOptions
+  ) {
     this.formFields.video.push({
+      pluginInfo,
       commonOptions,
       videoFormOptions
     })
   }
 
   private onSettingsScripts (pluginInfo: PluginInfo, options: RegisterClientSettingsScriptOptions) {
-    const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType)
-
-    this.settingsScripts[npmName] = options
+    this.settingsScripts[pluginInfo.plugin.npmName] = options
   }
 
   private onClientRoute (options: RegisterClientRouteOptions) {
@@ -167,7 +189,7 @@ export class PluginService implements ClientHook {
 
   private buildPeerTubeHelpers (pluginInfo: PluginInfo): RegisterClientHelpers {
     const { plugin } = pluginInfo
-    const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType)
+    const npmName = pluginInfo.plugin.npmName
 
     return {
       getBaseStaticRoute: () => {
@@ -241,11 +263,7 @@ export class PluginService implements ClientHook {
       },
 
       translate: (value: string) => {
-        const obs = this.translationsObservable
-            .pipe(map(allTranslations => allTranslations[npmName]))
-            .pipe(map(translations => peertubeTranslate(value, translations)))
-
-        return firstValueFrom(obs)
+        return this.translateBy(npmName, value)
       }
     }
   }