snappymail/dev/Common/Translator.js

158 lines
3.6 KiB
JavaScript
Raw Normal View History

2015-11-15 08:23:16 +08:00
import ko from 'ko';
2019-07-05 03:19:24 +08:00
import { Notification, UploadErrorCode } from 'Common/Enums';
import { langLink } from 'Common/Links';
import { doc, createElement } from 'Common/Globals';
import { getKeyByValue, forEachObjectEntry } from 'Common/Utils';
2016-06-17 07:23:49 +08:00
let I18N_DATA = {};
2016-06-17 07:23:49 +08:00
2021-08-20 21:40:07 +08:00
const
i18nToNode = element => {
const key = element.dataset.i18n;
if (key) {
if ('[' === key.slice(0, 1)) {
switch (key.slice(0, 6)) {
2021-08-20 21:40:07 +08:00
case '[html]':
element.innerHTML = i18n(key.slice(6));
2021-08-20 21:40:07 +08:00
break;
case '[place':
element.placeholder = i18n(key.slice(13));
2021-08-20 21:40:07 +08:00
break;
case '[title':
element.title = i18n(key.slice(7));
2021-08-20 21:40:07 +08:00
break;
// no default
}
} else {
element.textContent = i18n(key);
2015-11-15 08:23:16 +08:00
}
2016-06-17 07:23:49 +08:00
}
2021-08-20 21:40:07 +08:00
},
init = () => {
if (rl.I18N) {
I18N_DATA = rl.I18N;
Date.defineRelativeTimeFormat(rl.relativeTime || {});
rl.I18N = null;
return 1;
}
},
i18nKey = key => key.replace(/([a-z])([A-Z])/g, '$1_$2').toUpperCase(),
2021-08-20 21:40:07 +08:00
getNotificationMessage = code => {
let key = getKeyByValue(Notification, code);
if (key) {
key = i18nKey(key).replace('_NOTIFICATION', '_ERROR');
return I18N_DATA.NOTIFICATIONS[key];
}
};
export const
trigger = ko.observable(false),
/**
* @param {string} key
* @param {Object=} valueList
* @param {string=} defaulValue
* @returns {string}
*/
i18n = (key, valueList, defaulValue) => {
2021-09-14 22:11:50 +08:00
let result = defaulValue || key;
if (key) {
let path = key.split('/');
if (I18N_DATA[path[0]] && path[1]) {
result = I18N_DATA[path[0]][path[1]] || result;
}
2021-08-20 21:40:07 +08:00
}
if (valueList) {
forEachObjectEntry(valueList, (key, value) => {
2021-08-20 21:40:07 +08:00
result = result.replace('%' + key + '%', value);
});
}
return result;
},
2021-08-20 21:40:07 +08:00
/**
* @param {Object} elements
* @param {boolean=} animate = false
*/
i18nToNodes = element =>
setTimeout(() =>
element.querySelectorAll('[data-i18n]').forEach(item => i18nToNode(item))
, 1),
/**
* @param {Function} startCallback
* @param {Function=} langCallback = null
*/
initOnStartOrLangChange = (startCallback, langCallback = null) => {
startCallback && startCallback();
startCallback && trigger.subscribe(startCallback);
langCallback && trigger.subscribe(langCallback);
},
/**
* @param {number} code
* @param {*=} message = ''
* @param {*=} defCode = null
* @returns {string}
*/
getNotification = (code, message = '', defCode = 0) => {
code = parseInt(code, 10) || 0;
if (Notification.ClientViewError === code && message) {
return message;
}
return getNotificationMessage(code)
|| getNotificationMessage(parseInt(defCode, 10))
|| '';
},
/**
* @param {*} code
* @returns {string}
*/
getUploadErrorDescByCode = code => {
let key = getKeyByValue(UploadErrorCode, parseInt(code, 10));
return i18n('UPLOAD/ERROR_' + (key ? i18nKey(key) : 'UNKNOWN'));
2021-08-20 21:40:07 +08:00
},
/**
* @param {boolean} admin
* @param {string} language
*/
translatorReload = (admin, language) =>
2021-08-20 21:40:07 +08:00
new Promise((resolve, reject) => {
const script = createElement('script');
script.onload = () => {
// reload the data
if (init()) {
i18nToNodes(doc);
admin || rl.app.reloadTime();
trigger(!trigger());
}
script.remove();
resolve();
};
script.onerror = () => reject(new Error('Language '+language+' failed'));
script.src = langLink(language, admin);
// script.async = true;
doc.head.append(script);
}),
/**
*
* @param {string} language
* @param {boolean=} isEng = false
* @returns {string}
*/
convertLangName = (language, isEng = false) =>
i18n(
'LANGS_NAMES' + (true === isEng ? '_EN' : '') + '/' + language,
null,
language
);
init();