Skip to content

Commit

Permalink
Now using useTranslations independently from cgpv
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-NRCan committed Dec 19, 2023
1 parent 06f2941 commit dc3cfd4
Show file tree
Hide file tree
Showing 6 changed files with 968 additions and 856 deletions.
1,752 changes: 921 additions & 831 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
"ajv-formats": "^2.1.1",
"chart.js": "^4.4.0",
"chartjs-adapter-moment": "^1.0.1",
"i18next": "^23.7.11",
"moment": "^2.29.4",
"react": "^18.2.0",
"react-chartjs-2": "^5.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"react-i18next": "^13.5.0"
},
"devDependencies": {
"@babel/core": "^7.23.2",
Expand Down
3 changes: 2 additions & 1 deletion src/app.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useTranslation } from 'react-i18next';
import { GeoChart } from './chart';
import { GeoChartConfig, ChartType, ChartOptions, ChartData, GeoChartAction, DefaultDataPoint } from './chart-types';
import { SchemaValidator } from './chart-schema-validator';
Expand All @@ -20,7 +21,7 @@ export function App(props: TypeAppProps): JSX.Element {
const w = window as any;
// Fetch the cgpv module
const { cgpv } = w;
const { react, ui, useTranslation } = cgpv;
const { react, ui } = cgpv;
const { useEffect, useState, useCallback } = react;
const { Box } = ui.elements;
const { schemaValidator } = props;
Expand Down
30 changes: 7 additions & 23 deletions src/chart.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { I18nextProvider, useTranslation } from 'react-i18next';
import { Chart as ChartJS, ChartType, ChartOptions, ChartData, ChartDataset, registerables, ChartConfiguration, Plugin } from 'chart.js';
import { Chart as ChartReact } from 'react-chartjs-2';
import 'chartjs-adapter-moment';
Expand Down Expand Up @@ -25,8 +26,6 @@ import { createChartJSOptions, createChartJSData, fetchItemsViaQueryForDatasourc
import { isNumber, downloadJson, getColorFromPalette } from './chart-util';
import { sxClasses } from './chart-style';
import { logHigh, logUseEffectMount, logUseEffectUnmount } from './logger';
import T_EN from '../locales/en/translation.json';
import T_FR from '../locales/fr/translation.json';

// Activate useWhatChanged in development (leaving the code commented, see header of file for reason)
// setUseWhatChange(process.env.NODE_ENV === 'development');
Expand Down Expand Up @@ -129,7 +128,6 @@ export function GeoChart<
const w = window as any;
// Fetch the cgpv module
const { cgpv } = w;
const { useTranslation } = cgpv;
const { useEffect, useState, useRef, useCallback, CSSProperties } = cgpv.react;
const {
Box,
Expand Down Expand Up @@ -1118,22 +1116,6 @@ export function GeoChart<
};
}, [action]);

// Effect hook to be executed with i18n
useEffect(() => {
// Log
const USE_EFFECT_FUNC = 'GEOCHART - USE EFFECT - CURRENT - i18n';
logUseEffectMount(USE_EFFECT_FUNC);

// Add GeoChart translations file
i18n.addResourceBundle('en', 'translation', T_EN);
i18n.addResourceBundle('fr', 'translation', T_FR);

return () => {
// Log
logUseEffectUnmount(USE_EFFECT_FUNC);
};
}, [i18n]);

// #endregion

// #region EVENT HANDLERS SECTION ***********************************************************************************
Expand Down Expand Up @@ -1670,10 +1652,12 @@ export function GeoChart<
*/
const renderEverything = (): JSX.Element => {
return (
<Box sx={sxClasses.mainContainer}>
{!isLoadingChart && renderChartContainer()}
{isLoadingChart && <CircularProgress />}
</Box>
<I18nextProvider i18n={i18n} defaultNS="translation">
<Box sx={sxClasses.mainContainer}>
{!isLoadingChart && renderChartContainer()}
{isLoadingChart && <CircularProgress />}
</Box>
</I18nextProvider>
);
};

Expand Down
34 changes: 34 additions & 0 deletions src/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';

import translationEn from '../locales/en/translation.json';
import translationFr from '../locales/fr/translation.json';

i18n
.use(initReactI18next)
// init i18next
// for all options read: https://www.i18next.com/overview/configuration-options
.init({
debug: false,
lng: 'en',
fallbackLng: ['en', 'fr'],
supportedLngs: ['en', 'fr'],
interpolation: {
escapeValue: false, // not needed for react as it escapes by default
},
resources: {
en: {
translation: translationEn,
},
fr: {
translation: translationFr,
},
},
// special options for react-i18next
// learn more: https://react.i18next.com/components/i18next-instance
react: {
useSuspense: true,
},
});

export default i18n;
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import App from './app';
import { SchemaValidator } from './chart-schema-validator';

// Export the types from the package
export * from './i18n';
export * from './chart-types';
export * from './chart-parsing';
export * from './chart-schema-validator';
Expand Down

0 comments on commit dc3cfd4

Please sign in to comment.