Ghost/ghost/i18n/lib/i18n.js
Simon Backx 4f604af744 🐛 Fixed translations with spaces missing in signup-form
refs https://ghost.slack.com/archives/C02G9E68C/p1692784819620269
refs https://github.com/TryGhost/Product/issues/3504

- Somehow, when using i18n in TypeScript, the require will put some of the keys into 'default' and not into the root. Mainly all keys that have a space in them. Couldn't find any documentation about this
- The solution is to also add 'default' to the keys that are being used in the code

This change also fixes the translate script (wasn't updated for updated paths), includes the missing translations, and already adds comments to lookup translation strings
2023-08-23 15:11:41 +02:00

82 lines
2.1 KiB
JavaScript

const i18next = require('i18next');
const SUPPORTED_LOCALES = [
'af', // Afrikaans
'bg', // Bulgarian
'ca', // Catalan
'cs', // Czech
'da', // Danish
'de', // German
'en', // English
'eo', // Esperanto
'es', // Spanish
'fi', // Finnish
'fr', // French
'hr', // Croatian
'hu', // Hungarian
'id', // Indonesian
'it', // Italian
'ko', // Korean
'ja', // Japanese
'mn', // Mongolian
'ms', // Malay
'nl', // Dutch
'no', // Norwegian
'nn', // Norwegian Nynorsk
'pl', // Polish
'pt', // Portuguese
'pt-BR', // Portuguese (Brazil)
'ro', // Romanian
'ru', // Russian
'si', // Sinhala
'sl', // Slovenian
'sq', // Albanian
'sr', // Serbian
'sv', // Swedish
'tr', // Turkish
'uk', // Ukrainian
'uz', // Uzbek
'vi', // Vietnamese
'zh', // Chinese
'zh-Hant' // Traditional Chinese
];
/**
* @param {string} [lng]
* @param {'ghost'|'portal'|'test'|'signup-form'|'comments'} ns
*/
module.exports = (lng = 'en', ns = 'portal') => {
const i18nextInstance = i18next.createInstance();
i18nextInstance.init({
lng,
// 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, locale) => {
const res = require(`../locales/${locale}/${ns}.json`);
// Note: due some random thing in TypeScript, 'requiring' a JSON file with a space in a key name, only adds it to the default export
// If changing this behaviour, please also check the comments and signup-form apps in another language (mainly sentences with a space in them)
acc[locale] = {
[ns]: {...res, ...(res.default && typeof res.default === 'object' ? res.default : {})}
};
return acc;
}, {})
});
return i18nextInstance;
};
module.exports.SUPPORTED_LOCALES = SUPPORTED_LOCALES;