(cherry picked from commit ad2721dc55f3233e4c299babe5744418bc530418) Closes #2670 Closes #2674 Closes #2679pull/2682/head
parent
8bb52105fd
commit
f7c135faaf
@ -1,30 +1,36 @@
|
|||||||
import createAjaxRequest from 'Utilities/createAjaxRequest';
|
import createAjaxRequest from 'Utilities/createAjaxRequest';
|
||||||
|
|
||||||
function getTranslations() {
|
function getTranslations() {
|
||||||
let localization = null;
|
return createAjaxRequest({
|
||||||
const ajaxOptions = {
|
global: false,
|
||||||
async: false,
|
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
url: '/localization',
|
url: '/localization'
|
||||||
success: function(data) {
|
}).request;
|
||||||
localization = data.Strings;
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
createAjaxRequest(ajaxOptions);
|
let translations = {};
|
||||||
|
|
||||||
return localization;
|
export function fetchTranslations() {
|
||||||
|
return new Promise(async(resolve) => {
|
||||||
|
try {
|
||||||
|
const data = await getTranslations();
|
||||||
|
translations = data.Strings;
|
||||||
|
|
||||||
|
resolve(true);
|
||||||
|
} catch (error) {
|
||||||
|
resolve(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const translations = getTranslations();
|
export default function translate(key, args) {
|
||||||
|
const translation = translations[key] || key;
|
||||||
|
|
||||||
export default function translate(key, args = '') {
|
|
||||||
if (args) {
|
if (args) {
|
||||||
const translatedKey = translate(key);
|
return translation.replace(/\{(\d+)\}/g, (match, index) => {
|
||||||
return translatedKey.replace(/\{(\d+)\}/g, (match, index) => {
|
|
||||||
return args[index];
|
return args[index];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return translations[key] || key;
|
return translation;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue