mirror of https://github.com/arc53/DocsGPT
feat: auto browser lang detection on first visit
parent
90c367842f
commit
6727c42f18
@ -1,29 +1,39 @@
|
|||||||
import i18n from 'i18next';
|
import i18n from 'i18next';
|
||||||
import { initReactI18next } from 'react-i18next';
|
import { initReactI18next } from 'react-i18next';
|
||||||
|
import LanguageDetector from 'i18next-browser-languagedetector';
|
||||||
|
|
||||||
import en from './en.json'; //English
|
import en from './en.json'; //English
|
||||||
import es from './es.json'; //Spanish
|
import es from './es.json'; //Spanish
|
||||||
import jp from './jp.json'; //Japanese
|
import jp from './jp.json'; //Japanese
|
||||||
import zh from './zh.json'; //Mandarin
|
import zh from './zh.json'; //Mandarin
|
||||||
|
|
||||||
i18n.use(initReactI18next).init({
|
i18n
|
||||||
resources: {
|
.use(LanguageDetector)
|
||||||
en: {
|
.use(initReactI18next)
|
||||||
translation: en,
|
.init({
|
||||||
|
resources: {
|
||||||
|
en: {
|
||||||
|
translation: en,
|
||||||
|
},
|
||||||
|
es: {
|
||||||
|
translation: es,
|
||||||
|
},
|
||||||
|
jp: {
|
||||||
|
translation: jp,
|
||||||
|
},
|
||||||
|
zh: {
|
||||||
|
translation: zh,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
es: {
|
fallbackLng: 'en',
|
||||||
translation: es,
|
detection: {
|
||||||
|
order: ['localStorage', 'navigator'], // checks localStorage for existing lang before browser's
|
||||||
|
caches: ['localStorage'], //stores detected lang to localStorage with i18nextLng key
|
||||||
|
lookupLocalStorage: 'docsgpt-locale', //using docsgpt-locale as the custom key for storing and retrieving the lang rather than the default `i18nextLng`
|
||||||
},
|
},
|
||||||
jp: {
|
});
|
||||||
translation: jp,
|
|
||||||
},
|
|
||||||
zh: {
|
|
||||||
translation: zh,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const locale = localStorage.getItem('docsgpt-locale') ?? 'en';
|
const savedLocale = localStorage.getItem('docsgpt-locale') ?? i18n.language;
|
||||||
i18n.changeLanguage(locale);
|
i18n.changeLanguage(savedLocale);
|
||||||
|
|
||||||
export default i18n;
|
export default i18n;
|
||||||
|
Loading…
Reference in New Issue