35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
import i18n from 'i18next';
|
|
import { initReactI18next } from 'react-i18next';
|
|
import translationRU from './locales/ru.json';
|
|
import translationEN from './locales/en.json';
|
|
|
|
const LANGUAGES = ['ru', 'en'];
|
|
|
|
// i18next-browser-languagedetector в проекте нет, поэтому persist делаем сами:
|
|
// иначе выбор языка жил только в памяти и сбрасывался при перезагрузке.
|
|
const stored = localStorage.getItem('i18nextLng');
|
|
const savedLang = LANGUAGES.includes(stored) ? stored : 'ru';
|
|
|
|
i18n.use(initReactI18next).init({
|
|
resources: {
|
|
ru: { translation: translationRU },
|
|
en: { translation: translationEN },
|
|
},
|
|
lng: savedLang,
|
|
fallbackLng: 'ru',
|
|
supportedLngs: LANGUAGES,
|
|
interpolation: { escapeValue: false },
|
|
});
|
|
|
|
const updateDocumentTitle = (lng) => {
|
|
document.title = lng === 'en' ? 'Find' : 'Находка';
|
|
};
|
|
|
|
i18n.on('languageChanged', (lng) => {
|
|
localStorage.setItem('i18nextLng', lng);
|
|
updateDocumentTitle(lng);
|
|
});
|
|
updateDocumentTitle(i18n.language);
|
|
|
|
export default i18n;
|