diff --git a/frontend/src/application.jsx b/frontend/src/application.jsx index 464f700e..4b59acf5 100644 --- a/frontend/src/application.jsx +++ b/frontend/src/application.jsx @@ -1,44 +1,17 @@ import { configureStore } from '@reduxjs/toolkit'; import * as Sentry from '@sentry/react'; -import i18next from 'i18next'; -import LanguageDetector from 'i18next-browser-languagedetector'; -import { initReactI18next } from 'react-i18next'; import { Provider } from 'react-redux'; import { BrowserRouter } from 'react-router-dom'; import AppRoutes from './AppRoutes.jsx'; import ModalWindow from './components/Modals'; -import resources from './locales'; import AuthProvider from './providers/AuthProvider.jsx'; import SnippetsProvider from './providers/SnippetsProvider.jsx'; import { rootReducer } from './slices'; +import { initI18next } from './initI18next.js'; export default async () => { - const defaultLanguage = 'ru'; - const baseI18NextConfig = { debug: false, resources }; - - if (process.env.NODE_ENV === 'production') { - await i18next - .use(LanguageDetector) - .use(initReactI18next) - .init({ ...baseI18NextConfig, fallbackLng: defaultLanguage }); - } - - if ( - process.env.NODE_ENV === 'development' && - !process.env.REACT_APP_NODE_ENV - ) { - await i18next - .use(LanguageDetector) - .use(initReactI18next) - .init(baseI18NextConfig); - } - - if (process.env.REACT_APP_NODE_ENV === 'test') { - await i18next - .use(initReactI18next) - .init({ ...baseI18NextConfig, lng: defaultLanguage }); - } + initI18next(); const store = configureStore({ reducer: rootReducer, diff --git a/frontend/src/hooks/useLanguage.js b/frontend/src/hooks/useLanguage.js index 84a37657..9490ad49 100644 --- a/frontend/src/hooks/useLanguage.js +++ b/frontend/src/hooks/useLanguage.js @@ -1,10 +1,7 @@ import { useTranslation } from 'react-i18next'; import { useLayoutEffect, useState } from 'react'; -const AVAILABLE_LANGUAGES = ['en', 'ru']; -if (process.env.NODE_ENV !== 'production') { - AVAILABLE_LANGUAGES.push('dev'); -} +import { AVAILABLE_LANGUAGES } from '../initI18next'; const useLanguage = () => { const { i18n } = useTranslation(); diff --git a/frontend/src/initI18next.js b/frontend/src/initI18next.js new file mode 100644 index 00000000..d64def6d --- /dev/null +++ b/frontend/src/initI18next.js @@ -0,0 +1,28 @@ +import i18next from 'i18next'; +import LanguageDetector from 'i18next-browser-languagedetector'; +import { initReactI18next } from 'react-i18next'; + +import resources from './locales'; + +const defaultLanguage = 'ru'; +const baseI18NextConfig = { + debug: process.env.NODE_ENV === 'development', + resources, +}; + +export const AVAILABLE_LANGUAGES = ['en', 'ru']; + +export const initI18next = async () => { + if (process.env.REACT_APP_NODE_ENV === 'test') { + await i18next + .use(initReactI18next) + .init({ ...baseI18NextConfig, lng: defaultLanguage }); + + return; + } + + await i18next + .use(LanguageDetector) + .use(initReactI18next) + .init({ ...baseI18NextConfig, fallbackLng: defaultLanguage }); +};