0

I am trying to detect language from context api global state but unfortunately not detecting. I need solution how can I get current language from context api state and when user change the language it will save on my global state using dispatch.

import { useContext } from "react";
import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import en from "../english.json";
import ur from "../urdu.json";
import { GlobalContext } from "../../context/context";
import { Alert } from "react-native";

const LANGUAGE_DETECTOR = {
  type: "languageDetector",
  async: true,
  detect: (callback) => {
    Alert.alert("working");
    const { state, dispatch } = useContext(GlobalContext);
    const currentlan = state.currentlan;
    console.log(currentlan, "curent");
    callback(currentlan);
  },
  init: () => {},
  cacheUserLanguage: () => {},
};
i18n
  .use(initReactI18next)
  .use(LANGUAGE_DETECTOR)
  .init({
    fallbackLng: "en",
    resources: {
      en: en,
      ur: ur,
    },
    interpolation: {
      escapeValue: false, // react already safes from xss
    },
  });

export default i18n;
HDM91
  • 1,318
  • 9
  • 16
Zeeshan
  • 174
  • 10
  • you can use useTranslation hook and get i18 instance in every component and then get the current language from it – HDM91 Jan 01 '22 at 09:18
  • https://stackoverflow.com/questions/62242963/get-current-language-next-i18next – HDM91 Jan 01 '22 at 09:18
  • i dont want current language on every component instead this i want when app will start the language user select or defualt will save in context api and other component will fetch from that global state in context.js. thanks i hope you will understand and help me or give me better solution/approach. – Zeeshan Jan 01 '22 at 09:27
  • you want to have a global context that has a current lang? if yes you have to create context provider in root of your app with current lang that gets from i18n – HDM91 Jan 01 '22 at 10:23

0 Answers0