From 94d859e734326cf62577c7af4a01b3e0dcb52aa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Castillo?= Date: Mon, 2 Sep 2024 15:38:59 -0300 Subject: [PATCH 1/3] Add default error messages for authErrorHandler MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tomás Castillo --- src/utils/actions.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/utils/actions.js b/src/utils/actions.js index 4dd719f..b128737 100644 --- a/src/utils/actions.js +++ b/src/utils/actions.js @@ -101,18 +101,28 @@ export const authErrorHandler = ( let payload, callback; dispatch(stopLoading()); + + const defaultError401 = "Hold on. Your user is not authenticated!."; + const defaultError403 = "Hold on. Your user is not authorized!."; + const defaultError500 = "There was a problem with our server, please contact admin."; + + const translateOrDefault = (key, msg) => { + const translated = T.translate(key); + // If the translation is the same as the key, return the default message + return translated === key ? msg : translated; + }; switch (code) { case 401: if (notifyErrorHandler !== showMessage) { - payload = buildNotifyHandlerErrorPayload(code, "ERROR", T.translate("errors.user_not_auth")); + payload = buildNotifyHandlerErrorPayload(code, "ERROR", translateOrDefault("errors.user_not_auth", defaultError401)); callback = () => dispatch(initLogin()); } else { dispatch(initLogin()); } break; case 403: - payload = buildNotifyHandlerErrorPayload(code, "ERROR", T.translate("errors.user_not_authz")); + payload = buildNotifyHandlerErrorPayload(code, "ERROR", translateOrDefault("errors.user_not_authz", defaultError403)); callback = initLogOut; break; case 404: @@ -131,7 +141,7 @@ export const authErrorHandler = ( payload = buildNotifyHandlerWarningPayload(code, "Validation error", msg); break; default: - payload = buildNotifyHandlerErrorPayload(code, "ERROR", T.translate("errors.server_error")); + payload = buildNotifyHandlerErrorPayload(code, "ERROR", translateOrDefault("errors.server_error", defaultError500)); } if (payload) From a26123dd6484717b9591d60d3d434b31fdf891ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Castillo?= Date: Wed, 4 Sep 2024 11:20:53 -0300 Subject: [PATCH 2/3] Export init18n function, merge app and uicore dictionaries MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tomás Castillo --- src/components/index.js | 21 +++------------------ src/i18n/i18n.js | 37 ++++++++++++++++++++----------------- src/utils/actions.js | 19 +++++++------------ 3 files changed, 30 insertions(+), 47 deletions(-) diff --git a/src/components/index.js b/src/components/index.js index ab999a1..9665807 100644 --- a/src/components/index.js +++ b/src/components/index.js @@ -1,6 +1,5 @@ import React from 'react'; -import T from 'i18n-react'; -import { getCurrentUserLanguage } from '../utils/methods'; +import { initI18n } from '../i18n/i18n.js'; export {default as AjaxLoader} from './ajaxloader'; export {default as RawHTML} from './raw-html'; @@ -60,19 +59,5 @@ export {default as ScheduleBuilderView} from './schedule-builder-view' // export {default as ExtraQuestionsForm } from './extra-questions/index.js'; // export {default as GMap} from './google-map'; -let language = getCurrentUserLanguage(); - -// language would be something like es-ES or es_ES -// However we store our files with format es.json or en.json -// therefore retrieve only the first 2 digits - -if (language.length > 2) { - language = language.split("-")[0]; - language = language.split("_")[0]; -} - -try { - T.setTexts(require(`../i18n/${language}.json`)); -} catch (e) { - T.setTexts(require(`../i18n/en.json`)); -} +// initialize i18n +initI18n(); diff --git a/src/i18n/i18n.js b/src/i18n/i18n.js index b240a41..7d854ff 100644 --- a/src/i18n/i18n.js +++ b/src/i18n/i18n.js @@ -1,28 +1,31 @@ -import {getCurrentUserLanguage} from "../utils/methods"; +import { getCurrentUserLanguage } from "../utils/methods"; import T from "i18n-react"; import en from './en.json'; import zh from './zh.json'; import es from './es.json'; const resources = { - 'en' : en, - 'zh' : zh, - 'es' : es, + 'en': en, + 'zh': zh, + 'es': es, } -let language = getCurrentUserLanguage(); +export const initI18n = (dictionary) => { + let language = getCurrentUserLanguage(); -// language would be something like es-ES or es_ES -// However we store our files with format es.json or en.json -// therefore retrieve only the first 2 digits + // language would be something like es-ES or es_ES + // However we store our files with format es.json or en.json + // therefore retrieve only the first 2 digits -if (language.length > 2) { - language = language.split("-")[0]; - language = language.split("_")[0]; -} + if (language.length > 2) { + language = language.split("-")[0]; + language = language.split("_")[0]; + } -try { - T.setTexts(resources[language]); -} catch (e) { - T.setTexts(resources['en']); -} \ No newline at end of file + const isDictionaryValid = dictionary && typeof dictionary === 'object' && !Array.isArray(dictionary); + try { + T.setTexts(isDictionaryValid ? { ...resources[language], ...dictionary } : resources[language]); + } catch (e) { + T.setTexts(isDictionaryValid ? { ...resources['en'], ...dictionary } : resources['en']); + } +}; \ No newline at end of file diff --git a/src/utils/actions.js b/src/utils/actions.js index b128737..c2dc4df 100644 --- a/src/utils/actions.js +++ b/src/utils/actions.js @@ -19,6 +19,7 @@ import T from "i18n-react/dist/i18n-react"; import { isClearingSessionState, setSessionClearingState, getCurrentPathName } from './methods'; import { CLEAR_SESSION_STATE } from '../components/security/actions'; import { doLogin, initLogOut } from '../components/security/methods'; +import { initI18n } from '../i18n/i18n'; export const GENERIC_ERROR = "Yikes. Something seems to be broken. Our web team has been notified, and we apologize for the inconvenience."; export const RESET_LOADING = 'RESET_LOADING'; @@ -28,6 +29,9 @@ export const VALIDATE = 'VALIDATE'; export const CLEAR_MESSAGE = 'CLEAR_MESSAGE'; export const SHOW_MESSAGE = 'SHOW_MESSAGE'; +// initialize i18n +initI18n(); + export const createAction = type => payload => ({ type, payload @@ -102,27 +106,18 @@ export const authErrorHandler = ( dispatch(stopLoading()); - const defaultError401 = "Hold on. Your user is not authenticated!."; - const defaultError403 = "Hold on. Your user is not authorized!."; - const defaultError500 = "There was a problem with our server, please contact admin."; - - const translateOrDefault = (key, msg) => { - const translated = T.translate(key); - // If the translation is the same as the key, return the default message - return translated === key ? msg : translated; - }; switch (code) { case 401: if (notifyErrorHandler !== showMessage) { - payload = buildNotifyHandlerErrorPayload(code, "ERROR", translateOrDefault("errors.user_not_auth", defaultError401)); + payload = buildNotifyHandlerErrorPayload(code, "ERROR", T.translate("errors.user_not_auth")); callback = () => dispatch(initLogin()); } else { dispatch(initLogin()); } break; case 403: - payload = buildNotifyHandlerErrorPayload(code, "ERROR", translateOrDefault("errors.user_not_authz", defaultError403)); + payload = buildNotifyHandlerErrorPayload(code, "ERROR", T.translate("errors.user_not_authz")); callback = initLogOut; break; case 404: @@ -141,7 +136,7 @@ export const authErrorHandler = ( payload = buildNotifyHandlerWarningPayload(code, "Validation error", msg); break; default: - payload = buildNotifyHandlerErrorPayload(code, "ERROR", translateOrDefault("errors.server_error", defaultError500)); + payload = buildNotifyHandlerErrorPayload(code, "ERROR", T.translate("errors.server_error")); } if (payload) From 90b6476583e08d911d38d3abdf3994d5d89511f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Castillo?= Date: Thu, 5 Sep 2024 16:43:26 -0300 Subject: [PATCH 3/3] Remove i18n from actions file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tomás Castillo --- src/utils/actions.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/utils/actions.js b/src/utils/actions.js index c2dc4df..cb84c3e 100644 --- a/src/utils/actions.js +++ b/src/utils/actions.js @@ -19,7 +19,6 @@ import T from "i18n-react/dist/i18n-react"; import { isClearingSessionState, setSessionClearingState, getCurrentPathName } from './methods'; import { CLEAR_SESSION_STATE } from '../components/security/actions'; import { doLogin, initLogOut } from '../components/security/methods'; -import { initI18n } from '../i18n/i18n'; export const GENERIC_ERROR = "Yikes. Something seems to be broken. Our web team has been notified, and we apologize for the inconvenience."; export const RESET_LOADING = 'RESET_LOADING'; @@ -29,9 +28,6 @@ export const VALIDATE = 'VALIDATE'; export const CLEAR_MESSAGE = 'CLEAR_MESSAGE'; export const SHOW_MESSAGE = 'SHOW_MESSAGE'; -// initialize i18n -initI18n(); - export const createAction = type => payload => ({ type, payload