From 21ea426794963e4e52c3abf8842f3a21a0584a34 Mon Sep 17 00:00:00 2001 From: Daniel Lockyer Date: Thu, 16 Mar 2023 14:15:01 +0100 Subject: [PATCH] Added i18n service to Ghost core refs https://github.com/TryGhost/Ghost/issues/15502 - this adds the scaffolding for enabling i18n translations within Ghost core - also adds `yarn translate:ghost` as an option to the `i18n` package - the locale is currently hardcoded to `en` so we don't utilize the translations until we're ready --- ghost/core/core/boot.js | 5 +++++ ghost/core/core/server/services/i18n.js | 14 ++++++++++++++ ghost/core/package.json | 1 + ghost/i18n/lib/i18n.js | 2 +- ghost/i18n/locales/en/ghost.json | 1 + ghost/i18n/locales/nl/ghost.json | 1 + ghost/i18n/package.json | 3 ++- 7 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 ghost/core/core/server/services/i18n.js create mode 100644 ghost/i18n/locales/en/ghost.json create mode 100644 ghost/i18n/locales/nl/ghost.json diff --git a/ghost/core/core/boot.js b/ghost/core/core/boot.js index 477e59c858..ebc1a2d0b2 100644 --- a/ghost/core/core/boot.js +++ b/ghost/core/core/boot.js @@ -102,6 +102,11 @@ async function initCore({ghostServer, config, bootLogger, frontend}) { await settings.syncEmailSettings(config.get('hostSettings:emailVerification:verified')); debug('End: settings'); + debug('Begin: i18n'); + const i18n = require('./server/services/i18n'); + await i18n.init(); + debug('End: i18n'); + // The URLService is a core part of Ghost, which depends on models. debug('Begin: Url Service'); const urlService = require('./server/services/url'); diff --git a/ghost/core/core/server/services/i18n.js b/ghost/core/core/server/services/i18n.js new file mode 100644 index 0000000000..331ce7d4c5 --- /dev/null +++ b/ghost/core/core/server/services/i18n.js @@ -0,0 +1,14 @@ +//const debug = require('@tryghost/debug')('i18n'); +const i18n = require('@tryghost/i18n'); + +module.exports.init = function () { + //const events = require('../lib/common/events'); + //const settingsCache = require('../../shared/settings-cache'); + + module.exports = i18n(/* settingsCache.get('locale') */ 'en', 'ghost'); + + /*events.on('settings.locale.edited', (model) => { + debug('locale changed, updating i18n to', model.get('value')); + i18nInstance.changeLanguage(model.get('value')); + });*/ +}; diff --git a/ghost/core/package.json b/ghost/core/package.json index 12d4375353..025f29b82f 100644 --- a/ghost/core/package.json +++ b/ghost/core/package.json @@ -88,6 +88,7 @@ "@tryghost/helpers": "1.1.76", "@tryghost/html-to-plaintext": "0.0.0", "@tryghost/http-cache-utils": "0.1.7", + "@tryghost/i18n": "0.0.0", "@tryghost/image-transform": "1.2.4", "@tryghost/importer-handler-content-files": "0.0.0", "@tryghost/importer-revue": "0.0.0", diff --git a/ghost/i18n/lib/i18n.js b/ghost/i18n/lib/i18n.js index b2e4826df8..32d8bc1ead 100644 --- a/ghost/i18n/lib/i18n.js +++ b/ghost/i18n/lib/i18n.js @@ -4,7 +4,7 @@ const SUPPORTED_LOCALES = ['en', 'nl']; /** * @param {string} [lng] - * @param {'portal'|'test'} ns + * @param {'ghost'|'portal'|'test'} ns */ module.exports = (lng = 'en', ns = 'portal') => { const i18nextInstance = i18next.createInstance(); diff --git a/ghost/i18n/locales/en/ghost.json b/ghost/i18n/locales/en/ghost.json new file mode 100644 index 0000000000..0967ef424b --- /dev/null +++ b/ghost/i18n/locales/en/ghost.json @@ -0,0 +1 @@ +{} diff --git a/ghost/i18n/locales/nl/ghost.json b/ghost/i18n/locales/nl/ghost.json new file mode 100644 index 0000000000..0967ef424b --- /dev/null +++ b/ghost/i18n/locales/nl/ghost.json @@ -0,0 +1 @@ +{} diff --git a/ghost/i18n/package.json b/ghost/i18n/package.json index 640c576e2c..7f3d089667 100644 --- a/ghost/i18n/package.json +++ b/ghost/i18n/package.json @@ -12,7 +12,8 @@ "lint:code": "eslint *.js lib/ --ext .js --cache", "lint": "yarn lint:code && yarn lint:test", "lint:test": "eslint -c test/.eslintrc.js test/ --ext .js --cache", - "translate": "yarn translate:portal && yarn translate:test", + "translate": "yarn translate:ghost && yarn translate:portal && yarn translate:test", + "translate:ghost": "NAMESPACE=ghost i18next '../core/core/{frontend,server,shared}/**/*.{js,jsx}'", "translate:portal": "NAMESPACE=portal i18next '../portal/src/**/*.{js,jsx}'", "translate:test": "NAMESPACE=test i18next './test/**/*.js'" },