From db3f91cbcc08f6279e2651db4d03ad27e99b0c1a Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Wed, 24 May 2017 16:44:33 +0700 Subject: [PATCH] Change to declarative API for plugins --- client/coral-framework/helpers/plugins.js | 6 ++++++ client/coral-framework/services/i18n.js | 10 ++++++++++ plugins/coral-plugin-like/client/index.js | 5 +---- plugins/coral-plugin-love/client/index.js | 5 +---- plugins/coral-plugin-offtopic/client/index.js | 4 +--- plugins/coral-plugin-respect/client/index.js | 5 +---- 6 files changed, 20 insertions(+), 15 deletions(-) diff --git a/client/coral-framework/helpers/plugins.js b/client/coral-framework/helpers/plugins.js index f14963833..a70c6f6f6 100644 --- a/client/coral-framework/helpers/plugins.js +++ b/client/coral-framework/helpers/plugins.js @@ -88,3 +88,9 @@ export function getGraphQLExtensions() { .filter((o) => o); } +export function getTranslations() { + return plugins + .map((o) => o.module.translations) + .filter((o) => o); +} + diff --git a/client/coral-framework/services/i18n.js b/client/coral-framework/services/i18n.js index 6a9b07d05..882127972 100644 --- a/client/coral-framework/services/i18n.js +++ b/client/coral-framework/services/i18n.js @@ -6,6 +6,7 @@ import merge from 'lodash/merge'; import esTA from '../../../node_modules/timeago.js/locales/es'; import en from '../../../locales/en.yml'; import es from '../../../locales/es.yml'; +import * as plugins from '../helpers/plugins'; // Translations are happening at https://www.transifex.com/the-coral-project/talk-1/dashboard/. @@ -14,6 +15,7 @@ const translations = {...en, ...es}; let lang; let timeagoInstance; +let loadedPluginsTranslations = false; function setLocale(locale) { try { @@ -43,6 +45,10 @@ function init() { timeagoInstance = ta(); } +function loadPluginsTranslations() { + plugins.getTranslations().forEach((t) => loadTranslations(t)); +} + export function loadTranslations(newTranslations) { merge(translations, newTranslations); } @@ -61,6 +67,10 @@ export function timeago(time) { * any extra parameters are optional and replace a variable marked by {0}, {1}, etc in the translation. */ export function t(key, ...replacements) { + if (!loadedPluginsTranslations) { + loadPluginsTranslations(); + loadedPluginsTranslations = true; + } const fullKey = `${lang}.${key}`; if (has(translations, fullKey)) { let translation = get(translations, fullKey); diff --git a/plugins/coral-plugin-like/client/index.js b/plugins/coral-plugin-like/client/index.js index a67d3eb84..86d20863a 100644 --- a/plugins/coral-plugin-like/client/index.js +++ b/plugins/coral-plugin-like/client/index.js @@ -1,11 +1,8 @@ import LikeButton from './containers/LikeButton'; - import translations from './translations.json'; -import {loadTranslations} from '/coral-framework/services/i18n'; - -loadTranslations(translations); export default { + translations, slots: { commentReactions: [LikeButton] } diff --git a/plugins/coral-plugin-love/client/index.js b/plugins/coral-plugin-love/client/index.js index 38f32c1cb..fa2f71159 100644 --- a/plugins/coral-plugin-love/client/index.js +++ b/plugins/coral-plugin-love/client/index.js @@ -1,11 +1,8 @@ import LoveButton from './LoveButton'; - import translations from './translations.json'; -import {loadTranslations} from '/coral-framework/services/i18n'; - -loadTranslations(translations); export default { + translations, slots: { commentReactions: [LoveButton] } diff --git a/plugins/coral-plugin-offtopic/client/index.js b/plugins/coral-plugin-offtopic/client/index.js index c485c65f2..f3459441d 100644 --- a/plugins/coral-plugin-offtopic/client/index.js +++ b/plugins/coral-plugin-offtopic/client/index.js @@ -1,11 +1,9 @@ import OffTopicCheckbox from './components/OffTopicCheckbox'; import OffTopicTag from './components/OffTopicTag'; import translations from './translations.json'; -import {loadTranslations} from 'coral-framework/services/i18n'; - -loadTranslations(translations); export default { + translations, slots: { commentInputDetailArea: [OffTopicCheckbox], commentInfoBar: [OffTopicTag] diff --git a/plugins/coral-plugin-respect/client/index.js b/plugins/coral-plugin-respect/client/index.js index 4fe209940..4df7c66ec 100644 --- a/plugins/coral-plugin-respect/client/index.js +++ b/plugins/coral-plugin-respect/client/index.js @@ -1,11 +1,8 @@ import RespectButton from './containers/RespectButton'; - import translations from './translations.json'; -import {loadTranslations} from 'coral-framework/services/i18n'; - -loadTranslations(translations); export default { + translations, slots: { commentActions: [RespectButton], }