Skip to content

Commit e1b4609

Browse files
committed
⚡️(frontend) set html lang attribute dynamically based on current loc
ensures proper language tag is set for accessibility and SEO compliance Signed-off-by: Cyril <[email protected]>
1 parent 30dfea7 commit e1b4609

File tree

3 files changed

+44
-11
lines changed

3 files changed

+44
-11
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { useEffect } from 'react';
2+
import { useTranslation } from 'react-i18next';
3+
4+
export function HtmlLangUpdater() {
5+
const { i18n } = useTranslation();
6+
7+
useEffect(() => {
8+
const lang = i18n.language?.split('-')[0] || 'fr';
9+
document.documentElement.setAttribute('lang', lang);
10+
}, [i18n.language]);
11+
12+
return null;
13+
}

src/frontend/apps/impress/src/pages/_app.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { AppProps } from 'next/app';
22
import Head from 'next/head';
33
import { useTranslation } from 'react-i18next';
44

5+
import { HtmlLangUpdater } from '@/components/HtmlLangUpdater';
56
import { AppProvider } from '@/core/';
67
import { useCunninghamTheme } from '@/cunningham';
78
import { useOffline, useSWRegister } from '@/features/service-worker/';
@@ -49,6 +50,7 @@ export default function App({ Component, pageProps }: AppPropsWithLayout) {
4950
/>
5051
<meta name="viewport" content="width=device-width, initial-scale=1" />
5152
</Head>
53+
<HtmlLangUpdater />
5254
<AppProvider>{getLayout(<Component {...pageProps} />)}</AppProvider>
5355
</>
5456
);
Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
1-
import { Head, Html, Main, NextScript } from 'next/document';
1+
import Document, {
2+
DocumentContext,
3+
Head,
4+
Html,
5+
Main,
6+
NextScript,
7+
} from 'next/document';
28

3-
export default function RootLayout() {
4-
return (
5-
<Html>
6-
<Head />
7-
<body suppressHydrationWarning={process.env.NODE_ENV === 'development'}>
8-
<Main />
9-
<NextScript />
10-
</body>
11-
</Html>
12-
);
9+
class MyDocument extends Document<{ locale: string }> {
10+
static async getInitialProps(ctx: DocumentContext) {
11+
const initialProps = await Document.getInitialProps(ctx);
12+
return {
13+
...initialProps,
14+
locale: ctx.locale || 'fr',
15+
};
16+
}
17+
18+
render() {
19+
return (
20+
<Html lang={this.props.locale}>
21+
<Head />
22+
<body>
23+
<Main />
24+
<NextScript />
25+
</body>
26+
</Html>
27+
);
28+
}
1329
}
30+
31+
export default MyDocument;

0 commit comments

Comments
 (0)