React native i18n multi language with context api

796 views Asked by At

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;
0

There are 0 answers