Skip to content

Commit

Permalink
Add default translation file (#1938)
Browse files Browse the repository at this point in the history
* Add default translation file

* remove explicit any
  • Loading branch information
jadmsaadaot authored Aug 1, 2023
1 parent 1447dfd commit a9984bf
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
20 changes: 15 additions & 5 deletions met-web/src/App.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);

Expand All @@ -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}/`);
}
Expand Down Expand Up @@ -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.',
Expand All @@ -104,7 +114,7 @@ const App = () => {
return <MidScreenLoader />;
}

if (!tenant) {
if (!tenant.isLoaded && !tenant.loading) {
return (
<Router>
<Routes>
Expand Down
22 changes: 22 additions & 0 deletions met-web/src/locales/en/default.json
Original file line number Diff line number Diff line change
@@ -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 protected]"
}
}
}
}
6 changes: 2 additions & 4 deletions met-web/src/locales/en/eao.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": "[email protected]"
"ifThreatContact": "If there is a threat/menace in the comments, check the box below. No email will be sent. Contact Sarah Plank at [email protected]"
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions met-web/src/reduxSlices/tenantSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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;
},
},
});
Expand Down

0 comments on commit a9984bf

Please sign in to comment.