From a9984bff6d6424b4bd69cf8933fcee0437b75545 Mon Sep 17 00:00:00 2001 From: jadmsaadaot <91914654+jadmsaadaot@users.noreply.github.com> Date: Tue, 1 Aug 2023 13:11:58 -0700 Subject: [PATCH] Add default translation file (#1938) * Add default translation file * remove explicit any --- met-web/src/App.tsx | 20 +++++++++++++++----- met-web/src/locales/en/default.json | 22 ++++++++++++++++++++++ met-web/src/locales/en/eao.json | 6 ++---- met-web/src/reduxSlices/tenantSlice.ts | 3 +++ 4 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 met-web/src/locales/en/default.json diff --git a/met-web/src/App.tsx b/met-web/src/App.tsx index 393df08cd..73f10d3a8 100644 --- a/met-web/src/App.tsx +++ b/met-web/src/App.tsx @@ -1,5 +1,5 @@ -import './App.scss'; import React, { useEffect } from 'react'; +import './App.scss'; import { Route, BrowserRouter as Router, Routes } from 'react-router-dom'; import UserService from './services/userService'; import { useAppSelector, useAppDispatch } from './hooks'; @@ -33,6 +33,7 @@ const App = () => { const authenticationLoading = useAppSelector((state) => state.user.authentication.loading); const pathSegments = window.location.pathname.split('/'); const basename = pathSegments[1]; + const language = 'en'; // Default language is English, change as needed const tenant: TenantState = useAppSelector((state) => state.tenant); @@ -46,7 +47,6 @@ const App = () => { }, [basename, AppConfig.apiUrl]); const redirectToDefaultTenant = () => { - console.log('Redirecting to default tenant.'); if (!window.location.toString().includes(DEFAULT_TENANT)) { window.location.replace(`/${DEFAULT_TENANT}/`); } @@ -74,19 +74,29 @@ const App = () => { } }; + const getTranslationFile = async () => { + try { + const translationFile = await import(`./locales/${language}/${basename}.json`); + return translationFile; + } catch (error) { + const defaultTranslationFile = await import(`./locales/${language}/default.json`); + return defaultTranslationFile; + } + }; + const loadTranslation = async () => { if (!tenant.basename) { return; } - const language = 'en'; // Default language is English, change as needed i18n.changeLanguage(language); // Set the language for react-i18next try { - const translationFile = await import(`./locales/${language}/${basename}.json`); + const translationFile = await getTranslationFile(); i18n.addResourceBundle(language, basename, translationFile); dispatch(loadingTenant(false)); } catch (error) { + dispatch(loadingTenant(false)); dispatch( openNotification({ text: 'Error while trying to load texts. Please try again later.', @@ -104,7 +114,7 @@ const App = () => { return ; } - if (!tenant) { + if (!tenant.isLoaded && !tenant.loading) { return ( diff --git a/met-web/src/locales/en/default.json b/met-web/src/locales/en/default.json new file mode 100644 index 000000000..79486e353 --- /dev/null +++ b/met-web/src/locales/en/default.json @@ -0,0 +1,22 @@ +{ + "common": { + "logoUrl": "" + }, + "header": { + "title": "Modern Engagement" + }, + "landing": { + "banner": { + "header": "The Title of The Office", + "description": "Description about the office and public engagement." + } + }, + "comment": { + "admin": { + "review": { + "ifThreatContact": "If there is a threat/menace in the comments, check the box below. No email will be sent. Contact Joan Doe at", + "threatContactEmail": "email@gov.bc.ca" + } + } + } +} diff --git a/met-web/src/locales/en/eao.json b/met-web/src/locales/en/eao.json index 1bf51d675..7fa6eb800 100644 --- a/met-web/src/locales/en/eao.json +++ b/met-web/src/locales/en/eao.json @@ -3,8 +3,7 @@ "logoUrl": "" }, "header": { - "title": "Modern Engagement", - "description": "Public participation in environmental assessments is critical, giving citizens input into decisions on whether or not major projects should proceed, and if they do, what safeguards should be in place." + "title": "Modern Engagement" }, "landing": { "banner": { @@ -15,8 +14,7 @@ "comment": { "admin": { "review": { - "ifThreatContact": "If there is a threat/menace in the comments, check the box below. No email will be sent. Contact Sarah Plank at", - "threatContactEmail": "sarah.plank@gov.bc.ca" + "ifThreatContact": "If there is a threat/menace in the comments, check the box below. No email will be sent. Contact Sarah Plank at sarah.plank@gov.bc.ca" } } } diff --git a/met-web/src/reduxSlices/tenantSlice.ts b/met-web/src/reduxSlices/tenantSlice.ts index 0630715f1..ed3e5a343 100644 --- a/met-web/src/reduxSlices/tenantSlice.ts +++ b/met-web/src/reduxSlices/tenantSlice.ts @@ -5,12 +5,14 @@ export interface TenantState { logoUrl: string; basename: string; loading: boolean; + isLoaded: boolean; } const initialState: TenantState = { name: '', logoUrl: '', basename: '', loading: true, + isLoaded: false, }; export const userSlice = createSlice({ @@ -24,6 +26,7 @@ export const userSlice = createSlice({ state.name = action.payload.name; state.logoUrl = action.payload.logoUrl || ''; state.basename = action.payload.basename; + state.isLoaded = true; }, }, });