snappymail/dev/Common/Translator.js

160 lines
3.8 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';
2016-06-17 07:23:49 +08:00
2021-04-12 18:57:35 +08:00
let I18N_DATA = window.snappymailI18N || {};
2016-06-17 07:23:49 +08:00
export const trigger = ko.observable(false);
/**
* @param {string} key
* @param {Object=} valueList
* @param {string=} defaulValue
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-17 07:23:49 +08:00
*/
2019-07-05 03:19:24 +08:00
export function i18n(key, valueList, defaulValue) {
2021-04-12 22:30:39 +08:00
let path = key.split('/');
if (!I18N_DATA[path[0]] || !path[1]) {
return defaulValue || key;
}
let result = I18N_DATA[path[0]][path[1]] || defaulValue || key;
if (valueList) {
Object.entries(valueList).forEach(([key, value]) => {
result = result.replace('%' + key + '%', value);
});
2015-11-15 08:23:16 +08:00
}
2016-06-17 07:23:49 +08:00
return result;
}
2020-08-27 21:45:47 +08:00
const i18nToNode = element => {
const key = element.dataset.i18n;
2019-07-05 03:19:24 +08:00
if (key) {
if ('[' === key.substr(0, 1)) {
switch (key.substr(0, 6)) {
2016-06-17 07:23:49 +08:00
case '[html]':
2020-08-27 21:45:47 +08:00
element.innerHTML = i18n(key.substr(6));
2016-06-17 07:23:49 +08:00
break;
case '[place':
2020-08-27 21:45:47 +08:00
element.placeholder = i18n(key.substr(13));
2016-06-17 07:23:49 +08:00
break;
case '[title':
2020-08-27 21:45:47 +08:00
element.title = i18n(key.substr(7));
2016-06-17 07:23:49 +08:00
break;
2016-06-30 08:02:45 +08:00
// no default
2015-11-15 08:23:16 +08:00
}
2019-07-05 03:19:24 +08:00
} else {
2020-08-27 21:45:47 +08:00
element.textContent = i18n(key);
2016-06-17 07:23:49 +08:00
}
2015-11-15 08:23:16 +08:00
}
},
i18nKey = key => key.replace(/([a-z])([A-Z])/g, '$1_$2').toUpperCase(),
getKeyByValue = (o, v) => Object.keys(o).find(key => o[key] === v);
2015-11-15 08:23:16 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {Object} elements
* @param {boolean=} animate = false
*/
2020-08-27 21:45:47 +08:00
export function i18nToNodes(element) {
setTimeout(() =>
2020-08-27 21:45:47 +08:00
element.querySelectorAll('[data-i18n]').forEach(item => i18nToNode(item))
, 1);
2016-06-17 07:23:49 +08:00
}
2015-11-15 08:23:16 +08:00
2016-06-17 07:23:49 +08:00
/**
2016-07-07 07:11:13 +08:00
* @param {Function} startCallback
2016-06-17 07:23:49 +08:00
* @param {Function=} langCallback = null
*/
2019-07-05 03:19:24 +08:00
export function initOnStartOrLangChange(startCallback, langCallback = null) {
startCallback && startCallback();
startCallback && trigger.subscribe(startCallback);
langCallback && trigger.subscribe(langCallback);
2016-06-17 07:23:49 +08:00
}
2015-11-15 08:23:16 +08:00
function getNotificationMessage(code) {
let key = getKeyByValue(Notification, code);
if (key) {
key = i18nKey(key).replace('_NOTIFICATION', '_ERROR');
2021-04-12 18:57:35 +08:00
return I18N_DATA.NOTIFICATIONS[key];
}
}
2016-06-17 07:23:49 +08:00
/**
* @param {number} code
* @param {*=} message = ''
* @param {*=} defCode = null
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-17 07:23:49 +08:00
*/
export function getNotification(code, message = '', defCode = 0) {
code = parseInt(code, 10) || 0;
2019-07-05 03:19:24 +08:00
if (Notification.ClientViewError === code && message) {
2016-06-17 07:23:49 +08:00
return message;
}
return getNotificationMessage(code)
|| getNotificationMessage(parseInt(defCode, 10))
|| '';
2016-06-17 07:23:49 +08:00
}
/**
* @param {*} code
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-17 07:23:49 +08:00
*/
2019-07-05 03:19:24 +08:00
export function getUploadErrorDescByCode(code) {
let result = 'UNKNOWN';
code = parseInt(code, 10) || 0;
switch (code) {
2016-06-17 07:23:49 +08:00
case UploadErrorCode.FileIsTooBig:
case UploadErrorCode.FilePartiallyUploaded:
case UploadErrorCode.NoFileUploaded:
2016-06-17 07:23:49 +08:00
case UploadErrorCode.MissingTempFolder:
case UploadErrorCode.OnSavingFile:
2016-06-17 07:23:49 +08:00
case UploadErrorCode.FileType:
result = i18nKey(getKeyByValue(UploadErrorCode, code));
2016-06-17 07:23:49 +08:00
break;
2015-11-15 08:23:16 +08:00
}
return i18n('UPLOAD/ERROR_' + result);
2016-06-17 07:23:49 +08:00
}
2015-11-15 08:23:16 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {boolean} admin
* @param {string} language
*/
2019-07-05 03:19:24 +08:00
export function reload(admin, language) {
return new Promise((resolve, reject) => {
const script = createElement('script');
2020-10-15 18:59:56 +08:00
script.onload = () => {
// reload the data
2021-04-12 18:57:35 +08:00
if (window.snappymailI18N) {
I18N_DATA = window.snappymailI18N;
2020-10-15 18:59:56 +08:00
i18nToNodes(doc);
dispatchEvent(new CustomEvent('reload-time'));
trigger(!trigger());
}
2021-04-12 18:57:35 +08:00
window.snappymailI18N = null;
2020-10-15 18:59:56 +08:00
script.remove();
resolve();
};
script.onerror = () => reject(new Error('Language '+language+' failed'));
script.src = langLink(language, admin);
// script.async = true;
doc.head.append(script);
2016-06-17 07:23:49 +08:00
});
2015-11-15 08:23:16 +08:00
}
/**
*
* @param {string} language
* @param {boolean=} isEng = false
* @returns {string}
*/
export function convertLangName(language, isEng = false) {
return i18n(
2021-04-12 18:57:35 +08:00
'LANGS_NAMES' + (true === isEng ? '_EN' : '') + '/' + language,
null,
language
);
}