mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
🐛 Fixed incorrect locale loading when changing themes
closes #12271 - When previous active theme did not have locale data for certain language, loading a theme which has such data did not result in correct locale being loaded - Underlying issue was in settings cache being outdated during theme change related i18n initialization - Fix focuses on removing settings cache dependency and and rely on most up to date data about currently active theme - The benefit of this approach is reduced coupling with settings cache
This commit is contained in:
parent
e756af65f2
commit
33bdd2384b
4 changed files with 13 additions and 14 deletions
|
@ -16,7 +16,7 @@ function activate(loadedTheme, checkedTheme, error) {
|
||||||
active.set(loadedTheme, checkedTheme, error);
|
active.set(loadedTheme, checkedTheme, error);
|
||||||
const currentGhostAPI = active.get().engine('ghost-api');
|
const currentGhostAPI = active.get().engine('ghost-api');
|
||||||
|
|
||||||
events.emit('services.themes.activated');
|
events.emit('services.themes.activated', loadedTheme.name);
|
||||||
|
|
||||||
if (previousGhostAPI !== undefined && (previousGhostAPI !== currentGhostAPI)) {
|
if (previousGhostAPI !== undefined && (previousGhostAPI !== currentGhostAPI)) {
|
||||||
events.emit('services.themes.api.changed');
|
events.emit('services.themes.api.changed');
|
||||||
|
|
|
@ -3,7 +3,7 @@ const {i18n, events} = require('../../../server/lib/common');
|
||||||
const logging = require('../../../shared/logging');
|
const logging = require('../../../shared/logging');
|
||||||
const settingsCache = require('../../../server/services/settings/cache');
|
const settingsCache = require('../../../server/services/settings/cache');
|
||||||
const config = require('../../../shared/config');
|
const config = require('../../../shared/config');
|
||||||
|
const active = require('./active');
|
||||||
const jp = require('jsonpath');
|
const jp = require('jsonpath');
|
||||||
|
|
||||||
const isNil = require('lodash/isNil');
|
const isNil = require('lodash/isNil');
|
||||||
|
@ -16,11 +16,12 @@ class ThemeI18n extends i18n.I18n {
|
||||||
/**
|
/**
|
||||||
* Setup i18n support for themes:
|
* Setup i18n support for themes:
|
||||||
* - Load correct language file into memory
|
* - Load correct language file into memory
|
||||||
|
*
|
||||||
|
* @param {String} activeTheme - name of the currently loaded theme
|
||||||
*/
|
*/
|
||||||
init() {
|
init(activeTheme) {
|
||||||
// This function is called during theme initialization, and when switching language or theme.
|
// This function is called during theme initialization, and when switching language or theme.
|
||||||
const currentLocale = this._loadLocale();
|
const currentLocale = this._loadLocale();
|
||||||
const activeTheme = settingsCache.get('active_theme');
|
|
||||||
|
|
||||||
// Reading file for current locale and active theme and keeping its content in memory
|
// Reading file for current locale and active theme and keeping its content in memory
|
||||||
if (activeTheme) {
|
if (activeTheme) {
|
||||||
|
@ -101,15 +102,15 @@ let themeI18n = new ThemeI18n();
|
||||||
// * 1. you override a theme, which is already active
|
// * 1. you override a theme, which is already active
|
||||||
// * 2. The data has not changed, no event is triggered.
|
// * 2. The data has not changed, no event is triggered.
|
||||||
// */
|
// */
|
||||||
events.on('services.themes.activated', function () {
|
events.on('services.themes.activated', function (activeTheme) {
|
||||||
themeI18n.init();
|
themeI18n.init(activeTheme);
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When locale changes, we reload theme translations
|
* When locale changes, we reload theme translations
|
||||||
*/
|
*/
|
||||||
events.on('settings.lang.edited', function () {
|
events.on('settings.lang.edited', function () {
|
||||||
themeI18n.init();
|
themeI18n.init(active.get().name);
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = themeI18n;
|
module.exports = themeI18n;
|
||||||
|
|
|
@ -19,7 +19,7 @@ module.exports = {
|
||||||
init: function initThemes() {
|
init: function initThemes() {
|
||||||
const activeThemeName = settingsCache.get('active_theme');
|
const activeThemeName = settingsCache.get('active_theme');
|
||||||
|
|
||||||
i18n.init();
|
i18n.init(activeThemeName);
|
||||||
|
|
||||||
debug('init themes', activeThemeName);
|
debug('init themes', activeThemeName);
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ const configUtils = require('../../utils/configUtils');
|
||||||
|
|
||||||
describe('{{t}} helper', function () {
|
describe('{{t}} helper', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
settingsCache.set('active_theme', {value: 'casper'});
|
|
||||||
configUtils.set('paths:contentPath', path.join(__dirname, '../../utils/fixtures/'));
|
configUtils.set('paths:contentPath', path.join(__dirname, '../../utils/fixtures/'));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -18,7 +17,7 @@ describe('{{t}} helper', function () {
|
||||||
|
|
||||||
it('theme translation is DE', function () {
|
it('theme translation is DE', function () {
|
||||||
settingsCache.set('lang', {value: 'de'});
|
settingsCache.set('lang', {value: 'de'});
|
||||||
themeI18n.init();
|
themeI18n.init('casper');
|
||||||
|
|
||||||
let rendered = helpers.t.call({}, 'Top left Button', {
|
let rendered = helpers.t.call({}, 'Top left Button', {
|
||||||
hash: {}
|
hash: {}
|
||||||
|
@ -29,7 +28,7 @@ describe('{{t}} helper', function () {
|
||||||
|
|
||||||
it('theme translation is EN', function () {
|
it('theme translation is EN', function () {
|
||||||
settingsCache.set('lang', {value: 'en'});
|
settingsCache.set('lang', {value: 'en'});
|
||||||
themeI18n.init();
|
themeI18n.init('casper');
|
||||||
|
|
||||||
let rendered = helpers.t.call({}, 'Top left Button', {
|
let rendered = helpers.t.call({}, 'Top left Button', {
|
||||||
hash: {}
|
hash: {}
|
||||||
|
@ -40,7 +39,7 @@ describe('{{t}} helper', function () {
|
||||||
|
|
||||||
it('[fallback] no theme translation file found for FR', function () {
|
it('[fallback] no theme translation file found for FR', function () {
|
||||||
settingsCache.set('lang', {value: 'fr'});
|
settingsCache.set('lang', {value: 'fr'});
|
||||||
themeI18n.init();
|
themeI18n.init('casper');
|
||||||
|
|
||||||
let rendered = helpers.t.call({}, 'Top left Button', {
|
let rendered = helpers.t.call({}, 'Top left Button', {
|
||||||
hash: {}
|
hash: {}
|
||||||
|
@ -50,9 +49,8 @@ describe('{{t}} helper', function () {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('[fallback] no theme files at all, use key as translation', function () {
|
it('[fallback] no theme files at all, use key as translation', function () {
|
||||||
settingsCache.set('active_theme', {value: 'casper-1.4'});
|
|
||||||
settingsCache.set('lang', {value: 'de'});
|
settingsCache.set('lang', {value: 'de'});
|
||||||
themeI18n.init();
|
themeI18n.init('casper-1.4');
|
||||||
|
|
||||||
let rendered = helpers.t.call({}, 'Top left Button', {
|
let rendered = helpers.t.call({}, 'Top left Button', {
|
||||||
hash: {}
|
hash: {}
|
||||||
|
|
Loading…
Reference in a new issue