From ba53de1addb3ebd9c1447be803b631038053978a Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Tue, 4 May 2021 13:51:25 +0100 Subject: [PATCH] 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 --- .../services/theme-engine/{ => i18n}/i18n.js | 16 +++++++--------- .../frontend/services/theme-engine/i18n/index.js | 5 +++++ core/shared/{ => i18n}/i18n.js | 9 ++++----- core/shared/i18n/index.js | 4 ++++ core/shared/{ => i18n}/translations/en.json | 0 test/unit/services/theme-engine/i18n_spec.js | 10 ++++++++++ test/unit/shared/i18n_spec.js | 10 ++++++++++ 7 files changed, 40 insertions(+), 14 deletions(-) rename core/frontend/services/theme-engine/{ => i18n}/i18n.js (90%) create mode 100644 core/frontend/services/theme-engine/i18n/index.js rename core/shared/{ => i18n}/i18n.js (97%) create mode 100644 core/shared/i18n/index.js rename core/shared/{ => i18n}/translations/en.json (100%) create mode 100644 test/unit/services/theme-engine/i18n_spec.js create mode 100644 test/unit/shared/i18n_spec.js diff --git a/core/frontend/services/theme-engine/i18n.js b/core/frontend/services/theme-engine/i18n/i18n.js similarity index 90% rename from core/frontend/services/theme-engine/i18n.js rename to core/frontend/services/theme-engine/i18n/i18n.js index d2adaa7596..611ff04996 100644 --- a/core/frontend/services/theme-engine/i18n.js +++ b/core/frontend/services/theme-engine/i18n/i18n.js @@ -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; diff --git a/core/frontend/services/theme-engine/i18n/index.js b/core/frontend/services/theme-engine/i18n/index.js new file mode 100644 index 0000000000..a5e1924f65 --- /dev/null +++ b/core/frontend/services/theme-engine/i18n/index.js @@ -0,0 +1,5 @@ +const ThemeI18n = require('./i18n'); +const themeI18n = new ThemeI18n(); + +module.exports = themeI18n; +module.exports.ThemeI18n = ThemeI18n; diff --git a/core/shared/i18n.js b/core/shared/i18n/i18n.js similarity index 97% rename from core/shared/i18n.js rename to core/shared/i18n/i18n.js index 902647b4e4..0ff8a3dd9a 100644 --- a/core/shared/i18n.js +++ b/core/shared/i18n/i18n.js @@ -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; diff --git a/core/shared/i18n/index.js b/core/shared/i18n/index.js new file mode 100644 index 0000000000..81e31e4537 --- /dev/null +++ b/core/shared/i18n/index.js @@ -0,0 +1,4 @@ +const I18n = require('./i18n'); + +module.exports = new I18n(); +module.exports.I18n = I18n; diff --git a/core/shared/translations/en.json b/core/shared/i18n/translations/en.json similarity index 100% rename from core/shared/translations/en.json rename to core/shared/i18n/translations/en.json diff --git a/test/unit/services/theme-engine/i18n_spec.js b/test/unit/services/theme-engine/i18n_spec.js new file mode 100644 index 0000000000..9199eb2662 --- /dev/null +++ b/test/unit/services/theme-engine/i18n_spec.js @@ -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'); + }); +}); diff --git a/test/unit/shared/i18n_spec.js b/test/unit/shared/i18n_spec.js new file mode 100644 index 0000000000..4eeef1988c --- /dev/null +++ b/test/unit/shared/i18n_spec.js @@ -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'); + }); +});