2018-09-10 15:39:50 +03:00
|
|
|
const supportedLocales = ['en'],
|
2017-12-04 19:13:12 +03:00
|
|
|
chalk = require('chalk'),
|
2018-01-09 16:50:57 +03:00
|
|
|
fs = require('fs-extra'),
|
2017-12-04 19:13:12 +03:00
|
|
|
MessageFormat = require('intl-messageformat'),
|
2018-01-09 16:50:57 +03:00
|
|
|
jp = require('jsonpath'),
|
2018-09-10 15:39:50 +03:00
|
|
|
isString = require('lodash/isString'),
|
|
|
|
isObject = require('lodash/isObject'),
|
|
|
|
isEqual = require('lodash/isEqual'),
|
|
|
|
merge = require('lodash/merge'),
|
2018-01-09 16:50:57 +03:00
|
|
|
path = require('path'),
|
|
|
|
config = require('../../config'),
|
2017-12-04 19:13:12 +03:00
|
|
|
errors = require('./errors'),
|
2018-01-09 16:50:57 +03:00
|
|
|
events = require('./events'),
|
|
|
|
logging = require('./logging'),
|
|
|
|
settingsCache = require('../../services/settings/cache'),
|
2018-09-10 15:39:50 +03:00
|
|
|
_private = {};
|
2015-07-18 07:07:42 +03:00
|
|
|
|
Bumped major test dependencies (#10403)
refs #9389
- eslint@5.12.1, eslint-plugin-ghost@0.1.0, grunt-contrib-clean@2.0.0, grunt-contrib-uglify@4.0.0, grunt-eslint@21.0.0, grunt-mocha-cli@4.0.0, grunt-shell@3.0.1, mocha@5.2
.0, nock@10.0.6, rewire@4.0.1
All of them dropped Node v4. I was not able to find any other big breaking changes, which affect us right now.
2019-01-21 20:28:44 +03:00
|
|
|
// currentLocale, dynamically based on overall settings (key = "default_locale") in the settings db table
|
|
|
|
// (during Ghost's initialization, settings available inside i18n functions below; see core/server/index.js)
|
|
|
|
//
|
|
|
|
// E.g.: en = English (default), es = Spanish, en-US = American English, etc.
|
|
|
|
// Standard:
|
|
|
|
// Language tags in HTML and XML
|
|
|
|
// https://www.w3.org/International/articles/language-tags/
|
|
|
|
//
|
|
|
|
// The corresponding translation files should be at content/themes/mytheme/locales/es.json, etc.
|
2018-09-10 15:39:50 +03:00
|
|
|
let currentLocale,
|
2018-01-09 16:50:57 +03:00
|
|
|
activeTheme,
|
|
|
|
coreStrings,
|
|
|
|
themeStrings,
|
2015-07-18 07:07:42 +03:00
|
|
|
I18n;
|
|
|
|
|
2018-01-09 16:50:57 +03:00
|
|
|
/**
|
|
|
|
* When active theme changes, we reload theme translations
|
2018-01-12 12:19:28 +03:00
|
|
|
* We listen on the service event, because of the following known case:
|
|
|
|
* 1. you override a theme, which is already active
|
|
|
|
* 2. The data has not changed, no event is triggered.
|
2018-01-09 16:50:57 +03:00
|
|
|
*/
|
2018-01-12 12:19:28 +03:00
|
|
|
events.on('services.themes.activated', function () {
|
2018-01-09 16:50:57 +03:00
|
|
|
I18n.loadThemeTranslations();
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* When locale changes, we reload theme translations
|
|
|
|
*/
|
|
|
|
events.on('settings.default_locale.edited', function () {
|
|
|
|
I18n.loadThemeTranslations();
|
|
|
|
});
|
|
|
|
|
2015-07-18 07:07:42 +03:00
|
|
|
I18n = {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to find and compile the given data context with a proper string resource.
|
|
|
|
*
|
|
|
|
* @param {string} path Path with in the JSON language file to desired string (ie: "errors.init.jsNotBuilt")
|
2016-02-22 03:57:22 +03:00
|
|
|
* @param {object} [bindings]
|
2015-07-18 07:07:42 +03:00
|
|
|
* @returns {string}
|
|
|
|
*/
|
2016-02-22 03:57:22 +03:00
|
|
|
t: function t(path, bindings) {
|
2018-09-10 15:39:50 +03:00
|
|
|
let string, isTheme, msg;
|
2018-01-09 16:50:57 +03:00
|
|
|
|
|
|
|
currentLocale = I18n.locale();
|
|
|
|
if (bindings !== undefined) {
|
|
|
|
isTheme = bindings.isThemeString;
|
|
|
|
delete bindings.isThemeString;
|
|
|
|
}
|
|
|
|
string = I18n.findString(path, {isThemeString: isTheme});
|
2015-07-18 07:07:42 +03:00
|
|
|
|
|
|
|
// 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
|
|
|
|
// translated/formatted string.
|
2018-09-10 15:39:50 +03:00
|
|
|
if (Array.isArray(string)) {
|
2015-07-18 07:07:42 +03:00
|
|
|
msg = [];
|
|
|
|
string.forEach(function (s) {
|
2018-09-10 15:39:50 +03:00
|
|
|
let m = new MessageFormat(s, currentLocale);
|
2015-07-18 07:07:42 +03:00
|
|
|
|
2017-12-04 19:13:12 +03:00
|
|
|
try {
|
|
|
|
m.format(bindings);
|
|
|
|
} catch (err) {
|
|
|
|
logging.error(err.message);
|
|
|
|
|
|
|
|
// fallback
|
2018-01-09 16:50:57 +03:00
|
|
|
m = new MessageFormat(coreStrings.errors.errors.anErrorOccurred, currentLocale);
|
2017-12-04 19:13:12 +03:00
|
|
|
m = msg.format();
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.push(m);
|
2015-07-18 07:07:42 +03:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
msg = new MessageFormat(string, currentLocale);
|
2017-12-04 19:13:12 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
msg = msg.format(bindings);
|
|
|
|
} catch (err) {
|
|
|
|
logging.error(err.message);
|
|
|
|
|
|
|
|
// fallback
|
2018-01-09 16:50:57 +03:00
|
|
|
msg = new MessageFormat(coreStrings.errors.errors.anErrorOccurred, currentLocale);
|
2017-12-04 19:13:12 +03:00
|
|
|
msg = msg.format();
|
|
|
|
}
|
2015-07-18 07:07:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse JSON file for matching locale, returns string giving path.
|
|
|
|
*
|
|
|
|
* @param {string} msgPath Path with in the JSON language file to desired string (ie: "errors.init.jsNotBuilt")
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2017-11-14 16:03:48 +03:00
|
|
|
findString: function findString(msgPath, opts) {
|
2018-09-10 15:39:50 +03:00
|
|
|
const options = merge({log: true}, opts || {});
|
|
|
|
let candidateString, matchingString, path;
|
2017-11-14 16:03:48 +03:00
|
|
|
|
2015-07-18 07:07:42 +03:00
|
|
|
// no path? no string
|
2018-09-10 15:39:50 +03:00
|
|
|
if (msgPath.length === 0 || !isString(msgPath)) {
|
2018-01-09 16:50:57 +03:00
|
|
|
chalk.yellow('i18n.t() - received an empty path.');
|
2015-07-18 07:07:42 +03:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:50:57 +03:00
|
|
|
// If not in memory, load translations for core
|
|
|
|
if (coreStrings === undefined) {
|
2015-11-12 15:29:45 +03:00
|
|
|
I18n.init();
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:50:57 +03:00
|
|
|
if (options.isThemeString) {
|
|
|
|
// If not in memory, load translations for theme
|
|
|
|
if (themeStrings === undefined) {
|
|
|
|
I18n.loadThemeTranslations();
|
|
|
|
}
|
|
|
|
// Both jsonpath's dot-notation and bracket-notation start with '$'
|
|
|
|
// E.g.: $.store.book.title or $['store']['book']['title']
|
|
|
|
// The {{t}} translation helper passes the default English text
|
|
|
|
// The full Unicode jsonpath with '$' is built here
|
|
|
|
// jp.stringify and jp.value are jsonpath methods
|
|
|
|
// Info: https://www.npmjs.com/package/jsonpath
|
|
|
|
path = jp.stringify(['$', msgPath]);
|
|
|
|
candidateString = jp.value(themeStrings, path) || msgPath;
|
|
|
|
} else {
|
|
|
|
// Backend messages use dot-notation, and the '$.' prefix is added here
|
|
|
|
// While bracket-notation allows any Unicode characters in keys for themes,
|
|
|
|
// dot-notation allows only word characters in keys for backend messages
|
|
|
|
// (that is \w or [A-Za-z0-9_] in RegExp)
|
2018-09-10 15:39:50 +03:00
|
|
|
path = `$.${msgPath}`;
|
2018-01-09 16:50:57 +03:00
|
|
|
candidateString = jp.value(coreStrings, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
matchingString = candidateString || {};
|
2015-07-18 07:07:42 +03:00
|
|
|
|
2018-09-10 15:39:50 +03:00
|
|
|
if (isObject(matchingString) || isEqual(matchingString, {})) {
|
2017-11-14 16:03:48 +03:00
|
|
|
if (options.log) {
|
|
|
|
logging.error(new errors.IncorrectUsageError({
|
|
|
|
message: `i18n error: path "${msgPath}" was not found`
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:50:57 +03:00
|
|
|
matchingString = coreStrings.errors.errors.anErrorOccurred;
|
2015-07-18 07:07:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return matchingString;
|
|
|
|
},
|
|
|
|
|
Added more database soft limits (#9225)
refs #8143
Sets soft limits for certain db fields:
- `posts`:
- `title`: 255 chars (current hard limit: 2,000 chars)
- `meta_title`: 300 chars (current hard limit: 2,000 chars)
- `meta_description`: 500 chars (current hard limit: 2,000 chars)
- `users`:
- `bio`: 200 chars (current hard limit: 65,535 chars)
- `location`: 150 chars (current hard limit: 65,535 chars)
- `meta_description`: 500 chars (current hard limit: 2,000 chars)
- `meta_title`: 300 chars (current hard limit: 2,000 chars)
- `tags`:
- `description`: 500 chars (current hard limit: 65,535 chars)
- `meta_title`: 300 chars (current hard limit: 2,000 chars)
- `meta_description`: 500 chars (current hard limit: 2,000 chars)
- same error message for isLength validator as for hard limits (more improvements are comming with https://github.com/TryGhost/Ghost/issues/6050)
- added more tests for importer
- added dynamic translation key handling for validators errors (isLength is only supported atm)
2017-11-09 17:22:20 +03:00
|
|
|
doesTranslationKeyExist: function doesTranslationKeyExist(msgPath) {
|
2018-09-10 15:39:50 +03:00
|
|
|
const translation = I18n.findString(msgPath, {log: false});
|
2018-01-09 16:50:57 +03:00
|
|
|
return translation !== coreStrings.errors.errors.anErrorOccurred;
|
Added more database soft limits (#9225)
refs #8143
Sets soft limits for certain db fields:
- `posts`:
- `title`: 255 chars (current hard limit: 2,000 chars)
- `meta_title`: 300 chars (current hard limit: 2,000 chars)
- `meta_description`: 500 chars (current hard limit: 2,000 chars)
- `users`:
- `bio`: 200 chars (current hard limit: 65,535 chars)
- `location`: 150 chars (current hard limit: 65,535 chars)
- `meta_description`: 500 chars (current hard limit: 2,000 chars)
- `meta_title`: 300 chars (current hard limit: 2,000 chars)
- `tags`:
- `description`: 500 chars (current hard limit: 65,535 chars)
- `meta_title`: 300 chars (current hard limit: 2,000 chars)
- `meta_description`: 500 chars (current hard limit: 2,000 chars)
- same error message for isLength validator as for hard limits (more improvements are comming with https://github.com/TryGhost/Ghost/issues/6050)
- added more tests for importer
- added dynamic translation key handling for validators errors (isLength is only supported atm)
2017-11-09 17:22:20 +03:00
|
|
|
},
|
|
|
|
|
2015-07-18 07:07:42 +03:00
|
|
|
/**
|
|
|
|
* Setup i18n support:
|
2018-01-09 16:50:57 +03:00
|
|
|
* - Load proper language file into memory
|
2015-07-18 07:07:42 +03:00
|
|
|
*/
|
|
|
|
init: function init() {
|
2018-01-09 16:50:57 +03:00
|
|
|
// This function is called during Ghost's initialization.
|
|
|
|
// Reading translation file for messages from core .js files and keeping its content in memory
|
|
|
|
// The English file is always loaded, until back-end translations are enabled in future versions.
|
|
|
|
// Before that, see previous tasks on issue #6526 (error codes or identifiers, error message
|
|
|
|
// translation at the point of display...)
|
|
|
|
coreStrings = fs.readFileSync(path.join(__dirname, '..', '..', 'translations', 'en.json'));
|
2016-05-25 10:34:46 +03:00
|
|
|
|
|
|
|
// if translation file is not valid, you will see an error
|
|
|
|
try {
|
2018-01-09 16:50:57 +03:00
|
|
|
coreStrings = JSON.parse(coreStrings);
|
2016-05-25 10:34:46 +03:00
|
|
|
} catch (err) {
|
2018-01-09 16:50:57 +03:00
|
|
|
coreStrings = undefined;
|
2016-05-25 10:34:46 +03:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:50:57 +03:00
|
|
|
_private.initializeIntl();
|
|
|
|
},
|
2015-07-18 07:07:42 +03:00
|
|
|
|
2018-01-09 16:50:57 +03:00
|
|
|
/**
|
|
|
|
* Setup i18n support for themes:
|
|
|
|
* - Load proper language file into memory
|
|
|
|
*/
|
|
|
|
loadThemeTranslations: function loadThemeTranslations() {
|
|
|
|
// This function is called during theme initialization, and when switching language or theme.
|
|
|
|
currentLocale = I18n.locale();
|
|
|
|
activeTheme = settingsCache.get('active_theme');
|
2015-07-18 07:07:42 +03:00
|
|
|
|
2018-01-09 16:50:57 +03:00
|
|
|
// Reading file for current locale and active theme and keeping its content in memory
|
|
|
|
if (activeTheme) {
|
|
|
|
// Reading translation file for theme .hbs templates.
|
|
|
|
// Compatibility with both old themes and i18n-capable themes.
|
|
|
|
// Preventing missing files.
|
|
|
|
try {
|
|
|
|
themeStrings = fs.readFileSync(path.join(config.getContentPath('themes'), activeTheme, 'locales', currentLocale + '.json'));
|
|
|
|
} catch (err) {
|
|
|
|
themeStrings = undefined;
|
|
|
|
if (err.code === 'ENOENT') {
|
2019-04-16 12:32:20 +03:00
|
|
|
if (currentLocale !== 'en') {
|
|
|
|
logging.warn(`Theme's file locales/${currentLocale}.json not found.`);
|
|
|
|
}
|
2018-01-09 16:50:57 +03:00
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
2015-07-18 07:07:42 +03:00
|
|
|
}
|
2018-01-09 16:50:57 +03:00
|
|
|
if (themeStrings === undefined && currentLocale !== 'en') {
|
|
|
|
logging.warn('Falling back to locales/en.json.');
|
|
|
|
try {
|
|
|
|
themeStrings = fs.readFileSync(path.join(config.getContentPath('themes'), activeTheme, 'locales', 'en.json'));
|
|
|
|
} catch (err) {
|
|
|
|
themeStrings = undefined;
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
logging.warn('Theme\'s file locales/en.json not found.');
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (themeStrings !== undefined) {
|
|
|
|
// if translation file is not valid, you will see an error
|
|
|
|
try {
|
|
|
|
themeStrings = JSON.parse(themeStrings);
|
|
|
|
} catch (err) {
|
|
|
|
themeStrings = undefined;
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (themeStrings === undefined) {
|
|
|
|
// even if empty, themeStrings must be an object for jp.value
|
|
|
|
themeStrings = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
_private.initializeIntl();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exporting the current locale (e.g. "en") to make it available for other files as well,
|
2019-06-19 12:30:28 +03:00
|
|
|
* such as core/frontend/helpers/date.js and core/frontend/helpers/lang.js
|
2018-01-09 16:50:57 +03:00
|
|
|
*/
|
|
|
|
locale: function locale() {
|
|
|
|
return settingsCache.get('default_locale');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup i18n support:
|
|
|
|
* - Polyfill node.js if it does not have Intl support or support for a particular locale
|
|
|
|
*/
|
|
|
|
_private.initializeIntl = function initializeIntl() {
|
2018-09-10 15:39:50 +03:00
|
|
|
let hasBuiltInLocaleData, IntlPolyfill;
|
2018-01-09 16:50:57 +03:00
|
|
|
|
|
|
|
if (global.Intl) {
|
|
|
|
// Determine if the built-in `Intl` has the locale data we need.
|
|
|
|
hasBuiltInLocaleData = supportedLocales.every(function (locale) {
|
|
|
|
return Intl.NumberFormat.supportedLocalesOf(locale)[0] === locale &&
|
|
|
|
Intl.DateTimeFormat.supportedLocalesOf(locale)[0] === locale;
|
|
|
|
});
|
|
|
|
if (!hasBuiltInLocaleData) {
|
|
|
|
// `Intl` exists, but it doesn't have the data we need, so load the
|
|
|
|
// polyfill and replace the constructors with need with the polyfill's.
|
|
|
|
IntlPolyfill = require('intl');
|
|
|
|
Intl.NumberFormat = IntlPolyfill.NumberFormat;
|
|
|
|
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
|
2015-07-18 07:07:42 +03:00
|
|
|
}
|
2018-01-09 16:50:57 +03:00
|
|
|
} else {
|
|
|
|
// No `Intl`, so use and load the polyfill.
|
|
|
|
global.Intl = require('intl');
|
2015-07-18 07:07:42 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = I18n;
|