snappymail/dev/Common/Translator.js

254 lines
8.4 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';
2016-06-17 07:23:49 +08:00
let I18N_DATA = window.rainloopI18N || {};
2020-08-27 21:45:47 +08:00
const doc = document,
I18N_NOTIFICATION_DATA = {},
I18N_NOTIFICATION_MAP = [
2016-06-17 07:23:49 +08:00
[Notification.InvalidToken, 'NOTIFICATIONS/INVALID_TOKEN'],
[Notification.InvalidToken, 'NOTIFICATIONS/INVALID_TOKEN'],
[Notification.AuthError, 'NOTIFICATIONS/AUTH_ERROR'],
[Notification.AccessError, 'NOTIFICATIONS/ACCESS_ERROR'],
[Notification.ConnectionError, 'NOTIFICATIONS/CONNECTION_ERROR'],
[Notification.CaptchaError, 'NOTIFICATIONS/CAPTCHA_ERROR'],
[Notification.SocialFacebookLoginAccessDisable, 'NOTIFICATIONS/SOCIAL_FACEBOOK_LOGIN_ACCESS_DISABLE'],
[Notification.SocialTwitterLoginAccessDisable, 'NOTIFICATIONS/SOCIAL_TWITTER_LOGIN_ACCESS_DISABLE'],
[Notification.SocialGoogleLoginAccessDisable, 'NOTIFICATIONS/SOCIAL_GOOGLE_LOGIN_ACCESS_DISABLE'],
[Notification.DomainNotAllowed, 'NOTIFICATIONS/DOMAIN_NOT_ALLOWED'],
[Notification.AccountNotAllowed, 'NOTIFICATIONS/ACCOUNT_NOT_ALLOWED'],
[Notification.AccountTwoFactorAuthRequired, 'NOTIFICATIONS/ACCOUNT_TWO_FACTOR_AUTH_REQUIRED'],
[Notification.AccountTwoFactorAuthError, 'NOTIFICATIONS/ACCOUNT_TWO_FACTOR_AUTH_ERROR'],
[Notification.CouldNotSaveNewPassword, 'NOTIFICATIONS/COULD_NOT_SAVE_NEW_PASSWORD'],
[Notification.CurrentPasswordIncorrect, 'NOTIFICATIONS/CURRENT_PASSWORD_INCORRECT'],
[Notification.NewPasswordShort, 'NOTIFICATIONS/NEW_PASSWORD_SHORT'],
[Notification.NewPasswordWeak, 'NOTIFICATIONS/NEW_PASSWORD_WEAK'],
[Notification.NewPasswordForbidden, 'NOTIFICATIONS/NEW_PASSWORD_FORBIDDENT'],
[Notification.ContactsSyncError, 'NOTIFICATIONS/CONTACTS_SYNC_ERROR'],
[Notification.CantGetMessageList, 'NOTIFICATIONS/CANT_GET_MESSAGE_LIST'],
[Notification.CantGetMessage, 'NOTIFICATIONS/CANT_GET_MESSAGE'],
[Notification.CantDeleteMessage, 'NOTIFICATIONS/CANT_DELETE_MESSAGE'],
[Notification.CantMoveMessage, 'NOTIFICATIONS/CANT_MOVE_MESSAGE'],
[Notification.CantCopyMessage, 'NOTIFICATIONS/CANT_MOVE_MESSAGE'],
[Notification.CantSaveMessage, 'NOTIFICATIONS/CANT_SAVE_MESSAGE'],
[Notification.CantSendMessage, 'NOTIFICATIONS/CANT_SEND_MESSAGE'],
[Notification.InvalidRecipients, 'NOTIFICATIONS/INVALID_RECIPIENTS'],
[Notification.CantSaveFilters, 'NOTIFICATIONS/CANT_SAVE_FILTERS'],
[Notification.CantGetFilters, 'NOTIFICATIONS/CANT_GET_FILTERS'],
[Notification.FiltersAreNotCorrect, 'NOTIFICATIONS/FILTERS_ARE_NOT_CORRECT'],
[Notification.CantCreateFolder, 'NOTIFICATIONS/CANT_CREATE_FOLDER'],
[Notification.CantRenameFolder, 'NOTIFICATIONS/CANT_RENAME_FOLDER'],
[Notification.CantDeleteFolder, 'NOTIFICATIONS/CANT_DELETE_FOLDER'],
[Notification.CantDeleteNonEmptyFolder, 'NOTIFICATIONS/CANT_DELETE_NON_EMPTY_FOLDER'],
[Notification.CantSubscribeFolder, 'NOTIFICATIONS/CANT_SUBSCRIBE_FOLDER'],
[Notification.CantUnsubscribeFolder, 'NOTIFICATIONS/CANT_UNSUBSCRIBE_FOLDER'],
[Notification.CantSaveSettings, 'NOTIFICATIONS/CANT_SAVE_SETTINGS'],
[Notification.CantSavePluginSettings, 'NOTIFICATIONS/CANT_SAVE_PLUGIN_SETTINGS'],
[Notification.DomainAlreadyExists, 'NOTIFICATIONS/DOMAIN_ALREADY_EXISTS'],
[Notification.CantInstallPackage, 'NOTIFICATIONS/CANT_INSTALL_PACKAGE'],
[Notification.CantDeletePackage, 'NOTIFICATIONS/CANT_DELETE_PACKAGE'],
[Notification.InvalidPluginPackage, 'NOTIFICATIONS/INVALID_PLUGIN_PACKAGE'],
[Notification.UnsupportedPluginPackage, 'NOTIFICATIONS/UNSUPPORTED_PLUGIN_PACKAGE'],
[Notification.DemoSendMessageError, 'NOTIFICATIONS/DEMO_SEND_MESSAGE_ERROR'],
[Notification.DemoAccountError, 'NOTIFICATIONS/DEMO_ACCOUNT_ERROR'],
[Notification.AccountAlreadyExists, 'NOTIFICATIONS/ACCOUNT_ALREADY_EXISTS'],
[Notification.AccountDoesNotExist, 'NOTIFICATIONS/ACCOUNT_DOES_NOT_EXIST'],
[Notification.MailServerError, 'NOTIFICATIONS/MAIL_SERVER_ERROR'],
[Notification.InvalidInputArgument, 'NOTIFICATIONS/INVALID_INPUT_ARGUMENT'],
[Notification.UnknownNotification, 'NOTIFICATIONS/UNKNOWN_ERROR'],
[Notification.UnknownError, 'NOTIFICATIONS/UNKNOWN_ERROR']
];
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) {
let valueName = '',
2016-06-30 08:02:45 +08:00
result = I18N_DATA[key];
2016-06-17 07:23:49 +08:00
if (undefined === result) {
result = undefined === defaulValue ? key : defaulValue;
2015-11-15 08:23:16 +08:00
}
if (null != valueList) {
2019-07-05 03:19:24 +08:00
for (valueName in valueList) {
if (Object.prototype.hasOwnProperty.call(valueList, valueName)) {
2016-06-17 07:23:49 +08:00
result = result.replace('%' + valueName + '%', valueList[valueName]);
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
}
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 {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-30 08:02:45 +08:00
/**
* @returns {void}
*/
2019-07-05 03:19:24 +08:00
export function initNotificationLanguage() {
I18N_NOTIFICATION_MAP.forEach(item => I18N_NOTIFICATION_DATA[item[0]] = i18n(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
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
*/
2019-07-05 03:19:24 +08:00
export function getNotification(code, message = '', defCode = null) {
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;
}
defCode = defCode ? parseInt(defCode, 10) || 0 : 0;
return undefined === I18N_NOTIFICATION_DATA[code]
? defCode && undefined === I18N_NOTIFICATION_DATA[defCode]
2019-07-05 03:19:24 +08:00
? I18N_NOTIFICATION_DATA[defCode]
: ''
: I18N_NOTIFICATION_DATA[code];
2016-06-17 07:23:49 +08:00
}
/**
* @param {object} response
* @param {number} defCode = Notification.UnknownNotification
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 getNotificationFromResponse(response, defCode = Notification.UnknownNotification) {
return response && response.ErrorCode
2020-08-27 21:45:47 +08:00
? getNotification(parseInt(response.ErrorCode, 10) || defCode, response.ErrorMessage || '')
2019-07-05 03:19:24 +08:00
: getNotification(defCode);
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 {*} 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) {
2016-06-17 07:23:49 +08:00
let result = '';
switch (parseInt(code, 10) || 0) {
2016-06-17 07:23:49 +08:00
case UploadErrorCode.FileIsTooBig:
result = i18n('UPLOAD/ERROR_FILE_IS_TOO_BIG');
break;
case UploadErrorCode.FilePartiallyUploaded:
result = i18n('UPLOAD/ERROR_FILE_PARTIALLY_UPLOADED');
break;
case UploadErrorCode.FileNoUploaded:
result = i18n('UPLOAD/ERROR_NO_FILE_UPLOADED');
break;
case UploadErrorCode.MissingTempFolder:
result = i18n('UPLOAD/ERROR_MISSING_TEMP_FOLDER');
break;
case UploadErrorCode.FileOnSaveingError:
result = i18n('UPLOAD/ERROR_ON_SAVING_FILE');
break;
case UploadErrorCode.FileType:
result = i18n('UPLOAD/ERROR_FILE_TYPE');
break;
default:
result = i18n('UPLOAD/ERROR_UNKNOWN');
break;
2015-11-15 08:23:16 +08:00
}
2016-06-17 07:23:49 +08:00
return result;
}
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-08-18 03:35:03 +08:00
const start = Date.now();
2015-11-15 08:23:16 +08:00
return new Promise((resolve, reject) => {
return fetch(langLink(language, admin), {cache: 'reload'})
.then(response => {
if (response.ok && response.headers.get('Content-Type').includes('application/javascript')) {
return response.text();
}
reject(new Error('Invalid response'))
}, error => {
reject(new Error(error.message))
})
.then(data => {
2020-08-27 21:45:47 +08:00
var script = doc.createElement('script');
script.text = data;
doc.head.appendChild(script).remove();
setTimeout(
2019-07-05 03:19:24 +08:00
() => {
// reload the data
if (window.rainloopI18N) {
I18N_DATA = window.rainloopI18N || {};
i18nToNodes(doc);
dispatchEvent(new CustomEvent('reload-time'));
trigger(!trigger());
}
window.rainloopI18N = null;
2019-07-05 03:19:24 +08:00
resolve();
},
2020-08-18 03:35:03 +08:00
500 < Date.now() - start ? 1 : 500
2019-07-05 03:19:24 +08:00
);
});
2016-06-17 07:23:49 +08:00
});
2015-11-15 08:23:16 +08:00
}