Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Theme i18n #21490

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ghost/core/core/frontend/helpers/t.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function t(text, options = {}) {
// no-op: translation key is missing, return an empty string
return '';
}

console.log('t got', text, options);
const bindings = {};
let prop;
for (prop in options.hash) {
Expand Down
24 changes: 19 additions & 5 deletions ghost/core/core/frontend/services/theme-engine/i18n/I18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const isEqual = require('lodash/isEqual');
const isNil = require('lodash/isNil');
const merge = require('lodash/merge');
const get = require('lodash/get');
//const i18nLib = require('@tryghost/i18n');
//const i18n = require('@tryghost/i18n/lib/i18n');

class I18n {
/**
Expand Down Expand Up @@ -66,11 +68,13 @@ class I18n {
* @param {object} [bindings]
* @returns {string}
*/
t(translationPath, bindings) {
/*t(translationPath, bindings) {
let string;
let msg;

string = this._findString(translationPath);
//string = this._findString(translationPath);

return i18n.t(translationPath, bindings);

// If the path returns an array (as in the case with anything that has multiple paragraphs such as emails), then
// loop through them and return an array of translated/formatted strings. Otherwise, just return the normal
Expand All @@ -92,11 +96,21 @@ class I18n {
* - Load proper language file into memory
*/
init() {
this._strings = this._loadStrings();

this._initializeIntl();
}

//this._strings = this._loadStrings();
//this._initializeIntl();
}
t(key, hash) {
const i18nLib = require('@tryghost/i18n');
const i18nLanguage = this.locale() || 'en';
const i18n = i18nLib(i18nLanguage, 'theme');
i18n.init({
lng: this.locale(),
ns: 'theme'
});
return i18n.t(key, hash);
}
/**
* Attempt to load strings from a file
*
Expand Down
37 changes: 26 additions & 11 deletions ghost/i18n/lib/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ const SUPPORTED_LOCALES = [

/**
* @param {string} [lng]
* @param {'ghost'|'portal'|'test'|'signup-form'|'comments'|'search'|'newsletter'} ns
* @param {'ghost'|'portal'|'test'|'signup-form'|'comments'|'search'|'newsletter'|'theme'} ns

*/
module.exports = (lng = 'en', ns = 'portal') => {
const i18nextInstance = i18next.createInstance();
Expand All @@ -73,6 +74,29 @@ module.exports = (lng = 'en', ns = 'portal') => {
suffix: '}'
};
}
let resources;
if (ns !== 'theme') {
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;
}, {});
} else {
resources = {
en: {
theme: require(`../locales/en/theme.json`)
},
fr: {
theme: require(`../locales/fr/theme.json`)
}
};
}

i18nextInstance.init({
lng,

Expand All @@ -92,16 +116,7 @@ module.exports = (lng = 'en', ns = 'portal') => {
// separators
interpolation,

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;
}, {})
resources
});

return i18nextInstance;
Expand Down
3 changes: 3 additions & 0 deletions ghost/i18n/locales/en/theme.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Read more": "Read more"
}
3 changes: 3 additions & 0 deletions ghost/i18n/locales/fr/theme.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Read more": "Lirer plus"
}
Loading