Skip to content

Commit da9d2c7

Browse files
committed
add auto detect for languages
1 parent f86fb9f commit da9d2c7

File tree

4 files changed

+45
-14
lines changed

4 files changed

+45
-14
lines changed

package-lock.json

Lines changed: 10 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
@@ -17,6 +17,7 @@
1717
"@rjsf/validator-ajv8": "^5.22.1",
1818
"bootstrap": "^4.6.2",
1919
"i18next": "^23.16.5",
20+
"i18next-browser-languagedetector": "^8.0.0",
2021
"react": "^18.3.1",
2122
"react-bootstrap": "^1.6.8",
2223
"react-copy-to-clipboard": "^5.1.0",

src/i18n/config.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,33 @@ import i18next from "i18next";
22
import { initReactI18next } from "react-i18next";
33
import enTranslation from "./en/translation.json";
44
import nlTranslation from "./nl/translation.json";
5+
import LanguageDetector from "i18next-browser-languagedetector";
56

6-
i18next.use(initReactI18next).init({
7-
lng: "en", // if you're using a language detector, do not define the lng option
8-
fallbackLng: "en",
9-
debug: true,
10-
resources: {
11-
en: {
12-
translation: enTranslation,
7+
i18next
8+
.use(LanguageDetector) // Add the language detector
9+
.use(initReactI18next)
10+
.init({
11+
fallbackLng: "en", // Default language if detection fails
12+
debug: true,
13+
resources: {
14+
en: {
15+
translation: enTranslation,
16+
},
17+
nl: {
18+
translation: nlTranslation,
19+
},
1320
},
14-
nl: {
15-
translation: nlTranslation,
21+
detection: {
22+
// Prioritize localStorage over browser language
23+
order: [
24+
"localStorage",
25+
"querystring",
26+
"htmlTag",
27+
"navigator",
28+
"path",
29+
"subdomain",
30+
],
31+
caches: ["localStorage"],
1632
},
17-
},
18-
// if you see an error like: "Argument of type 'DefaultTFuncReturn' is not assignable to parameter of type xyz"
19-
// set returnNull to false (and also in the i18next.d.ts options)
20-
returnNull: false,
21-
});
33+
returnNull: false,
34+
});

yarn.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,13 @@ html-parse-stringify@^3.0.1:
11221122
dependencies:
11231123
void-elements "3.1.0"
11241124

1125+
i18next-browser-languagedetector@^8.0.0:
1126+
version "8.0.0"
1127+
resolved "https://registry.npmjs.org/i18next-browser-languagedetector/-/i18next-browser-languagedetector-8.0.0.tgz"
1128+
integrity sha512-zhXdJXTTCoG39QsrOCiOabnWj2jecouOqbchu3EfhtSHxIB5Uugnm9JaizenOy39h7ne3+fLikIjeW88+rgszw==
1129+
dependencies:
1130+
"@babel/runtime" "^7.23.2"
1131+
11251132
i18next-resources-for-ts@^1.5.0:
11261133
version "1.5.0"
11271134
resolved "https://registry.npmjs.org/i18next-resources-for-ts/-/i18next-resources-for-ts-1.5.0.tgz"

0 commit comments

Comments
 (0)