From 07bb947e649127b20a4a50593f9f1fe4bfb3a1e7 Mon Sep 17 00:00:00 2001 From: Tore Brede <Tore.Brede@uib.no> Date: Fri, 24 Sep 2021 12:25:27 +0200 Subject: [PATCH] Adding detection and storing of selected language --- frontend/package-lock.json | 38 ++++++++++++++++++++++++++++++++++++++ frontend/package.json | 1 + frontend/src/i18n.ts | 7 ++++++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 187616b1..d55f0778 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -20,6 +20,7 @@ "date-fns": "^2.24.0", "http-proxy-middleware": "^2.0.1", "i18next": "^20.6.0", + "i18next-browser-languagedetector": "^6.1.2", "i18next-http-backend": "^1.3.1", "react": "^17.0.2", "react-datepicker": "^4.2.1", @@ -10098,6 +10099,25 @@ "@babel/runtime": "^7.12.0" } }, + "node_modules/i18next-browser-languagedetector": { + "version": "6.1.2", + "resolved": "https://registry.npmjs.org/i18next-browser-languagedetector/-/i18next-browser-languagedetector-6.1.2.tgz", + "integrity": "sha512-YDzIGHhMRvr7M+c8B3EQUKyiMBhfqox4o1qkFvt4QXuu5V2cxf74+NCr+VEkUuU0y+RwcupA238eeolW1Yn80g==", + "dependencies": { + "@babel/runtime": "^7.14.6" + } + }, + "node_modules/i18next-browser-languagedetector/node_modules/@babel/runtime": { + "version": "7.15.4", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.15.4.tgz", + "integrity": "sha512-99catp6bHCaxr4sJ/DbTGgHS4+Rs2RVd2g7iOap6SLGPDknRK9ztKNsE/Fg6QhSeh1FGE5f6gHGQmvvn3I3xhw==", + "dependencies": { + "regenerator-runtime": "^0.13.4" + }, + "engines": { + "node": ">=6.9.0" + } + }, "node_modules/i18next-http-backend": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/i18next-http-backend/-/i18next-http-backend-1.3.1.tgz", @@ -29612,6 +29632,24 @@ "@babel/runtime": "^7.12.0" } }, + "i18next-browser-languagedetector": { + "version": "6.1.2", + "resolved": "https://registry.npmjs.org/i18next-browser-languagedetector/-/i18next-browser-languagedetector-6.1.2.tgz", + "integrity": "sha512-YDzIGHhMRvr7M+c8B3EQUKyiMBhfqox4o1qkFvt4QXuu5V2cxf74+NCr+VEkUuU0y+RwcupA238eeolW1Yn80g==", + "requires": { + "@babel/runtime": "^7.14.6" + }, + "dependencies": { + "@babel/runtime": { + "version": "7.15.4", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.15.4.tgz", + "integrity": "sha512-99catp6bHCaxr4sJ/DbTGgHS4+Rs2RVd2g7iOap6SLGPDknRK9ztKNsE/Fg6QhSeh1FGE5f6gHGQmvvn3I3xhw==", + "requires": { + "regenerator-runtime": "^0.13.4" + } + } + } + }, "i18next-http-backend": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/i18next-http-backend/-/i18next-http-backend-1.3.1.tgz", diff --git a/frontend/package.json b/frontend/package.json index e4753e74..0486fe9d 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -15,6 +15,7 @@ "date-fns": "^2.24.0", "http-proxy-middleware": "^2.0.1", "i18next": "^20.6.0", + "i18next-browser-languagedetector": "^6.1.2", "i18next-http-backend": "^1.3.1", "react": "^17.0.2", "react-datepicker": "^4.2.1", diff --git a/frontend/src/i18n.ts b/frontend/src/i18n.ts index 532ddcc6..f900bb86 100644 --- a/frontend/src/i18n.ts +++ b/frontend/src/i18n.ts @@ -1,9 +1,11 @@ import i18n from 'i18next' import { initReactI18next } from 'react-i18next' import Backend from 'i18next-http-backend' +import LanguageDetector from 'i18next-browser-languagedetector' i18n .use(Backend) + .use(LanguageDetector) .use(initReactI18next) .init({ fallbackLng: { @@ -13,7 +15,6 @@ i18n }, supportedLngs: ['nb', 'en', 'nn'], debug: process.env.NODE_ENV !== 'production', - lng: 'nb', backend: { loadPath: '/locales/{{lng}}/{{ns}}.json', }, @@ -23,6 +24,10 @@ i18n react: { useSuspense: false, }, + detection: { + order: ['cookie'], + caches: ['cookie'], + }, }) export default i18n -- GitLab