Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Frontend]Adding separate translation files #2227

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions openbas-front/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"html-react-parser": "5.2.0",
"html-to-image": "1.11.11",
"http-proxy-middleware": "3.0.3",
"i18n-auto-translation": "1.10.0",
"js-file-download": "0.4.12",
"leaflet": "1.9.4",
"mdi-material-ui": "7.9.2",
Expand Down Expand Up @@ -125,6 +126,7 @@
"node": ">= 22.11.0"
},
"scripts": {
"auto-translation:all": "i18n-auto-translation -a deepl-free -p ./src/utils/lang/en.json -t fr -k $SUBSCRIPTION_KEY && i18n-auto-translation -a deepl-free -p ./src/utils/lang/en.json -t zh -k $SUBSCRIPTION_KEY",
"start": "vite",
"deprecated-start": "node builder/dev/dev.js",
"build": "node builder/prod/prod.js",
Expand Down
24 changes: 12 additions & 12 deletions openbas-front/src/components/AppIntlProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,21 @@ import 'cronstrue/locales/zh_CN';

import { LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFnsV3';
import enLocale from 'date-fns/locale/en-US';
import esLocale from 'date-fns/locale/es';
import frLocale from 'date-fns/locale/fr';
import cnLocale from 'date-fns/locale/zh-CN';
import moment from 'moment';
import * as PropTypes from 'prop-types';
import { useEffect } from 'react';
import { IntlProvider } from 'react-intl';

import { useHelper } from '../store';
import locale, { DEFAULT_LANG } from '../utils/BrowserLanguage';
import i18n from '../utils/Localization';
import enOpenBAS from '../utils/lang/en.json';
import frOpenBAS from '../utils/lang/fr.json';
import zhOpenBAS from '../utils/lang/zh.json';

const localeMap = {
en: enLocale,
fr: frLocale,
es: esLocale,
zh: cnLocale,
const langOpenBAS = {
en: enOpenBAS,
fr: frOpenBAS,
zh: zhOpenBAS,
};

const momentMap = {
Expand All @@ -49,7 +46,10 @@ const AppIntlProvider = (props) => {
return { platformName: name, lang: userLang };
});
LANG = lang;
const baseMessages = i18n.messages[lang] || i18n.messages[DEFAULT_LANG];
const baseMessages = langOpenBAS[lang] || langOpenBAS[DEFAULT_LANG];
/*
passser l'anglais par défaut ou le fichier approprié en fonction de la langue
*/
const momentLocale = momentMap[lang];
moment.locale(momentLocale);
useEffect(() => {
Expand All @@ -70,7 +70,7 @@ const AppIntlProvider = (props) => {
>
<LocalizationProvider
dateAdapter={AdapterDateFns}
adapterLocale={localeMap[lang]}
adapterLocale={langOpenBAS[lang]}
>
{children}
</LocalizationProvider>
Expand Down
16 changes: 12 additions & 4 deletions openbas-front/src/utils/Action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@ import { DATA_FETCH_ERROR } from '../constants/ActionTypes';
import { api } from '../network';
import { store } from '../store';
import { MESSAGING$ } from './Environment';
import i18n from './Localization';
import enOpenBAS from './lang/en.json';
import frOpenBAS from './lang/fr.json';
import zhOpenBAS from './lang/zh.json';

const isEmptyPath = R.isNil(window.BASE_PATH) || R.isEmpty(window.BASE_PATH);
const contextPath = isEmptyPath || window.BASE_PATH === '/' ? '' : window.BASE_PATH;
export const APP_BASE_PATH = isEmptyPath || contextPath.startsWith('/') ? contextPath : `/${contextPath}`;

const cache = createIntlCache();

const langOpenBAS = {
en: enOpenBAS,
fr: frOpenBAS,
zh: zhOpenBAS,
};

export const buildUri = (uri: string) => `${APP_BASE_PATH}${uri}`;

const buildError = (data: AxiosError) => {
Expand All @@ -44,7 +52,7 @@ const buildError = (data: AxiosError) => {
const notifyError = (error: AxiosError) => {
const intl = createIntl({
locale: LANG,
messages: i18n.messages[LANG as keyof typeof i18n.messages],
messages: langOpenBAS[LANG as keyof typeof langOpenBAS],
}, cache);
if (error.status === 401 || error.status === 404) {
// Do not notify the user, as a 401 error will already trigger a disconnection, as 404 already handle inside the app
Expand All @@ -62,10 +70,10 @@ const notifyError = (error: AxiosError) => {
};

const notifySuccess = (message: string) => {
const messages = i18n.messages[LANG as keyof typeof i18n.messages] as Record<string, string>;
const messages = langOpenBAS[LANG as keyof typeof langOpenBAS] as Record<string, string>;
const intl = createIntl({
locale: LANG,
messages: i18n.messages[LANG as keyof typeof i18n.messages],
messages: langOpenBAS[LANG as keyof typeof langOpenBAS],
}, cache);

if (!messages[message]) {
Expand Down
Loading