0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00

Added support for namespaces to i18n package

refs https://github.com/TryGhost/Ghost/issues/15502

- this adds support for namespaces to the i18n package so we can
  separate translations for different packages
This commit is contained in:
Daniel Lockyer 2023-03-01 09:10:11 +01:00
parent 150a3a9c68
commit 6d8ca23625
No known key found for this signature in database
9 changed files with 62 additions and 34 deletions

View file

@ -9,11 +9,13 @@ module.exports = {
keySeparator: false,
namespaceSeparator: false,
defaultNamespace: process.env.NAMESPACE || 'translation',
createOldCatalogs: false,
indentation: 4,
sort: true,
failOnUpdate: process.env.CI,
output: 'locales/$LOCALE.json'
output: 'locales/$LOCALE/$NAMESPACE.json'
};

View file

@ -1,17 +1,12 @@
const i18next = require('i18next');
const RESOURCES = {
en: {
translation: require('../locales/en.json')
},
nl: {
translation: require('../locales/nl.json')
}
};
const SUPPORTED_LOCALES = ['en', 'nl'];
const SUPPORTED_LOCALES = Object.keys(RESOURCES);
module.exports = (lng = 'en') => {
/**
* @param {string} [lng]
* @param {'portal'|'test'} ns
*/
module.exports = (lng = 'en', ns = 'portal') => {
const i18nextInstance = i18next.createInstance();
i18nextInstance.init({
lng,
@ -27,7 +22,15 @@ module.exports = (lng = 'en') => {
// do not load a fallback
fallbackLng: false,
resources: RESOURCES
ns: ns,
defaultNS: ns,
resources: SUPPORTED_LOCALES.reduce((acc, lng) => {
acc[lng] = {
[ns]: require(`../locales/${lng}/${ns}.json`)
}
return acc;
}, {})
});
return i18nextInstance;

View file

@ -26,7 +26,6 @@
"Get help": "",
"Get notified when someone replies to your comment": "",
"Give feedback on this post": "",
"Hello": "",
"Less like this": "",
"Manage": "",
"Monthly": "",

View file

@ -0,0 +1,4 @@
{
"Hello": "Hello Test",
"Name": ""
}

View file

@ -26,12 +26,11 @@
"Get help": "",
"Get notified when someone replies to your comment": "",
"Give feedback on this post": "",
"Hello": "Hallo",
"Less like this": "",
"Manage": "",
"Monthly": "",
"More like this": "",
"Name": "",
"Name": "Naam",
"Not receiving emails?": "",
"Now check your email!": "",
"Powered by Ghost": "",

View file

@ -0,0 +1,4 @@
{
"Hello": "Hallo Test",
"Name": ""
}

View file

@ -8,12 +8,13 @@
"scripts": {
"dev": "echo \"Implement me!\"",
"test:unit": "NODE_ENV=testing c8 --include index.js --include lib --check-coverage --100 --reporter text --reporter cobertura mocha './test/**/*.test.js'",
"test": "yarn test:unit && yarn test:translate",
"test:translate": "yarn translate:generate",
"test": "yarn test:unit && yarn translate",
"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:generate": "i18next '../portal/src/**/*.{js,jsx}' './test/**/*.js'"
"translate": "yarn translate:portal && yarn translate:test",
"translate:portal": "NAMESPACE=portal i18next '../portal/src/**/*.{js,jsx}'",
"translate:test": "NAMESPACE=test i18next './test/**/*.js'"
},
"files": [
"index.js",

View file

@ -2,28 +2,44 @@ const assert = require('assert');
const i18n = require('../');
describe('Can translate', function () {
describe('Dutch', function () {
let t;
describe('i18n', function () {
describe('Can use Portal resources', function () {
describe('English', function () {
let t;
before(function () {
t = i18n('nl').t;
});
before(function () {
t = i18n('nl', 'portal').t;
});
it('can translate Dutch', function () {
assert.equal(t('Hello'), 'Hallo');
it('can translate `Name`', function () {
assert.equal(t('Name'), 'Naam');
});
});
});
describe('English', function () {
let t;
describe('Can translate', function () {
describe('Dutch', function () {
let t;
before(function () {
t = i18n('en').t;
before(function () {
t = i18n('nl', 'test').t;
});
it('can translate Dutch', function () {
assert.equal(t('Hello'), 'Hallo Test');
});
});
it('can translate English', function () {
assert.equal(t('Hello'), 'Hello');
describe('English', function () {
let t;
before(function () {
t = i18n('en', 'test').t;
});
it('can translate English', function () {
assert.equal(t('Hello'), 'Hello Test');
});
});
});
});

View file

@ -156,7 +156,7 @@ export default class App extends React.Component {
try {
// Fetch data from API, links, preview, dev sources
const {site, member, page, showPopup, popupNotification, lastPage, pageQuery, pageData} = await this.fetchData();
const i18n = require('@tryghost/i18n')(/*site.locale || */ 'en'); // TODO: uncomment when you want to enable i18n translations
const i18n = require('@tryghost/i18n')(/*site.locale || */ 'en', 'portal'); // TODO: uncomment when you want to enable i18n translations
const state = {
site,
member,