From 15fad7837f0a0919a41e2f5b4ce0accd455653b0 Mon Sep 17 00:00:00 2001 From: Hannah Wolfe Date: Thu, 6 May 2021 19:51:38 +0100 Subject: [PATCH] Moved i18n basePath concept from themes to core - we need the basePath concept for the main i18n class so we can pull it out into a module - we already had this in the themeI18n class, so I just had to move it up - also I added a default of __dirname, so we don't have to declare this constantly in the tests --- .../services/theme-engine/i18n/i18n.js | 15 --------------- core/shared/i18n/i18n.js | 18 +++++++++++++++++- core/shared/i18n/index.js | 4 ++-- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/core/frontend/services/theme-engine/i18n/i18n.js b/core/frontend/services/theme-engine/i18n/i18n.js index 20aab9d7f8..3d904bf802 100644 --- a/core/frontend/services/theme-engine/i18n/i18n.js +++ b/core/frontend/services/theme-engine/i18n/i18n.js @@ -11,21 +11,6 @@ class ThemeI18n extends i18n.I18n { super(options); // We don't care what gets passed in, themes use fulltext mode this._stringMode = 'fulltext'; - this._basePath = options.basePath; - } - - /** - * BasePath getter & setter used for testing - */ - set basePath(basePath) { - this._basePath = basePath; - } - - /** - * Need to call init after this - */ - get basePath() { - return this._basePath; } /** diff --git a/core/shared/i18n/i18n.js b/core/shared/i18n/i18n.js index bab9f01fd2..96cf277463 100644 --- a/core/shared/i18n/i18n.js +++ b/core/shared/i18n/i18n.js @@ -13,11 +13,13 @@ const get = require('lodash/get'); class I18n { /** * @param {objec} [options] + * @param {string} basePath - the base path to the translations directory * @param {string} [locale] - a locale string * @param {{dot|fulltext}} [stringMode] - which mode our translation keys use * @param {{object}} [logging] - logging method */ constructor(options = {}) { + this._basePath = options.basePath || __dirname; this._locale = options.locale || this.defaultLocale(); this._stringMode = options.stringMode || 'dot'; this._logging = options.logging || console; @@ -25,6 +27,20 @@ class I18n { this._strings = null; } + /** + * BasePath getter & setter used for testing + */ + set basePath(basePath) { + this._basePath = basePath; + } + + /** + * Need to call init after this + */ + get basePath() { + return this._basePath; + } + /** * English is our default locale */ @@ -185,7 +201,7 @@ class I18n { } _translationFileDirs() { - return [__dirname, 'translations']; + return [this.basePath]; } // If we are passed a locale, use that, else use this.locale diff --git a/core/shared/i18n/index.js b/core/shared/i18n/index.js index 677805555c..4daf6ff14e 100644 --- a/core/shared/i18n/index.js +++ b/core/shared/i18n/index.js @@ -1,6 +1,6 @@ +const path = require('path'); const logging = require('../logging'); - const I18n = require('./i18n'); -module.exports = new I18n({logging}); +module.exports = new I18n({logging, basePath: path.join(__dirname, 'translations')}); module.exports.I18n = I18n;