Moved theme i18n to new theme engine service

refs: bf0823c9a2

- 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.
This commit is contained in:
Hannah Wolfe 2021-04-26 11:53:15 +01:00
parent 5212187fd0
commit 9614d71e1f
7 changed files with 18 additions and 27 deletions

View File

@ -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,

View File

@ -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

View File

@ -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;

View File

@ -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');

View File

@ -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

View File

@ -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() {

View File

@ -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 () {