mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-20 17:32:15 +03:00
e1699afc77
- preparation for moving the base class out of Ghost - refactored so that all the logic for file loading and fallbacks live in the base class - theme i18n now only overrides init with the properties it needs, filepath generation and error handling - this makes it much easier to move the i18n file out, and eventually have theme i18n live elsewhere too - also prepares for using DI for logging
71 lines
2.2 KiB
JavaScript
71 lines
2.2 KiB
JavaScript
const errors = require('@tryghost/errors');
|
|
const i18n = require('../../../../shared/i18n');
|
|
const logging = require('../../../../shared/logging');
|
|
const config = require('../../../../shared/config');
|
|
|
|
class ThemeI18n extends i18n.I18n {
|
|
/**
|
|
* @param {objec} [options]
|
|
* @param {string} [locale] - a locale string
|
|
*/
|
|
constructor(options = {}) {
|
|
super(options);
|
|
// We don't care what gets passed in, themes use fulltext mode
|
|
this._stringMode = 'fulltext';
|
|
}
|
|
|
|
/**
|
|
* Setup i18n support for themes:
|
|
* - Load correct language file into memory
|
|
*
|
|
* @param {object} options
|
|
* @param {String} options.activeTheme - name of the currently loaded theme
|
|
* @param {String} options.locale - name of the currently loaded locale
|
|
*
|
|
*/
|
|
init({activeTheme, locale}) {
|
|
// This function is called during theme initialization, and when switching language or theme.
|
|
this._locale = locale || this._locale;
|
|
this._activetheme = activeTheme || 'casper';
|
|
|
|
super.init();
|
|
}
|
|
|
|
_translationFileDirs() {
|
|
return [config.getContentPath('themes'), this._activetheme, 'locales'];
|
|
}
|
|
|
|
_handleFallbackToDefault() {
|
|
logging.warn(`Theme translations falling back to locales/${this.defaultLocale()}.json.`);
|
|
}
|
|
|
|
_handleMissingFileError(locale) {
|
|
if (locale !== this.defaultLocale()) {
|
|
logging.warn(`Theme translations file locales/${locale}.json not found.`);
|
|
}
|
|
}
|
|
_handleInvalidFileError(locale, err) {
|
|
logging.error(new errors.IncorrectUsageError({
|
|
err,
|
|
message: `Theme translations unable to parse locales/${locale}.json. Please check that it is valid JSON.`
|
|
}));
|
|
}
|
|
|
|
_handleEmptyKeyError() {
|
|
logging.warn('Theme translations {{t}} helper called without a translation key.');
|
|
}
|
|
|
|
_handleMissingKeyError() {
|
|
// This case cannot be reached in themes as we use the key as the fallback
|
|
}
|
|
|
|
_handleInvalidKeyError(key, err) {
|
|
throw new errors.IncorrectUsageError({
|
|
err,
|
|
message: `Theme translations {{t}} helper called with an invalid translation key: ${key}`
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = ThemeI18n;
|