From 9614d71e1f9c6225a60f9b382898fda726decb4a Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Mon, 26 Apr 2021 11:53:15 +0100 Subject: [PATCH] Moved theme i18n to new theme engine service refs: https://github.com/TryGhost/Ghost/commit/bf0823c9a2ddbc93ad0ddcec36eed130c8c5a203 - continuing the work of splitting up the theme service into logical components - This one is a little more involved, as the i18n initialisation was unnecessarily spread over several locations. - I moved it into being part of the ActiveTheme class and called in the constructor, meaning we don't need the services.theme.activated event anymore as the constructor is called in the same cases. - Also moved the event listener for locales into the bridge, as I don't want that inside of theme-engine, and we don't want circular dependencies. We'll figure out a wayto refactor this soon too. --- core/frontend/services/proxy.js | 2 +- core/frontend/services/theme-engine/active.js | 7 +++++++ .../services/{themes => theme-engine}/i18n.js | 20 +------------------ core/frontend/services/themes/activate.js | 2 -- core/frontend/services/themes/index.js | 3 --- core/shared/bridge.js | 9 ++++++++- test/unit/helpers/t_spec.js | 2 +- 7 files changed, 18 insertions(+), 27 deletions(-) rename core/frontend/services/{themes => theme-engine}/i18n.js (84%) diff --git a/core/frontend/services/proxy.js b/core/frontend/services/proxy.js index 8f96e3d5fd..eefb4d3828 100644 --- a/core/frontend/services/proxy.js +++ b/core/frontend/services/proxy.js @@ -33,7 +33,7 @@ module.exports = { logging, // Theme i18n is separate to common i18n - themeI18n: require('./themes/i18n'), + themeI18n: require('./theme-engine/i18n'), // This is used to detect if "isPost" is true in prevNext. checks: require('../../server/data/schema').checks, diff --git a/core/frontend/services/theme-engine/active.js b/core/frontend/services/theme-engine/active.js index a9b00ab4f3..034bcd5bf9 100644 --- a/core/frontend/services/theme-engine/active.js +++ b/core/frontend/services/theme-engine/active.js @@ -18,6 +18,7 @@ const themeConfig = require('./config'); const themeEngines = require('./engines'); const config = require('../../../shared/config'); const engine = require('./engine'); +const themeI18n = require('./i18n'); // Current instance of ActiveTheme let currentActiveTheme; @@ -51,6 +52,8 @@ class ActiveTheme { // Create a theme engines object this._engines = themeEngines.create(this._packageInfo); + + this.initI18n(); } get name() { @@ -93,6 +96,10 @@ class ActiveTheme { return this._engines[key]; } + initI18n() { + themeI18n.init(this._name); + } + mount(siteApp) { // reset the asset hash // @TODO: set this on the theme instead of globally, or use proper file-based hash diff --git a/core/frontend/services/themes/i18n.js b/core/frontend/services/theme-engine/i18n.js similarity index 84% rename from core/frontend/services/themes/i18n.js rename to core/frontend/services/theme-engine/i18n.js index 926d1d6067..2b53a5c853 100644 --- a/core/frontend/services/themes/i18n.js +++ b/core/frontend/services/theme-engine/i18n.js @@ -1,9 +1,8 @@ const errors = require('@tryghost/errors'); -const {i18n, events} = require('../../../server/lib/common'); +const {i18n} = require('../../../server/lib/common'); const logging = require('../../../shared/logging'); const settingsCache = require('../../../server/services/settings/cache'); const config = require('../../../shared/config'); -const active = require('../theme-engine/active'); const jp = require('jsonpath'); const isNil = require('lodash/isNil'); @@ -96,21 +95,4 @@ class ThemeI18n extends i18n.I18n { let themeI18n = new ThemeI18n(); -// /** -// * When active theme changes, we reload theme translations -// * We listen on the service event, because of the following known case: -// * 1. you override a theme, which is already active -// * 2. The data has not changed, no event is triggered. -// */ -events.on('services.themes.activated', function (activeTheme) { - themeI18n.init(activeTheme); -}); - -/** - * When locale changes, we reload theme translations - */ -events.on('settings.lang.edited', function () { - themeI18n.init(active.get().name); -}); - module.exports = themeI18n; diff --git a/core/frontend/services/themes/activate.js b/core/frontend/services/themes/activate.js index 3a7bd3da0a..4c5861b9f4 100644 --- a/core/frontend/services/themes/activate.js +++ b/core/frontend/services/themes/activate.js @@ -16,8 +16,6 @@ function activate(loadedTheme, checkedTheme, error) { active.set(loadedTheme, checkedTheme, error); const currentGhostAPI = active.get().engine('ghost-api'); - events.emit('services.themes.activated', loadedTheme.name); - if (previousGhostAPI !== undefined && (previousGhostAPI !== currentGhostAPI)) { events.emit('services.themes.api.changed'); const siteApp = require('../../../server/web/site/app'); diff --git a/core/frontend/services/themes/index.js b/core/frontend/services/themes/index.js index 5e92f3926c..a1c35bd01d 100644 --- a/core/frontend/services/themes/index.js +++ b/core/frontend/services/themes/index.js @@ -6,7 +6,6 @@ const errors = require('@tryghost/errors'); const themeLoader = require('./loader'); const activate = require('./activate'); const validate = require('./validate'); -const i18n = require('./i18n'); const list = require('./list'); const settingsCache = require('../../../server/services/settings/cache'); @@ -16,8 +15,6 @@ module.exports = { init: function initThemes() { const activeThemeName = settingsCache.get('active_theme'); - i18n.init(activeThemeName); - debug('init themes', activeThemeName); // Just read the active theme for now return themeLoader diff --git a/core/shared/bridge.js b/core/shared/bridge.js index fe8f370427..8fe139a0fb 100644 --- a/core/shared/bridge.js +++ b/core/shared/bridge.js @@ -1,10 +1,17 @@ // @TODO: refactor constructor pattern so we don't have to require config here? const config = require('./config'); +const {events} = require('../server/lib/common'); const themeEngine = require('../frontend/services/theme-engine'); class Bridge { constructor() { - + /** + * When locale changes, we reload theme translations + * @deprecated: the term "lang" was deprecated in favour of "locale" publicly 4.0 + */ + events.on('settings.lang.edited', () => { + this.getActiveTheme().initI18n(); + }); } getActiveTheme() { diff --git a/test/unit/helpers/t_spec.js b/test/unit/helpers/t_spec.js index 47c55cb0e5..29f01bd454 100644 --- a/test/unit/helpers/t_spec.js +++ b/test/unit/helpers/t_spec.js @@ -2,7 +2,7 @@ const should = require('should'); const path = require('path'); const settingsCache = require('../../../core/server/services/settings/cache'); const helpers = require('../../../core/frontend/helpers'); -const themeI18n = require('../../../core/frontend/services/themes/i18n'); +const themeI18n = require('../../../core/frontend/services/theme-engine/i18n'); const configUtils = require('../../utils/configUtils'); describe('{{t}} helper', function () {