Revert t function to returning the plain string by default

Add `t.html` function for getting the string with the `lang` attribute
This commit is contained in:
Quincy Morgan
2020-09-22 12:03:29 -04:00
parent a3549f9a76
commit 5435082d9c
132 changed files with 597 additions and 580 deletions
+28 -17
View File
@@ -8,7 +8,7 @@ let _t = _mainLocalizer.t;
export {
_mainLocalizer as localizer,
// export `t` function for ease-of-use
// export `t` functions for ease-of-use
_t as t
};
@@ -221,20 +221,17 @@ export function coreLocalizer() {
return 'other';
}
let _lastUsedTCode;
/**
* Given a string identifier, try to find that string in the current
* language, and return it. This function will be called recursively
* with locale `en` if a string can not be found in the requested language.
* Try to find that string in `locale` or the current `_localeCode` matching
* the given `stringId`. If no string can be found in the requested locale,
* we'll recurse down all the `_localeCodes` until one is found.
*
* @param {string} stringId string identifier
* @param {object?} replacements token replacements and default string
* @param {string?} locale locale to use (defaults to currentLocale)
* @return {string?} localized string
*/
localizer.t = function(stringId, replacements, locale) {
_lastUsedTCode = null;
localizer.tInfo = function(stringId, replacements, locale) {
locale = locale || _localeCode;
@@ -291,11 +288,10 @@ export function coreLocalizer() {
}
if (typeof result === 'string') {
// found a localized string!
_lastUsedTCode = locale;
if (replacements && replacements.html === false) {
return result;
}
return `<span class="localized-text" lang="${locale}">${result}</span>`;
return {
text: result,
locale: locale
};
}
}
// no localized string found...
@@ -305,21 +301,36 @@ export function coreLocalizer() {
if (index >= 0 && index < _localeCodes.length - 1) {
// eventually this will be 'en' or another locale with 100% coverage
let fallback = _localeCodes[index + 1];
return localizer.t(stringId, replacements, fallback);
return localizer.tInfo(stringId, replacements, fallback);
}
if (replacements && 'default' in replacements) {
// Fallback to a default value if one is specified in `replacements`
return replacements.default;
return {
text: replacements.default,
locale: null
};
}
const missing = `Missing ${locale} translation: ${stringId}`;
if (typeof console !== 'undefined') console.error(missing); // eslint-disable-line
return missing;
return {
text: missing,
locale: 'en'
};
};
localizer.t.lastCode = () => _lastUsedTCode;
// Returns only the localized text, discarding the locale info
localizer.t = function(stringId, replacements, locale) {
return localizer.tInfo(stringId, replacements, locale).text;
};
// Returns the localized text wrapped in an HTML element encoding the locale info
localizer.t.html = function(stringId, replacements, locale) {
const info = localizer.tInfo(stringId, replacements, locale);
return `<span class="localized-text" lang="${info.locale || 'unknown'}">${info.text}</span>`;
};
localizer.languageName = (code, options) => {