'How to update Context manually for React Native android?
I have set up a language context for my app which works to change the language when I change the language of the device. What I am trying to accomplish now is adding a button to switch the language manually. Is this possible with my code currently or should I redo my context?
This is my language context code.
Thank you
import React, { useContext, useEffect, useState } from 'react';
import en from "../lang/en.json"
import ga from "../lang/ga.json"
import * as RNLocalize from "react-native-localize"
import App from '../../App';
import { StringSchema } from 'yup';
import { DevSettings } from 'react-native';
type LanguageContextType = {
hello: string;
Good: string;
}
export const LanguageContext = React.createContext<LanguageContextType>({} as LanguageContextType);
let languageObj = ({
'en': en,
'es-US': ga
})
export const LanguageContextProvider: React.FC = () => {
const [selectedLanguage, setSelectedLanguage] = useState('en');
useEffect(() => {
console.log(RNLocalize.getLocales())
const currentLanguage =RNLocalize.findBestAvailableLanguage(Object.keys(languageObj));
setSelectedLanguage(currentLanguage?.languageTag || 'es-US');
}, []);
const value = {
...languageObj[selectedLanguage as 'en' | 'es-US'],
};
return (
<LanguageContext.Provider value={value}>
<App />
</LanguageContext.Provider>
);
};
export const useTranslation = () => useContext(LanguageContext);
I would be hoping that be clicking a button my code would switch from whatever language out of the 2 is set and set the other one
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
