From 67baf6478a56e4fabb22da27ee91783add7ed369 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 20 Apr 2021 16:14:09 +0200 Subject: Allow plugins to set short translation locale --- server/lib/plugins/plugin-manager.ts | 9 ++++++--- shared/core-utils/i18n/i18n.ts | 6 +++--- support/doc/plugins/guide.md | 3 +-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/server/lib/plugins/plugin-manager.ts b/server/lib/plugins/plugin-manager.ts index f3766ffa8..ae05af721 100644 --- a/server/lib/plugins/plugin-manager.ts +++ b/server/lib/plugins/plugin-manager.ts @@ -23,6 +23,7 @@ import { PluginLibrary, RegisterServerAuthExternalOptions, RegisterServerAuthPas import { ClientHtml } from '../client-html' import { RegisterHelpers } from './register-helpers' import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn' +import { getCompleteLocale } from '@shared/core-utils' export interface RegisteredPlugin { npmName: string @@ -444,10 +445,12 @@ export class PluginManager implements ServerHook { const path = translationPaths[locale] const json = await readJSON(join(this.getPluginPath(plugin.name, plugin.type), path)) - if (!this.translations[locale]) this.translations[locale] = {} - this.translations[locale][npmName] = json + const completeLocale = getCompleteLocale(locale) - logger.info('Added locale %s of plugin %s.', locale, npmName) + if (!this.translations[completeLocale]) this.translations[completeLocale] = {} + this.translations[completeLocale][npmName] = json + + logger.info('Added locale %s of plugin %s.', completeLocale, npmName) } } diff --git a/shared/core-utils/i18n/i18n.ts b/shared/core-utils/i18n/i18n.ts index a342cb3f8..f27de20f1 100644 --- a/shared/core-utils/i18n/i18n.ts +++ b/shared/core-utils/i18n/i18n.ts @@ -75,9 +75,9 @@ export function isDefaultLocale (locale: string) { } export function peertubeTranslate (str: string, translations?: { [ id: string ]: string }) { - // FIXME: remove disable rule when the client is upgraded to typescript 3.7 - // eslint-disable-next-line - return translations && translations[str] ? translations[str] : str + if (!translations || !translations[str]) return str + + return translations[str] } const possiblePaths = POSSIBLE_LOCALES.map(l => '/' + l) diff --git a/support/doc/plugins/guide.md b/support/doc/plugins/guide.md index 17c681afd..5b5a3065d 100644 --- a/support/doc/plugins/guide.md +++ b/support/doc/plugins/guide.md @@ -744,7 +744,7 @@ If you want to translate strings of your plugin (like labels of your registered { ..., "translations": { - "fr-FR": "./languages/fr.json", + "fr": "./languages/fr.json", "pt-BR": "./languages/pt-BR.json" }, ... @@ -752,7 +752,6 @@ If you want to translate strings of your plugin (like labels of your registered ``` The key should be one of the locales defined in [i18n.ts](https://github.com/Chocobozzz/PeerTube/blob/develop/shared/models/i18n/i18n.ts). -You **must** use the complete locales (`fr-FR` instead of `fr`). Translation files are just objects, with the english sentence as the key and the translation as the value. `fr.json` could contain for example: -- cgit v1.2.3