Ghost/ghost/i18n/lib/i18n.js
Daniel Lockyer 6d8ca23625
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
2023-03-01 10:24:08 +01:00

40 lines
944 B
JavaScript

const i18next = require('i18next');
const SUPPORTED_LOCALES = ['en', 'nl'];
/**
* @param {string} [lng]
* @param {'portal'|'test'} ns
*/
module.exports = (lng = 'en', ns = 'portal') => {
const i18nextInstance = i18next.createInstance();
i18nextInstance.init({
lng,
debug: process.env.NODE_ENV === 'development',
// allow keys to be phrases having `:`, `.`
nsSeparator: false,
keySeparator: false,
// if the value is an empty string, return the key
returnEmptyString: false,
// do not load a fallback
fallbackLng: false,
ns: ns,
defaultNS: ns,
resources: SUPPORTED_LOCALES.reduce((acc, lng) => {
acc[lng] = {
[ns]: require(`../locales/${lng}/${ns}.json`)
}
return acc;
}, {})
});
return i18nextInstance;
};
module.exports.SUPPORTED_LOCALES = SUPPORTED_LOCALES;