Refactored i18n into a class + index

- preparation for using DI instead of requires, so we can move this out of Ghost
- have done this for both the main i18n and theme i18n file
- refactored the constructor
This commit is contained in:
Hannah Wolfe 2021-05-04 13:51:25 +01:00
parent 7360be5625
commit ba53de1add
7 changed files with 40 additions and 14 deletions

View File

@ -1,15 +1,15 @@
const errors = require('@tryghost/errors');
const i18n = require('../../../shared/i18n');
const logging = require('../../../shared/logging');
const settingsCache = require('../../../server/services/settings/cache');
const config = require('../../../shared/config');
const i18n = require('../../../../shared/i18n');
const logging = require('../../../../shared/logging');
const settingsCache = require('../../../../server/services/settings/cache');
const config = require('../../../../shared/config');
const jp = require('jsonpath');
const isNil = require('lodash/isNil');
class ThemeI18n extends i18n.I18n {
constructor(locale) {
super(locale);
constructor(options = {}) {
super(options);
}
/**
@ -93,6 +93,4 @@ class ThemeI18n extends i18n.I18n {
}
}
let themeI18n = new ThemeI18n();
module.exports = themeI18n;
module.exports = ThemeI18n;

View File

@ -0,0 +1,5 @@
const ThemeI18n = require('./i18n');
const themeI18n = new ThemeI18n();
module.exports = themeI18n;
module.exports.ThemeI18n = ThemeI18n;

View File

@ -9,11 +9,11 @@ const isNil = require('lodash/isNil');
const merge = require('lodash/merge');
const get = require('lodash/get');
const errors = require('@tryghost/errors');
const logging = require('./logging');
const logging = require('../logging');
class I18n {
constructor(locale) {
this._locale = locale || this.defaultLocale();
constructor(options = {}) {
this._locale = options.locale || this.defaultLocale();
this._strings = null;
}
@ -212,5 +212,4 @@ class I18n {
}
}
module.exports = new I18n();
module.exports.I18n = I18n;
module.exports = I18n;

View File

@ -0,0 +1,4 @@
const I18n = require('./i18n');
module.exports = new I18n();
module.exports.I18n = I18n;

View File

@ -0,0 +1,10 @@
const should = require('should');
const ThemeI18n = require('../../../../core/frontend/services/theme-engine/i18n').ThemeI18n;
describe('ThemeI18n Class Behaviour', function () {
it('defaults to en', function () {
const i18n = new ThemeI18n();
i18n.locale().should.eql('en');
});
});

View File

@ -0,0 +1,10 @@
const should = require('should');
const I18n = require('../../../core/shared/i18n').I18n;
describe('I18n Class Behaviour', function () {
it('defaults to en', function () {
const i18n = new I18n();
i18n.locale().should.eql('en');
});
});