Cleanup and simplify i18n code

There's no need for I18N_NOTIFICATION_DATA and I18N_NOTIFICATION_MAP
Also bugfix: NEW_PASSWORD_FORBIDDENT => NEW_PASSWORD_FORBIDDEN
This commit is contained in:
djmaze 2021-01-04 12:08:41 +01:00
parent 950579c7f5
commit 0d13484cdc
20 changed files with 45 additions and 142 deletions

View file

@ -8,7 +8,7 @@ import {
import { KeyState } from 'Common/Enums';
import { rootAdmin, rootUser } from 'Common/Links';
import { initOnStartOrLangChange, initNotificationLanguage } from 'Common/Translator';
import { initOnStartOrLangChange } from 'Common/Translator';
import LanguageStore from 'Stores/Language';
import ThemeStore from 'Stores/Theme';
@ -94,7 +94,7 @@ class AbstractApp {
ko.components.register('CheckboxSimple', require('Component/Checkbox').default);
}
initOnStartOrLangChange(initNotificationLanguage);
initOnStartOrLangChange();
if (!mobile) {
// mobile

View file

@ -138,9 +138,9 @@ export const UploadErrorCode = {
Normal: 0,
FileIsTooBig: 1,
FilePartiallyUploaded: 2,
FileNoUploaded: 3,
NoFileUploaded: 3,
MissingTempFolder: 4,
FileOnSaveingError: 5,
OnSavingFile: 5,
FileType: 98,
Unknown: 99
};
@ -353,9 +353,6 @@ export const Notification = {
AccessError: 103,
ConnectionError: 104,
CaptchaError: 105,
SocialFacebookLoginAccessDisable: 106,
SocialTwitterLoginAccessDisable: 107,
SocialGoogleLoginAccessDisable: 108,
DomainNotAllowed: 109,
AccountNotAllowed: 110,
@ -416,8 +413,6 @@ export const Notification = {
JsonParse: 952,
// JsonTimeout: 953,
UnknownNotification: 999,
UnknownNotification: 998,
UnknownError: 999
,getKeyByValue: function(v) { return Object.keys(this).find(key => this[key] === v); }
};

View file

@ -4,74 +4,7 @@ import { langLink } from 'Common/Links';
let I18N_DATA = window.rainloopI18N || {};
const doc = document,
I18N_NOTIFICATION_DATA = {},
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.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']
];
const doc = document;
export const trigger = ko.observable(false);
@ -120,7 +53,9 @@ const i18nToNode = element => {
element.textContent = i18n(key);
}
}
};
},
getKeyByValue = (o, v) => Object.keys(o).find(key => o[key] === v);
/**
* @param {Object} elements
@ -132,13 +67,6 @@ export function i18nToNodes(element) {
, 1);
}
/**
* @returns {void}
*/
export function initNotificationLanguage() {
I18N_NOTIFICATION_MAP.forEach(item => I18N_NOTIFICATION_DATA[item[0]] = i18n(item[1]));
}
/**
* @param {Function} startCallback
* @param {Function=} langCallback = null
@ -149,6 +77,15 @@ export function initOnStartOrLangChange(startCallback, langCallback = null) {
langCallback && trigger.subscribe(langCallback);
}
function getNotificationMessage(code) {
let key = getKeyByValue(Notification, code);
if (key) {
key = key.replace('CantCopyMessage', 'CantMoveMessage').replace('UnknownNotification', 'UnknownError');
key = 'NOTIFICATIONS/' + key.replace(/([a-z])([A-Z])/g, '$1_$2').toUpperCase();
return I18N_DATA[key];
}
}
/**
* @param {number} code
* @param {*=} message = ''
@ -162,11 +99,9 @@ export function getNotification(code, message = '', defCode = null) {
}
defCode = defCode ? parseInt(defCode, 10) || 0 : 0;
return undefined === I18N_NOTIFICATION_DATA[code]
? defCode && undefined === I18N_NOTIFICATION_DATA[defCode]
? I18N_NOTIFICATION_DATA[defCode]
: ''
: I18N_NOTIFICATION_DATA[code];
return getNotificationMessage(code)
|| getNotificationMessage(defCode)
|| '';
}
/**
@ -185,32 +120,19 @@ export function getNotificationFromResponse(response, defCode = Notification.Unk
* @returns {string}
*/
export function getUploadErrorDescByCode(code) {
let result = '';
switch (parseInt(code, 10) || 0) {
let result = 'UNKNOWN';
code = parseInt(code, 10) || 0;
switch (code) {
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.NoFileUploaded:
case UploadErrorCode.MissingTempFolder:
result = i18n('UPLOAD/ERROR_MISSING_TEMP_FOLDER');
break;
case UploadErrorCode.FileOnSaveingError:
result = i18n('UPLOAD/ERROR_ON_SAVING_FILE');
break;
case UploadErrorCode.OnSavingFile:
case UploadErrorCode.FileType:
result = i18n('UPLOAD/ERROR_FILE_TYPE');
break;
default:
result = i18n('UPLOAD/ERROR_UNKNOWN');
result = getKeyByValue(UploadErrorCode, code).replace(/([a-z])([A-Z])/g, '$1_$2').toUpperCase();
break;
}
return result;
return i18n('UPLOAD/ERROR_' + result);
}
/**

View file

@ -1112,7 +1112,7 @@ class ComposePopupView extends AbstractViewNext {
.waiting(false)
.uploading(false)
.complete(true)
.error(getUploadErrorDescByCode(UploadErrorCode.FileNoUploaded));
.error(getUploadErrorDescByCode(UploadErrorCode.NoFileUploaded));
}
});
}