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');
|
2020-06-12 11:08:44 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
const logging = require('../../../shared/logging');
|
2020-03-19 17:07:20 +03:00
|
|
|
|
|
|
|
class I18n {
|
|
|
|
constructor(locale) {
|
|
|
|
this._locale = locale || this.defaultLocale();
|
|
|
|
this._strings = null;
|
|
|
|
}
|
2015-07-18 07:07:42 +03:00
|
|
|
|
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() {
|
|
|
|
// This function is called during Ghost's initialization.
|
|
|
|
// Reading translation file for messages from core .json files and keeping its content in memory
|
|
|
|
// The English file is always loaded, until back-end translations are enabled in future versions.
|
|
|
|
try {
|
|
|
|
this._strings = this._readStringsFile(__dirname, '..', '..', 'translations', `${this.defaultLocale()}.json`);
|
|
|
|
} catch (err) {
|
|
|
|
this._strings = null;
|
|
|
|
throw err;
|
2015-07-18 07:07:42 +03:00
|
|
|
}
|
|
|
|
|
2020-03-19 17:07:20 +03:00
|
|
|
this._initializeIntl();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do the lookup with JSON path
|
|
|
|
*
|
|
|
|
* @param {String} msgPath
|
|
|
|
*/
|
|
|
|
_getCandidateString(msgPath) {
|
|
|
|
// 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)
|
2020-10-20 02:02:56 +03:00
|
|
|
let jsonPath = `$.${msgPath}`;
|
|
|
|
return jp.value(this._strings, jsonPath);
|
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)) {
|
2020-03-19 17:07:20 +03:00
|
|
|
logging.warn('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
|
2020-03-19 17:07:20 +03:00
|
|
|
if (isNil(this._strings)) {
|
|
|
|
this.init();
|
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) {
|
|
|
|
logging.error(new errors.IncorrectUsageError({
|
|
|
|
message: `i18n error: path "${msgPath}" was not found`
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-03-19 17:07:20 +03:00
|
|
|
/**
|
|
|
|
* Resolve filepath, read file, and attempt a parse
|
|
|
|
* Error handling to be done by consumer
|
|
|
|
*
|
|
|
|
* @param {...String} pathParts
|
|
|
|
*/
|
|
|
|
_readStringsFile(...pathParts) {
|
|
|
|
const content = fs.readFileSync(path.join(...pathParts));
|
|
|
|
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) {
|
2020-03-19 17:07:20 +03:00
|
|
|
logging.error(err.message);
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
2020-03-19 17:07:20 +03:00
|
|
|
module.exports = new I18n();
|
|
|
|
module.exports.I18n = I18n;
|