Introduction
This ReduxDetector detect language in Redux using the plugin i18next-browser-languageDetector for i18next (react-i18next).
Getting started
See Example
npm install --save i18next-browser-languagedetector
npm install --save i18next-redux-languagedetector
import i18n from 'i18next';
import { reactI18nextModule } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import ReduxDetector from 'i18next-redux-languagedetector';
import Backend from 'i18next-chained-backend';
import XHR from 'i18next-xhr-backend';
const LngDetector = new LanguageDetector();
LngDetector.addDetector(ReduxDetector);
export default function configureI18n({ redux, i18nextConfig }) {
i18n
.use(Backend)
.use(LngDetector)
.use(reactI18nextModule)
.init({
backend: {
backends: [
XHR
],
backendOptions: [
{
loadPath: '/locales/{{lng}}/{{ns}}.json'
}
]
},
detection: {
order: [
'redux',
],
lookupRedux: redux.lookupRedux,
cacheUserLanguageRedux: redux.cacheUserLanguageRedux,
caches: ['redux'],
excludeCacheFor: ['cimode'],
},
whitelist: i18nextConfig.whitelist,
fallbackLng: i18nextConfig.fallbackLng,
ns: i18nextConfig.ns,
defaultNS: i18nextConfig.defaultNS,
debug: i18nextConfig.debug,
interpolation: {
escapeValue: false,
},
react: {
defaultTransParent: 'div',
wait: true
}
});
return i18n;
};
import { i18nextReducer } from 'i18next-redux-languagedetector';
export default {
i18next: i18nextReducer,
}
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import { I18nextProvider } from 'react-i18next';
import './index.css';
import configureStore from './store';
import configureI18n from './i18n';
import { languageChange } from 'i18next-redux-languagedetector';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
const i18nextConfig = {
language: null,
whitelist: ['en', 'de', 'ru'],
fallbackLng: 'en',
ns: ['translations'],
defaultNS: 'translations',
debug: true,
}
const { store } = configureStore({
i18next: i18nextConfig,
});
const i18n = configureI18n({
i18nextConfig: i18nextConfig,
redux: {
lookupRedux: () => store.getState().i18next,
cacheUserLanguageRedux: (language) => store.dispatch(languageChange(language))
}
});
ReactDOM.render(
<Provider store={store}>
<I18nextProvider i18n={i18n}>
<App />
</I18nextProvider>
</Provider>
, document.getElementById('root')
);
registerServiceWorker();