snappymail/dev/Common/Translator.jsx

337 lines
9.3 KiB
React
Raw Normal View History

2015-11-15 08:23:16 +08:00
2016-07-02 06:49:59 +08:00
import window from 'window';
import _ from '_';
import $ from '$';
2015-11-15 08:23:16 +08:00
import ko from 'ko';
import {Notification, UploadErrorCode} from 'Common/Enums';
2016-06-17 07:23:49 +08:00
import {pInt, isUnd, isNull, has, microtime, inArray} from 'Common/Utils';
2016-06-07 05:57:52 +08:00
import {$html, bAnimationSupported} from 'Common/Globals';
2016-06-17 07:23:49 +08:00
import {reload as momentorReload} from 'Common/Momentor';
import {langLink} from 'Common/Links';
import Promise from 'Promise';
let I18N_DATA = window.rainloopI18N || {};
const I18N_NOTIFICATION_DATA = {};
const I18N_NOTIFICATION_MAP = [
[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.LicensingServerIsUnavailable, 'NOTIFICATIONS/LICENSING_SERVER_IS_UNAVAILABLE'],
[Notification.LicensingExpired, 'NOTIFICATIONS/LICENSING_EXPIRED'],
[Notification.LicensingBanned, 'NOTIFICATIONS/LICENSING_BANNED'],
[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
*/
export function i18n(key, valueList, defaulValue)
2015-11-15 08:23:16 +08:00
{
2016-06-17 07:23:49 +08:00
let
valueName = '',
2016-06-30 08:02:45 +08:00
result = I18N_DATA[key];
2016-06-17 07:23:49 +08:00
if (isUnd(result))
{
result = isUnd(defaulValue) ? key : defaulValue;
2015-11-15 08:23:16 +08:00
}
2016-06-17 07:23:49 +08:00
if (!isUnd(valueList) && !isNull(valueList))
{
for (valueName in valueList)
2016-04-21 01:12:51 +08:00
{
2016-06-17 07:23:49 +08:00
if (has(valueList, valueName))
2015-11-15 08:23:16 +08:00
{
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;
}
const i18nToNode = (element) => {
2015-11-15 08:23:16 +08:00
2016-06-17 07:23:49 +08:00
const
$el = $(element),
2016-06-30 08:02:45 +08:00
key = $el.data('i18n');
2015-11-15 08:23:16 +08:00
2016-06-17 07:23:49 +08:00
if (key)
{
if ('[' === key.substr(0, 1))
2015-11-15 08:23:16 +08:00
{
2016-06-17 07:23:49 +08:00
switch (key.substr(0, 6))
2015-11-15 08:23:16 +08:00
{
2016-06-17 07:23:49 +08:00
case '[html]':
$el.html(i18n(key.substr(6)));
break;
case '[place':
$el.attr('placeholder', i18n(key.substr(13)));
break;
case '[title':
$el.attr('title', i18n(key.substr(7)));
break;
2016-06-30 08:02:45 +08:00
// no default
2015-11-15 08:23:16 +08:00
}
}
2016-06-17 07:23:49 +08:00
else
{
$el.text(i18n(key));
}
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
*/
export function i18nToNodes(elements, animate = false)
{
_.defer(() => {
2015-11-15 08:23:16 +08:00
2016-06-17 07:23:49 +08:00
$('[data-i18n]', elements).each((index, item) => {
i18nToNode(item);
2015-11-15 08:23:16 +08:00
});
2016-06-17 07:23:49 +08:00
if (animate && bAnimationSupported)
2015-11-15 08:23:16 +08:00
{
2016-06-17 07:23:49 +08:00
$('.i18n-animation[data-i18n]', elements).letterfx({
fx: 'fall fade',
backwards: false,
timing: 50,
fx_duration: '50ms',
letter_end: 'restore',
element_end: 'restore'
});
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
const reloadData = () => {
if (window.rainloopI18N)
{
I18N_DATA = window.rainloopI18N || {};
2015-11-15 08:23:16 +08:00
2016-06-17 07:23:49 +08:00
i18nToNodes(window.document, true);
momentorReload();
trigger(!trigger());
2015-11-15 08:23:16 +08:00
}
2016-06-17 07:23:49 +08:00
window.rainloopI18N = null;
};
2015-11-15 08:23:16 +08:00
2016-06-30 08:02:45 +08:00
/**
* @returns {void}
*/
2016-06-17 07:23:49 +08:00
export function initNotificationLanguage()
{
I18N_NOTIFICATION_MAP.forEach((item) => {
I18N_NOTIFICATION_DATA[item[0]] = i18n(item[1]);
});
}
2015-11-15 08:23:16 +08:00
2016-06-17 07:23:49 +08:00
/**
* @param {Function} callback
* @param {Object} scope
* @param {Function=} langCallback = null
*/
export function initOnStartOrLangChange(callback, scope, langCallback = null)
{
if (callback)
{
callback.call(scope);
2015-11-15 08:23:16 +08:00
}
2016-06-17 07:23:49 +08:00
if (langCallback)
{
trigger.subscribe(() => {
if (callback)
{
callback.call(scope);
}
2015-11-15 08:23:16 +08:00
2016-06-17 07:23:49 +08:00
langCallback.call(scope);
});
}
else if (callback)
{
trigger.subscribe(callback, scope);
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 {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 = null)
{
code = window.parseInt(code, 10) || 0;
if (Notification.ClientViewError === code && message)
{
return message;
}
2016-06-17 07:23:49 +08:00
defCode = defCode ? (window.parseInt(defCode, 10)) || 0 : 0;
return isUnd(I18N_NOTIFICATION_DATA[code]) ? (
defCode && isUnd(I18N_NOTIFICATION_DATA[defCode]) ? I18N_NOTIFICATION_DATA[defCode] : ''
) : I18N_NOTIFICATION_DATA[code];
}
/**
* @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
*/
export function getNotificationFromResponse(response, defCode = Notification.UnknownNotification)
{
return response && response.ErrorCode ?
getNotification(pInt(response.ErrorCode), response.ErrorMessage || '') : getNotification(defCode);
}
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
*/
export function getUploadErrorDescByCode(code)
{
let result = '';
switch (window.parseInt(code, 10) || 0)
{
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
*/
export function reload(admin, language)
{
const start = microtime();
2015-11-15 08:23:16 +08:00
2016-06-17 07:23:49 +08:00
$html.addClass('rl-changing-language');
2015-11-15 08:23:16 +08:00
2016-06-17 07:23:49 +08:00
return new Promise((resolve, reject) => {
2015-11-15 08:23:16 +08:00
$.ajax({
2016-06-17 07:23:49 +08:00
url: langLink(language, admin),
dataType: 'script',
cache: true
}).then(() => {
_.delay(() => {
reloadData();
const isRtl = -1 < inArray(language, ['ar', 'ar_sa', 'he', 'he_he', 'ur', 'ur_ir']);
$html
.removeClass('rl-changing-language')
.removeClass('rl-rtl rl-ltr')
// .attr('dir', isRtl ? 'rtl' : 'ltr')
2016-06-30 08:02:45 +08:00
.addClass(isRtl ? 'rl-rtl' : 'rl-ltr');
2016-06-17 07:23:49 +08:00
resolve();
}, 500 < microtime() - start ? 1 : 500);
}, () => {
$html.removeClass('rl-changing-language');
window.rainloopI18N = null;
reject();
});
});
2015-11-15 08:23:16 +08:00
}
2016-06-17 07:23:49 +08:00
// init section
$html.addClass('rl-' + ($html.attr('dir') || 'ltr'));