2021-05-06 12:38:56 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2020-03-19 17:07:20 +03:00
|
|
|
const fs = require('fs-extra');
|
|
|
|
const path = require('path');
|
|
|
|
const MessageFormat = require('intl-messageformat');
|
|
|
|
const jp = require('jsonpath');
|
|
|
|
const isString = require('lodash/isString');
|
|
|
|
const isObject = require('lodash/isObject');
|
|
|
|
const isEqual = require('lodash/isEqual');
|
|
|
|
const isNil = require('lodash/isNil');
|
|
|
|
const merge = require('lodash/merge');
|
|
|
|
const get = require('lodash/get');
|
|
|
|
|
|
|
|
class I18n {
|
2021-05-05 18:24:38 +03:00
|
|
|
/**
|
|
|
|
* @param {objec} [options]
|
2021-05-06 21:51:38 +03:00
|
|
|
* @param {string} basePath - the base path to the translations directory
|
2021-05-05 18:24:38 +03:00
|
|
|
* @param {string} [locale] - a locale string
|
|
|
|
* @param {{dot|fulltext}} [stringMode] - which mode our translation keys use
|
2021-05-06 12:38:56 +03:00
|
|
|
* @param {{object}} [logging] - logging method
|
2021-05-05 18:24:38 +03:00
|
|
|
*/
|
2021-05-04 15:51:25 +03:00
|
|
|
constructor(options = {}) {
|
2021-05-06 21:51:38 +03:00
|
|
|
this._basePath = options.basePath || __dirname;
|
2021-05-04 15:51:25 +03:00
|
|
|
this._locale = options.locale || this.defaultLocale();
|
2021-05-04 16:28:15 +03:00
|
|
|
this._stringMode = options.stringMode || 'dot';
|
2021-05-06 12:38:56 +03:00
|
|
|
this._logging = options.logging || console;
|
|
|
|
|
2020-03-19 17:07:20 +03:00
|
|
|
this._strings = null;
|
|
|
|
}
|
2015-07-18 07:07:42 +03:00
|
|
|
|
2021-05-06 21:51:38 +03:00
|
|
|
/**
|
|
|
|
* BasePath getter & setter used for testing
|
|
|
|
*/
|
|
|
|
set basePath(basePath) {
|
|
|
|
this._basePath = basePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Need to call init after this
|
|
|
|
*/
|
|
|
|
get basePath() {
|
|
|
|
return this._basePath;
|
|
|
|
}
|
|
|
|
|
2020-03-19 17:07:20 +03:00
|
|
|
/**
|
|
|
|
* English is our default locale
|
|
|
|
*/
|
|
|
|
defaultLocale() {
|
|
|
|
return 'en';
|
|
|
|
}
|
2018-01-09 16:50:57 +03:00
|
|
|
|
2020-03-19 17:07:20 +03:00
|
|
|
supportedLocales() {
|
|
|
|
return [this.defaultLocale()];
|
|
|
|
}
|
2018-01-09 16:50:57 +03:00
|
|
|
|
2020-03-19 17:07:20 +03:00
|
|
|
/**
|
|
|
|
* Exporting the current locale (e.g. "en") to make it available for other files as well,
|
|
|
|
* such as core/frontend/helpers/date.js and core/frontend/helpers/lang.js
|
|
|
|
*/
|
|
|
|
locale() {
|
|
|
|
return this._locale;
|
|
|
|
}
|
2015-07-18 07:07:42 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to find and compile the given data context with a proper string resource.
|
|
|
|
*
|
2020-10-20 02:02:56 +03:00
|
|
|
* @param {string} translationPath Path within 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}
|
|
|
|
*/
|
2020-10-20 02:02:56 +03:00
|
|
|
t(translationPath, bindings) {
|
2020-04-29 18:44:27 +03:00
|
|
|
let string;
|
|
|
|
let msg;
|
2018-01-09 16:50:57 +03:00
|
|
|
|
2020-10-20 02:02:56 +03:00
|
|
|
string = this._findString(translationPath);
|
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) {
|
2020-03-19 17:07:20 +03:00
|
|
|
msg.push(this._formatMessage(s, bindings));
|
2015-07-18 07:07:42 +03:00
|
|
|
});
|
|
|
|
} else {
|
2020-03-19 17:07:20 +03:00
|
|
|
msg = this._formatMessage(string, bindings);
|
|
|
|
}
|
2017-12-04 19:13:12 +03:00
|
|
|
|
2020-03-19 17:07:20 +03:00
|
|
|
return msg;
|
|
|
|
}
|
2017-12-04 19:13:12 +03:00
|
|
|
|
2020-03-19 17:07:20 +03:00
|
|
|
/**
|
|
|
|
* Setup i18n support:
|
|
|
|
* - Load proper language file into memory
|
|
|
|
*/
|
|
|
|
init() {
|
2021-05-04 16:28:15 +03:00
|
|
|
this._strings = this._loadStrings();
|
|
|
|
|
|
|
|
this._initializeIntl();
|
|
|
|
}
|
|
|
|
|
2021-05-05 18:24:38 +03:00
|
|
|
/**
|
|
|
|
* Attempt to load strings from a file
|
|
|
|
*
|
|
|
|
* @param {sting} [locale]
|
|
|
|
* @returns {object} strings
|
|
|
|
*/
|
|
|
|
_loadStrings(locale) {
|
|
|
|
locale = locale || this.locale();
|
|
|
|
|
2020-03-19 17:07:20 +03:00
|
|
|
try {
|
2021-05-05 18:24:38 +03:00
|
|
|
return this._readTranslationsFile(locale);
|
2020-03-19 17:07:20 +03:00
|
|
|
} catch (err) {
|
2021-05-05 18:24:38 +03:00
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
this._handleMissingFileError(locale, err);
|
|
|
|
|
|
|
|
if (locale !== this.defaultLocale()) {
|
|
|
|
this._handleFallbackToDefault();
|
|
|
|
return this._loadStrings(this.defaultLocale());
|
|
|
|
}
|
|
|
|
} else if (err instanceof SyntaxError) {
|
|
|
|
this._handleInvalidFileError(locale, err);
|
|
|
|
} else {
|
|
|
|
throw err;
|
|
|
|
}
|
2015-07-18 07:07:42 +03:00
|
|
|
|
2021-05-05 18:24:38 +03:00
|
|
|
// At this point we've done all we can and strings must be an object
|
|
|
|
return {};
|
|
|
|
}
|
2020-03-19 17:07:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a key exists in the loaded strings
|
|
|
|
* @param {String} msgPath
|
|
|
|
*/
|
|
|
|
doesTranslationKeyExist(msgPath) {
|
|
|
|
const translation = this._findString(msgPath, {log: false});
|
|
|
|
return translation !== this._fallbackError();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-04 16:28:15 +03:00
|
|
|
* Do the lookup within the JSON file using jsonpath
|
2020-03-19 17:07:20 +03:00
|
|
|
*
|
|
|
|
* @param {String} msgPath
|
|
|
|
*/
|
|
|
|
_getCandidateString(msgPath) {
|
2021-05-04 16:28:15 +03:00
|
|
|
// Our default string mode is "dot" for dot-notation, e.g. $.something.like.this used in the backend
|
|
|
|
// Both jsonpath's dot-notation and bracket-notation start with '$' E.g.: $.store.book.title or $['store']['book']['title']
|
|
|
|
// While bracket-notation allows any Unicode characters in keys (i.e. for themes / fulltext mode) E.g. $['Read more']
|
|
|
|
// dot-notation allows only word characters in keys for backend messages (that is \w or [A-Za-z0-9_] in RegExp)
|
2020-10-20 02:02:56 +03:00
|
|
|
let jsonPath = `$.${msgPath}`;
|
2021-05-04 16:28:15 +03:00
|
|
|
let fallback = null;
|
|
|
|
|
|
|
|
if (this._stringMode === 'fulltext') {
|
|
|
|
jsonPath = jp.stringify(['$', msgPath]);
|
|
|
|
// In fulltext mode we can use the passed string as a fallback
|
|
|
|
fallback = msgPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return jp.value(this._strings, jsonPath) || fallback;
|
2021-05-05 18:24:38 +03:00
|
|
|
} catch (err) {
|
|
|
|
this._handleInvalidKeyError(msgPath, err);
|
2021-05-04 16:28:15 +03:00
|
|
|
}
|
2020-03-19 17:07:20 +03:00
|
|
|
}
|
2015-07-18 07:07:42 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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}
|
|
|
|
*/
|
2020-03-19 17:07:20 +03:00
|
|
|
_findString(msgPath, opts) {
|
2018-09-10 15:39:50 +03:00
|
|
|
const options = merge({log: true}, opts || {});
|
2020-04-29 18:44:27 +03:00
|
|
|
let candidateString;
|
|
|
|
let matchingString;
|
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)) {
|
2021-05-05 18:24:38 +03:00
|
|
|
this._handleEmptyKeyError();
|
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
|
2020-03-19 17:07:20 +03:00
|
|
|
if (isNil(this._strings)) {
|
2021-05-05 18:24:38 +03:00
|
|
|
this._handleUninitialisedError(msgPath);
|
2015-11-12 15:29:45 +03:00
|
|
|
}
|
|
|
|
|
2020-03-19 17:07:20 +03:00
|
|
|
candidateString = this._getCandidateString(msgPath);
|
2018-01-09 16:50:57 +03:00
|
|
|
|
|
|
|
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) {
|
2021-05-05 18:24:38 +03:00
|
|
|
this._handleMissingKeyError(msgPath);
|
2017-11-14 16:03:48 +03:00
|
|
|
}
|
|
|
|
|
2020-03-19 17:07:20 +03:00
|
|
|
matchingString = this._fallbackError();
|
2015-07-18 07:07:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return matchingString;
|
2020-03-19 17:07:20 +03:00
|
|
|
}
|
2015-07-18 07:07:42 +03:00
|
|
|
|
2021-05-05 18:24:38 +03:00
|
|
|
_translationFileDirs() {
|
2021-05-06 21:51:38 +03:00
|
|
|
return [this.basePath];
|
2021-05-05 18:24:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we are passed a locale, use that, else use this.locale
|
|
|
|
_translationFileName(locale) {
|
|
|
|
return `${locale || this.locale()}.json`;
|
|
|
|
}
|
|
|
|
|
2020-03-19 17:07:20 +03:00
|
|
|
/**
|
2021-05-05 18:24:38 +03:00
|
|
|
* Read the translations file
|
2020-03-19 17:07:20 +03:00
|
|
|
* Error handling to be done by consumer
|
|
|
|
*
|
2021-05-05 18:24:38 +03:00
|
|
|
* @param {string} locale
|
2020-03-19 17:07:20 +03:00
|
|
|
*/
|
2021-05-05 18:24:38 +03:00
|
|
|
_readTranslationsFile(locale) {
|
|
|
|
const filePath = path.join(...this._translationFileDirs(), this._translationFileName(locale));
|
|
|
|
const content = fs.readFileSync(filePath);
|
2020-03-19 17:07:20 +03:00
|
|
|
return JSON.parse(content);
|
|
|
|
}
|
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
|
|
|
/**
|
2020-03-19 17:07:20 +03:00
|
|
|
* Format the string using the correct locale and applying any bindings
|
|
|
|
* @param {String} string
|
|
|
|
* @param {Object} bindings
|
2015-07-18 07:07:42 +03:00
|
|
|
*/
|
2020-03-19 17:07:20 +03:00
|
|
|
_formatMessage(string, bindings) {
|
|
|
|
let currentLocale = this.locale();
|
|
|
|
let msg = new MessageFormat(string, currentLocale);
|
2016-05-25 10:34:46 +03:00
|
|
|
|
|
|
|
try {
|
2020-03-19 17:07:20 +03:00
|
|
|
msg = msg.format(bindings);
|
2016-05-25 10:34:46 +03:00
|
|
|
} catch (err) {
|
2021-05-05 18:24:38 +03:00
|
|
|
this._handleFormatError(err);
|
2020-03-19 17:07:20 +03:00
|
|
|
|
|
|
|
// fallback
|
|
|
|
msg = new MessageFormat(this._fallbackError(), currentLocale);
|
|
|
|
msg = msg.format();
|
2016-05-25 10:34:46 +03:00
|
|
|
}
|
|
|
|
|
2020-03-19 17:07:20 +03:00
|
|
|
return msg;
|
|
|
|
}
|
2015-07-18 07:07:42 +03:00
|
|
|
|
2018-01-09 16:50:57 +03:00
|
|
|
/**
|
2020-03-19 17:07:20 +03:00
|
|
|
* [Private] Setup i18n support:
|
|
|
|
* - Polyfill node.js if it does not have Intl support or support for a particular locale
|
2018-01-09 16:50:57 +03:00
|
|
|
*/
|
2020-03-19 17:07:20 +03:00
|
|
|
_initializeIntl() {
|
2020-04-29 18:44:27 +03:00
|
|
|
let hasBuiltInLocaleData;
|
|
|
|
let IntlPolyfill;
|
2020-03-19 17:07:20 +03:00
|
|
|
|
|
|
|
if (global.Intl) {
|
|
|
|
// Determine if the built-in `Intl` has the locale data we need.
|
|
|
|
hasBuiltInLocaleData = this.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;
|
2018-01-09 16:50:57 +03:00
|
|
|
}
|
2020-03-19 17:07:20 +03:00
|
|
|
} else {
|
|
|
|
// No `Intl`, so use and load the polyfill.
|
|
|
|
global.Intl = require('intl');
|
2018-01-09 16:50:57 +03:00
|
|
|
}
|
2020-03-19 17:07:20 +03:00
|
|
|
}
|
2018-01-09 16:50:57 +03:00
|
|
|
|
2021-05-05 18:24:38 +03:00
|
|
|
_handleUninitialisedError(key) {
|
2021-05-06 16:02:52 +03:00
|
|
|
this._logging.warn(`i18n was used before it was initialised with key ${key}`);
|
|
|
|
this.init();
|
2021-05-05 18:24:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_handleFormatError(err) {
|
2021-05-06 12:38:56 +03:00
|
|
|
this._logging.error(err.message);
|
2021-05-05 18:24:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_handleFallbackToDefault() {
|
2021-05-06 12:38:56 +03:00
|
|
|
this._logging.warn(`i18n is falling back to ${this.defaultLocale()}.json.`);
|
2021-05-05 18:24:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_handleMissingFileError(locale) {
|
2021-05-06 12:38:56 +03:00
|
|
|
this._logging.warn(`i18n was unable to find ${locale}.json.`);
|
2021-05-05 18:24:38 +03:00
|
|
|
}
|
|
|
|
_handleInvalidFileError(locale, err) {
|
2021-05-06 12:38:56 +03:00
|
|
|
this._logging.error(new errors.IncorrectUsageError({
|
2021-05-05 18:24:38 +03:00
|
|
|
err,
|
|
|
|
message: `i18n was unable to parse ${locale}.json. Please check that it is valid JSON.`
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleEmptyKeyError() {
|
2021-05-06 12:38:56 +03:00
|
|
|
this._logging.warn('i18n.t() was called without a key');
|
2021-05-05 18:24:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_handleMissingKeyError(key) {
|
2021-05-06 12:38:56 +03:00
|
|
|
this._logging.error(new errors.IncorrectUsageError({
|
2021-05-05 18:24:38 +03:00
|
|
|
message: `i18n.t() was called with a key that could not be found: ${key}`
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
_handleInvalidKeyError(key, err) {
|
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
err,
|
|
|
|
message: `i18n.t() called with an invalid key: ${key}`
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:50:57 +03:00
|
|
|
/**
|
2020-03-19 17:07:20 +03:00
|
|
|
* A really basic error for if everything goes wrong
|
2018-01-09 16:50:57 +03:00
|
|
|
*/
|
2020-03-19 17:07:20 +03:00
|
|
|
_fallbackError() {
|
|
|
|
return get(this._strings, 'errors.errors.anErrorOccurred', 'An error occurred');
|
2015-07-18 07:07:42 +03:00
|
|
|
}
|
2020-03-19 17:07:20 +03:00
|
|
|
}
|
2015-07-18 07:07:42 +03:00
|
|
|
|
2021-05-04 15:51:25 +03:00
|
|
|
module.exports = I18n;
|