%PDF- %PDF-
Direktori : /home/emergentqj/actioncivile/pre-auth/.8352c036a19b0051d0217d27d25e3f4a/static/js/actions/ |
Current File : /home/emergentqj/actioncivile/pre-auth/.8352c036a19b0051d0217d27d25e3f4a/static/js/actions/languageActions.js |
import i18n from "i18next"; import i18backend from "i18next-http-backend"; import { initReactI18next } from "react-i18next"; import { setLanguageData } from "actions/list_actions"; import { getEnvConstant } from "utils"; import { setLanguageCookie } from "utils/languages"; import { setCountryCookie } from "utils/countries"; import { getLanguage, getLanguageByCountry } from "utils/getLanguage"; import { getTranslations } from "utils/getTranslations"; import configFunc from "config/config"; // eslint-disable-line import/no-cycle const { languageFolder } = configFunc(); const getUrl = () => { const { protocol, hostname } = window.location; return (hostname.startsWith("localhost") || hostname.startsWith("127.0.0.1")) ? `${languageFolder}{{lng}}.json` : `${protocol}//${hostname}${languageFolder}{{lng}}.json`; }; export const initLanguage = (countryCode) => ( (dispatch) => { const languageCode = getLanguage(countryCode); if (countryCode) { setCountryCookie(countryCode); } setLanguageCookie(languageCode); const translations = getTranslations(countryCode); dispatch( setLanguageData({ // language: languageCode, translations, }), ); i18n .use(i18backend) .use(initReactI18next) .init({ react: { useSuspense: false, wait: true, }, initImmediate: false, // resources, lng: languageCode, fallbackLng: "en", debug: getEnvConstant() === "staging", preload: translations.map(({ code }) => code), backend: { loadPath: getUrl(), stringify: JSON.stringify, }, }); } ); export const setLanguage = (countryCode) => ( (dispatch, getStore) => { const languageManuallySet = getStore().config.get("languageManuallySet"); // If the user has already chosen a language // before choosing a country, we don't change the language... if (!languageManuallySet) { const languageCode = getLanguageByCountry(countryCode); setLanguageCookie(languageCode); i18n.changeLanguage(languageCode); } const translations = getTranslations(countryCode); i18n.changeLanguage(translations[0].code); dispatch( setLanguageData({ translations, }), ); } );