I'm working on a website and came upon a strange problem with i18next that I can't seem to solve.
I want i18next to save the current language in a cookie but only when the user has accepted to store cookies (custom cookie banner component)
Currently, I have the following code:
i18n.js
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import Backend from 'i18next-xhr-backend';
import LanguageDetector from 'i18next-browser-languagedetector';
i18n
.use(Backend)
.use(LanguageDetector)
.use(initReactI18next)
.init({
whitelist: ['en', 'fr', 'nl'],
fallbackLng: 'en',
interpolation: {
escapeValue: false,
},
ns: [
'common',
],
react: {
wait: true,
},
detection: {
caches: ['cookie'],
lookupFromPathIndex: 0,
},
});
index.js
import React, { Suspense } from 'react';
import ReactDOM from 'react-dom';
import './i18n';
...
My problem is with the caches: ['cookie']
property. I want it to be include "cookie" but only after the user has clicked on an "I Agree" button somewhere on the website. Is there a way to first load the i18next with an empty caches array and reload the object when the user clicked on "I Agree".
On the next page visit (when the user accepted cookies) i18next should know it can read from the language cookie as well. But I think when I know how to fix the first issue I can solve this issue easily.