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';
|
2021-03-25 16:38:45 +08:00
|
|
|
import { doc, createElement } from 'Common/Globals';
|
2016-06-17 07:23:49 +08:00
|
|
|
|
2021-04-28 18:08:07 +08:00
|
|
|
let I18N_DATA = {};
|
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;
|
2021-03-25 04:26:40 +08:00
|
|
|
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
|
|
|
}
|
2021-01-04 19:08:41 +08:00
|
|
|
},
|
|
|
|
|
2021-04-28 18:08:07 +08:00
|
|
|
init = () => {
|
|
|
|
if (rl.I18N) {
|
|
|
|
I18N_DATA = rl.I18N;
|
|
|
|
Date.defineRelativeTimeFormat(rl.relativeTime || {});
|
|
|
|
rl.I18N = null;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-01-27 07:26:31 +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
|
|
|
|
2021-04-28 18:08:07 +08:00
|
|
|
init();
|
|
|
|
|
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) {
|
2020-07-30 03:49:41 +08:00
|
|
|
setTimeout(() =>
|
2020-08-27 21:45:47 +08:00
|
|
|
element.querySelectorAll('[data-i18n]').forEach(item => i18nToNode(item))
|
2020-07-30 03:49:41 +08:00
|
|
|
, 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) {
|
2020-10-15 06:26:40 +08:00
|
|
|
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
|
|
|
|
2021-01-04 19:08:41 +08:00
|
|
|
function getNotificationMessage(code) {
|
|
|
|
let key = getKeyByValue(Notification, code);
|
|
|
|
if (key) {
|
2021-01-27 07:26:31 +08:00
|
|
|
key = i18nKey(key).replace('_NOTIFICATION', '_ERROR');
|
2021-04-12 18:57:35 +08:00
|
|
|
return I18N_DATA.NOTIFICATIONS[key];
|
2021-01-04 19:08:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2021-03-18 19:33:13 +08:00
|
|
|
export function getNotification(code, message = '', defCode = 0) {
|
2020-08-12 06:25:36 +08:00
|
|
|
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;
|
2015-12-15 12:02:30 +08:00
|
|
|
}
|
|
|
|
|
2021-01-04 19:08:41 +08:00
|
|
|
return getNotificationMessage(code)
|
2021-03-18 19:33:13 +08:00
|
|
|
|| getNotificationMessage(parseInt(defCode, 10))
|
2021-01-04 19:08:41 +08:00
|
|
|
|| '';
|
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) {
|
2021-01-04 19:08:41 +08:00
|
|
|
let result = 'UNKNOWN';
|
|
|
|
code = parseInt(code, 10) || 0;
|
|
|
|
switch (code) {
|
2016-06-17 07:23:49 +08:00
|
|
|
case UploadErrorCode.FileIsTooBig:
|
|
|
|
case UploadErrorCode.FilePartiallyUploaded:
|
2021-01-04 19:08:41 +08:00
|
|
|
case UploadErrorCode.NoFileUploaded:
|
2016-06-17 07:23:49 +08:00
|
|
|
case UploadErrorCode.MissingTempFolder:
|
2021-01-04 19:08:41 +08:00
|
|
|
case UploadErrorCode.OnSavingFile:
|
2016-06-17 07:23:49 +08:00
|
|
|
case UploadErrorCode.FileType:
|
2021-01-27 07:26:31 +08:00
|
|
|
result = i18nKey(getKeyByValue(UploadErrorCode, code));
|
2016-06-17 07:23:49 +08:00
|
|
|
break;
|
2015-11-15 08:23:16 +08:00
|
|
|
}
|
2021-01-04 19:08:41 +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) {
|
2020-07-24 02:35:37 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2021-03-25 16:38:45 +08:00
|
|
|
const script = createElement('script');
|
2020-10-15 18:59:56 +08:00
|
|
|
script.onload = () => {
|
|
|
|
// reload the data
|
2021-04-28 18:08:07 +08:00
|
|
|
if (init()) {
|
2020-10-15 18:59:56 +08:00
|
|
|
i18nToNodes(doc);
|
2021-07-19 20:26:32 +08:00
|
|
|
admin || rl.app.reloadTime();
|
2020-10-15 18:59:56 +08:00
|
|
|
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);
|
2016-06-17 07:23:49 +08:00
|
|
|
});
|
2015-11-15 08:23:16 +08:00
|
|
|
}
|
2021-01-26 05:00:13 +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,
|
2021-01-26 05:00:13 +08:00
|
|
|
null,
|
|
|
|
language
|
|
|
|
);
|
|
|
|
}
|