From d75db01f14138ea660c4c519e37ab05228b39d13 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 26 Jul 2019 14:44:50 +0200 Subject: Add plugin translation system --- .../+admin/plugins/shared/plugin-api.service.ts | 25 +++++++++++-- client/src/app/core/plugins/plugin.service.ts | 42 +++++++++++++++++----- client/src/app/shared/video/video.service.ts | 1 - client/src/types/register-client-option.model.ts | 12 ++++--- 4 files changed, 64 insertions(+), 16 deletions(-) (limited to 'client') diff --git a/client/src/app/+admin/plugins/shared/plugin-api.service.ts b/client/src/app/+admin/plugins/shared/plugin-api.service.ts index c360fc1b3..f6ef68e9c 100644 --- a/client/src/app/+admin/plugins/shared/plugin-api.service.ts +++ b/client/src/app/+admin/plugins/shared/plugin-api.service.ts @@ -1,4 +1,4 @@ -import { catchError } from 'rxjs/operators' +import { catchError, map, switchMap } from 'rxjs/operators' import { HttpClient, HttpParams } from '@angular/common/http' import { Injectable } from '@angular/core' import { environment } from '../../../../environments/environment' @@ -6,13 +6,14 @@ import { RestExtractor, RestService } from '../../../shared' import { I18n } from '@ngx-translate/i18n-polyfill' import { PluginType } from '@shared/models/plugins/plugin.type' import { ComponentPagination } from '@app/shared/rest/component-pagination.model' -import { ResultList } from '@shared/models' +import { peertubeTranslate, ResultList } from '@shared/models' import { PeerTubePlugin } from '@shared/models/plugins/peertube-plugin.model' import { ManagePlugin } from '@shared/models/plugins/manage-plugin.model' import { InstallOrUpdatePlugin } from '@shared/models/plugins/install-plugin.model' import { PeerTubePluginIndex } from '@shared/models/plugins/peertube-plugin-index.model' import { RegisteredServerSettings, RegisterServerSettingOptions } from '@shared/models/plugins/register-server-setting.model' import { PluginService } from '@app/core/plugins/plugin.service' +import { Observable } from 'rxjs' @Injectable() export class PluginApiService { @@ -92,7 +93,10 @@ export class PluginApiService { const path = PluginApiService.BASE_PLUGIN_URL + '/' + npmName + '/registered-settings' return this.authHttp.get(path) - .pipe(catchError(res => this.restExtractor.handleError(res))) + .pipe( + switchMap(res => this.translateSettingsLabel(npmName, res)), + catchError(res => this.restExtractor.handleError(res)) + ) } updatePluginSettings (pluginName: string, pluginType: PluginType, settings: any) { @@ -129,4 +133,19 @@ export class PluginApiService { return this.authHttp.post(PluginApiService.BASE_PLUGIN_URL + '/install', body) .pipe(catchError(res => this.restExtractor.handleError(res))) } + + private translateSettingsLabel (npmName: string, res: RegisteredServerSettings): Observable { + return this.pluginService.translationsObservable + .pipe( + map(allTranslations => allTranslations[npmName]), + map(translations => { + const registeredSettings = res.registeredSettings + .map(r => { + return Object.assign({}, r, { label: peertubeTranslate(r.label, translations) }) + }) + + return { registeredSettings } + }) + ) + } } diff --git a/client/src/app/core/plugins/plugin.service.ts b/client/src/app/core/plugins/plugin.service.ts index cca779177..3bb82e8a9 100644 --- a/client/src/app/core/plugins/plugin.service.ts +++ b/client/src/app/core/plugins/plugin.service.ts @@ -1,11 +1,11 @@ -import { Injectable, NgZone } from '@angular/core' +import { Inject, Injectable, LOCALE_ID, NgZone } from '@angular/core' import { Router } from '@angular/router' -import { ServerConfigPlugin } from '@shared/models' +import { getCompleteLocale, isDefaultLocale, peertubeTranslate, ServerConfigPlugin } from '@shared/models' import { ServerService } from '@app/core/server/server.service' import { ClientScript } from '@shared/models/plugins/plugin-package-json.model' import { ClientScript as ClientScriptModule } from '../../../types/client-script.model' import { environment } from '../../../environments/environment' -import { ReplaySubject } from 'rxjs' +import { Observable, of, ReplaySubject } from 'rxjs' import { catchError, first, map, shareReplay } from 'rxjs/operators' import { getHookType, internalRunHook } from '@shared/core-utils/plugins/hooks' import { ClientHook, ClientHookName, clientHookObject } from '@shared/models/plugins/client-hook.model' @@ -15,6 +15,9 @@ import { HttpClient } from '@angular/common/http' import { RestExtractor } from '@app/shared/rest' import { PluginType } from '@shared/models/plugins/plugin.type' import { PublicServerSetting } from '@shared/models/plugins/public-server.setting' +import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils' +import { RegisterClientHelpers } from '../../../types/register-client-option.model' +import { PluginTranslation } from '@shared/models/plugins/plugin-translation.model' interface HookStructValue extends RegisterClientHookOptions { plugin: ServerConfigPlugin @@ -30,7 +33,8 @@ type PluginInfo = { @Injectable() export class PluginService implements ClientHook { - private static BASE_PLUGIN_URL = environment.apiUrl + '/api/v1/plugins' + private static BASE_PLUGIN_API_URL = environment.apiUrl + '/api/v1/plugins' + private static BASE_PLUGIN_URL = environment.apiUrl + '/plugins' pluginsBuilt = new ReplaySubject(1) @@ -40,6 +44,8 @@ export class PluginService implements ClientHook { 'video-watch': new ReplaySubject(1) } + translationsObservable: Observable + private plugins: ServerConfigPlugin[] = [] private scopes: { [ scopeName: string ]: PluginInfo[] } = {} private loadedScripts: { [ script: string ]: boolean } = {} @@ -53,8 +59,10 @@ export class PluginService implements ClientHook { private server: ServerService, private zone: NgZone, private authHttp: HttpClient, - private restExtractor: RestExtractor + private restExtractor: RestExtractor, + @Inject(LOCALE_ID) private localeId: string ) { + this.loadTranslations() } initializePlugins () { @@ -235,8 +243,9 @@ export class PluginService implements ClientHook { } } - private buildPeerTubeHelpers (pluginInfo: PluginInfo) { + private buildPeerTubeHelpers (pluginInfo: PluginInfo): RegisterClientHelpers { const { plugin } = pluginInfo + const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType) return { getBaseStaticRoute: () => { @@ -245,8 +254,7 @@ export class PluginService implements ClientHook { }, getSettings: () => { - const npmName = this.nameToNpmName(pluginInfo.plugin.name, pluginInfo.pluginType) - const path = PluginService.BASE_PLUGIN_URL + '/' + npmName + '/public-settings' + const path = PluginService.BASE_PLUGIN_API_URL + '/' + npmName + '/public-settings' return this.authHttp.get(path) .pipe( @@ -254,10 +262,28 @@ export class PluginService implements ClientHook { catchError(res => this.restExtractor.handleError(res)) ) .toPromise() + }, + + translate: (value: string) => { + return this.translationsObservable + .pipe(map(allTranslations => allTranslations[npmName])) + .pipe(map(translations => peertubeTranslate(value, translations))) + .toPromise() } } } + private loadTranslations () { + const completeLocale = isOnDevLocale() ? getDevLocale() : getCompleteLocale(this.localeId) + + // Default locale, nothing to translate + if (isDefaultLocale(completeLocale)) this.translationsObservable = of({}).pipe(shareReplay()) + + this.translationsObservable = this.authHttp + .get(PluginService.BASE_PLUGIN_URL + '/translations/' + completeLocale + '.json') + .pipe(shareReplay()) + } + private getPluginPathPrefix (isTheme: boolean) { return isTheme ? '/themes' : '/plugins' } diff --git a/client/src/app/shared/video/video.service.ts b/client/src/app/shared/video/video.service.ts index d1af13c93..114b014ad 100644 --- a/client/src/app/shared/video/video.service.ts +++ b/client/src/app/shared/video/video.service.ts @@ -31,7 +31,6 @@ import { ServerService } from '@app/core' import { UserSubscriptionService } from '@app/shared/user-subscription/user-subscription.service' import { VideoChannel } from '@app/shared/video-channel/video-channel.model' import { I18n } from '@ngx-translate/i18n-polyfill' -import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model' import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service' export interface VideosProvider { diff --git a/client/src/types/register-client-option.model.ts b/client/src/types/register-client-option.model.ts index 473c2500f..243d74dea 100644 --- a/client/src/types/register-client-option.model.ts +++ b/client/src/types/register-client-option.model.ts @@ -3,9 +3,13 @@ import { RegisterClientHookOptions } from '@shared/models/plugins/register-clien export type RegisterClientOptions = { registerHook: (options: RegisterClientHookOptions) => void - peertubeHelpers: { - getBaseStaticRoute: () => string + peertubeHelpers: RegisterClientHelpers +} + +export type RegisterClientHelpers = { + getBaseStaticRoute: () => string + + getSettings: () => Promise<{ [ name: string ]: string }> - getSettings: () => Promise<{ [ name: string ]: string }> - } + translate: (toTranslate: string) => Promise } -- cgit v1.2.3