habitica-self-host/common/script/i18n.js

64 lines
1.3 KiB
JavaScript
Raw Normal View History

2015-11-15 14:16:55 +00:00
import _ from 'lodash';
2015-11-12 02:32:00 +00:00
2015-11-15 14:16:55 +00:00
let i18n = {
2015-11-12 02:32:00 +00:00
strings: null,
translations: {},
2015-11-15 14:16:55 +00:00
t, // eslint-disable-line no-use-before-define
};
function t (stringName) {
let vars = arguments[1];
let locale;
if (_.isString(arguments[1])) {
vars = null;
locale = arguments[1];
} else if (arguments[2]) {
locale = arguments[2];
}
let i18nNotSetup = !i18n.strings && !i18n.translations[locale];
if (!locale || i18nNotSetup) {
locale = 'en';
}
let string;
if (i18n.strings) {
string = i18n.strings[stringName];
} else {
string = i18n.translations[locale] && i18n.translations[locale][stringName];
}
let clonedVars = _.clone(vars) || {};
clonedVars.locale = locale;
if (string) {
try {
return _.template(string)(clonedVars);
2015-11-15 14:16:55 +00:00
} catch (_error) {
return 'Error processing the string. Please see Help > Report a Bug.';
2015-11-12 02:32:00 +00:00
}
2015-11-15 14:16:55 +00:00
} else {
let stringNotFound;
if (i18n.strings) {
stringNotFound = i18n.strings.stringNotFound;
} else if (i18n.translations[locale]) {
stringNotFound = i18n.translations[locale] && i18n.translations[locale].stringNotFound;
2015-11-12 02:32:00 +00:00
}
2015-11-15 14:16:55 +00:00
try {
return _.template(stringNotFound)({
2015-11-15 14:16:55 +00:00
string: stringName,
});
} catch (_error) {
return 'Error processing the string. Please see Help > Report a Bug.';
2015-11-12 02:32:00 +00:00
}
}
2015-11-15 14:16:55 +00:00
}
2016-03-03 03:36:27 +00:00
module.exports = i18n;