Skip to content

Commit 1ff5b29

Browse files
committed
🌐(frontend) add currentLocale to CunninghamProvider
In order to have the text of components from the Cunningham library translated, we need to pass the current locale to the CunninghamProvider. We need to create a new ThemeProvider component that will wrap the CunninghamProvider in order to have react-query fully loaded.
1 parent 8821338 commit 1ff5b29

File tree

4 files changed

+80
-6
lines changed

4 files changed

+80
-6
lines changed

src/frontend/apps/e2e/__tests__/app-impress/help.spec.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { expect, test } from '@playwright/test';
22

3-
import { overrideConfig } from './utils-common';
3+
import {
4+
TestLanguage,
5+
overrideConfig,
6+
waitForLanguageSwitch,
7+
} from './utils-common';
48

59
test.describe('Help feature', () => {
610
test.beforeEach(async ({ page }) => {
@@ -97,5 +101,30 @@ test.describe('Help feature', () => {
97101
await page.getByRole('button', { name: /skip/i }).click();
98102
await expect(modal).toBeHidden();
99103
});
104+
105+
test('Modal onboarding translated correctly', async ({ page }) => {
106+
// switch to french
107+
await waitForLanguageSwitch(page, TestLanguage.French);
108+
109+
await page.getByRole('button', { name: 'Open onboarding menu' }).click();
110+
111+
await page.getByRole('menuitem', { name: 'Onboarding' }).click();
112+
113+
const modal = page.getByLabel('Apprenez les principes fondamentaux');
114+
115+
await expect(modal.getByText('Découvrez Docs')).toBeVisible();
116+
await expect(
117+
modal.getByText(/Rédigez votre document facilement/).first(),
118+
).toBeVisible();
119+
await expect(
120+
modal.getByText(/Déplacez, dupliquez/).first(),
121+
).toBeVisible();
122+
await expect(
123+
modal.getByRole('button', { name: /Passer/i }),
124+
).toBeVisible();
125+
await expect(
126+
modal.getByRole('button', { name: /Suivant/i }),
127+
).toBeVisible();
128+
});
100129
});
101130
});

src/frontend/apps/impress/src/core/AppProvider.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { CunninghamProvider } from '@gouvfr-lasuite/cunningham-react';
21
import {
32
MutationCache,
43
QueryClient,
@@ -7,12 +6,12 @@ import {
76
import { useRouter } from 'next/router';
87
import { useEffect } from 'react';
98

10-
import { useCunninghamTheme } from '@/cunningham';
119
import { Auth, KEY_AUTH, setAuthUrl } from '@/features/auth';
1210
import { useRouteChangeCompleteFocus } from '@/hooks/useRouteChangeCompleteFocus';
1311
import { useResponsiveStore } from '@/stores/';
1412

1513
import { ConfigProvider } from './config/';
14+
import { ThemeProvider } from './config/ThemeProvider';
1615

1716
export const DEFAULT_QUERY_RETRY = 1;
1817

@@ -50,7 +49,6 @@ const queryClient = new QueryClient({
5049
});
5150

5251
export function AppProvider({ children }: { children: React.ReactNode }) {
53-
const { theme } = useCunninghamTheme();
5452
const { replace } = useRouter();
5553
useRouteChangeCompleteFocus();
5654

@@ -74,11 +72,11 @@ export function AppProvider({ children }: { children: React.ReactNode }) {
7472

7573
return (
7674
<QueryClientProvider client={queryClient}>
77-
<CunninghamProvider theme={theme}>
75+
<ThemeProvider>
7876
<ConfigProvider>
7977
<Auth>{children}</Auth>
8078
</ConfigProvider>
81-
</CunninghamProvider>
79+
</ThemeProvider>
8280
</QueryClientProvider>
8381
);
8482
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { CunninghamProvider } from '@gouvfr-lasuite/cunningham-react';
2+
3+
import { useCunninghamTheme } from '@/cunningham';
4+
import { useLocales } from '@/i18n/useLocale';
5+
6+
export function ThemeProvider({ children }: { children: React.ReactNode }) {
7+
const { theme } = useCunninghamTheme();
8+
const currentLocale = useLocales();
9+
10+
return (
11+
<CunninghamProvider theme={theme} currentLocale={currentLocale}>
12+
{children}
13+
</CunninghamProvider>
14+
);
15+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { DEFAULT_LOCALE } from '@gouvfr-lasuite/cunningham-react';
2+
import { useEffect, useState } from 'react';
3+
import { useTranslation } from 'react-i18next';
4+
5+
import { useConfig } from '@/core/config/api/useConfig';
6+
7+
enum Locales {
8+
enUS = 'en-US',
9+
frFR = 'fr-FR',
10+
}
11+
12+
export function useLocales() {
13+
const { i18n } = useTranslation();
14+
const { data: conf } = useConfig();
15+
const [currentLocale, setCurrentLocale] = useState<Locales>(DEFAULT_LOCALE);
16+
const resolvedLanguage = i18n.resolvedLanguage ?? i18n.language;
17+
18+
useEffect(() => {
19+
const rawLocale =
20+
conf?.LANGUAGES.find(([code]) =>
21+
code.startsWith(resolvedLanguage),
22+
)?.[0] ?? resolvedLanguage;
23+
const [lang, region] = rawLocale.split('-');
24+
const currentLocale = region
25+
? `${lang}-${region.toUpperCase()}`
26+
: rawLocale;
27+
28+
setCurrentLocale(currentLocale as Locales);
29+
}, [resolvedLanguage, conf?.LANGUAGES]);
30+
31+
return currentLocale;
32+
}

0 commit comments

Comments
 (0)