Skip to content
This repository was archived by the owner on Apr 12, 2023. It is now read-only.

Commit 49d0f2f

Browse files
committed
react-i18next
1 parent c4a7e23 commit 49d0f2f

File tree

8 files changed

+147
-55
lines changed

8 files changed

+147
-55
lines changed

components/Menu.tsx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,29 @@ import styles from "./Menu.module.css";
22
import { Panel } from "@navikt/ds-react";
33
import { Helptext, Historic, Information, Send } from "@navikt/ds-icons";
44
import StyledLink from "./StyledLink";
5-
import { useRouter } from "next/router";
6-
import allTexts, { LocalesType } from "../src/allTexts";
5+
import { useTranslation } from "react-i18next";
76

87
export default function Menu() {
98

10-
const router = useRouter();
11-
const texts = allTexts[(router.locale || router.defaultLocale) as keyof LocalesType];
9+
const { t } = useTranslation();
1210

1311
return (
1412
<Panel className={styles.menu}>
1513
<StyledLink href="/">
1614
<Send aria-hidden />
17-
{texts.menuSend}
15+
{t("menuSend")}
1816
</StyledLink>
1917
<StyledLink href="/history">
2018
<Historic aria-hidden />
21-
{texts.menuHistory}
19+
{t("menuHistory")}
2220
</StyledLink>
2321
<StyledLink href="/about">
2422
<Information aria-hidden />
25-
{texts.menuAbout}
23+
{t("menuAbout")}
2624
</StyledLink>
2725
<StyledLink href="/faq">
2826
<Helptext aria-hidden />
29-
{texts.menuFaq}
27+
{t("menuFaq")}
3028
</StyledLink>
3129
</Panel>
3230
);

next.config.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22
const nextConfig = {
33
reactStrictMode: true,
44
swcMinify: true,
5-
output: 'standalone',
6-
i18n: {
7-
locales: ['nb', 'en'],
8-
defaultLocale: 'nb',
9-
localeDetection: false
10-
},
5+
output: 'standalone'
116
};
127

138
module.exports = nextConfig;

package-lock.json

Lines changed: 92 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"next": "13.0.5",
1919
"react": "18.2.0",
2020
"react-dom": "18.2.0",
21+
"react-i18next": "^12.1.1",
2122
"uuid": "^9.0.0"
2223
},
2324
"devDependencies": {

src/allTexts.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/pages/_app.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,47 @@ import "../../styles/globals.css";
22
import "@navikt/ds-css";
33

44
import type { AppProps } from "next/app";
5+
import { onLanguageSelect } from "@navikt/nav-dekoratoren-moduler";
6+
7+
import i18n from 'i18next';
8+
import { initReactI18next } from 'react-i18next';
9+
10+
i18n
11+
// pass the i18n instance to react-i18next.
12+
.use(initReactI18next)
13+
// init i18next
14+
// for all options read: https://www.i18next.com/overview/configuration-options
15+
.init({
16+
debug: true,
17+
fallbackLng: 'nb',
18+
interpolation: {
19+
escapeValue: false, // not needed for react as it escapes by default
20+
},
21+
resources: {
22+
en: {
23+
translation: {
24+
"menuSend": "Send",
25+
"menuHistory": "Previous report cards",
26+
"menuAbout": "About",
27+
"menuFaq": "FAQ",
28+
"title": "New solution"
29+
}
30+
},
31+
nb: {
32+
translation: {
33+
"menuSend": "Send",
34+
"menuHistory": "Tidligere meldekort",
35+
"menuAbout": "Om meldekort",
36+
"menuFaq": "Ofte stilte spørsmål",
37+
"title": "Ny løsning"
38+
}
39+
}
40+
}
41+
});
42+
43+
onLanguageSelect((language) => {
44+
i18n.changeLanguage(language.locale);
45+
});
546

647
export default function App({ Component, pageProps }: AppProps) {
748
return <Component {...pageProps} />;

src/pages/_document.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
Components as DecoratorComponents,
44
Env,
55
fetchDecoratorReact,
6-
Locale,
76
Props as DecoratorProps
87
} from "@navikt/nav-dekoratoren-moduler/ssr";
98

@@ -17,14 +16,14 @@ export default class MyDocument extends Document<DecoratorComponents> {
1716
availableLanguages: [
1817
{
1918
locale: "nb",
20-
url: "/"
19+
handleInApp: true
2120
},
2221
{
2322
locale: "en",
24-
url: "/en"
23+
handleInApp: true
2524
}
2625
],
27-
language: (ctx.locale || "nb") as Locale
26+
language: "nb"
2827
};
2928

3029
const Dekorator: DecoratorComponents = await fetchDecoratorReact({

src/pages/index.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ import Menu from "../../components/Menu";
33
import Spacer from "../../components/Spacer";
44
import NavPanel from "../../components/NavPanel";
55
import { format, getISOWeek } from "date-fns";
6-
import { useRouter } from "next/router";
7-
import allTexts, { LocalesType } from "../allTexts";
6+
import { useTranslation } from "react-i18next";
87

98
export default function Page() {
109

11-
const router = useRouter();
12-
const texts = allTexts[(router.locale || router.defaultLocale) as keyof LocalesType];
10+
const { t } = useTranslation();
1311

1412
const data = [
1513
{
@@ -30,7 +28,7 @@ export default function Page() {
3028
<main>
3129
<Menu />
3230

33-
<h1>{texts.title}</h1>
31+
<h1>{t('title')}</h1>
3432
<p>Du kan sende inn meldekort for følgende perioder:</p>
3533

3634
<Table zebraStripes>

0 commit comments

Comments
 (0)