更新应用程序 React Natice 的所有屏幕中的变量

发布于 2025-01-15 10:08:21 字数 1438 浏览 6 评论 0原文

我的应用程序有法语和英语版本。我希望用户能够更改任何页面上的语言,并且当他们导航到任何其他屏幕时,我希望反映更改。

例如,我有 A.js

const A = (props) => {
  const [language, setLanguage] = useState('')
  return (
   <View style ={styles.container}>
            <Button onPress ={()=>setLanguage('french')}>{language==="french" ? changer la langue : change the language}
            </Button>
        </View>  
  )
}
Now when I go to any screen after pressing the button I want the content to be rendered in French and if I change the language in another screen I want changer la langue to be rendered in this screen

内存泄漏

const [language, setLanguage] = useState('') 
    const [visible, setVisible] = useState(false)
    const {t} = useTranslation();    
    const fetchLanguage =  async () =>{
        try{
            const currentLanguage = await AsyncStorage.getItem('language')
            if (currentLanguage === null ){
                setVisible(true)
            }
            else{
                setLanguage (currentLanguage)
                i18n.changeLanguage(currentLanguage)
            }
        }catch (e){
            console.log(e)
        }
       
    }
    useEffect (()=>{
        fetchLanguage()
    }, [])

My app is both in French and English. I want the user to be able to change the language on any page and when they navigate to any other screen I want the change to be reflected.

For instance, I have A.js

const A = (props) => {
  const [language, setLanguage] = useState('')
  return (
   <View style ={styles.container}>
            <Button onPress ={()=>setLanguage('french')}>{language==="french" ? changer la langue : change the language}
            </Button>
        </View>  
  )
}

Now when I go to any screen after pressing the button I want the content to be rendered in French and if I change the language in another screen I want changer la langue to be rendered in this screen

Memory leakage

const [language, setLanguage] = useState('') 
    const [visible, setVisible] = useState(false)
    const {t} = useTranslation();    
    const fetchLanguage =  async () =>{
        try{
            const currentLanguage = await AsyncStorage.getItem('language')
            if (currentLanguage === null ){
                setVisible(true)
            }
            else{
                setLanguage (currentLanguage)
                i18n.changeLanguage(currentLanguage)
            }
        }catch (e){
            console.log(e)
        }
       
    }
    useEffect (()=>{
        fetchLanguage()
    }, [])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇到 2025-01-22 10:08:21

您可以继续创建一个上下文来手动管理它,但这就像重新创建轮子一样。

您可以立即使用 i18nextreact-i18next 支持应用程序范围的本地化,只需最少的设置,就像下面的 React Native 一样。

import i18n from 'i18next';
import {initReactI18next} from 'react-i18next';

// the translations
// (tip move them in a JSON file and import them,
// or even better, manage them separated from your code: 
https://react.i18next.com/guides/multiple-translation-files)
const resources = {
  en: {
    "title": "Hello",
  },
  fr: {
    "title": "Bonjour",
  },
};

i18n
  .use(initReactI18next) // passes i18n down to react-i18next
  .init({
    resources,
    compatibilityJSON: 'v3',
    lng: 'en',  //default language.
    interpolation: {
      escapeValue: false, // react already safes from xss
    },
});

export default i18n;

然后,您可以通过 docs 中描述的简单方法在应用程序中轻松使用这些翻译,

例如

const {t} = useTranslations();
....
return (
  <Text>{t('title')}</Text>
)

< strong>更新:让用户在应用程序内更改语言

您仍然可以使用此 i18next 让您的用户在应用程序内更改语言,只需在 i18 实例上调用一个简单的方法即可,如文档此处

i18n.changeLanguage('fr');

You can go ahead make a context to manage that manually however that will be something like recreating wheel.

You can right away use i18next and react-i18next to support app wide localisation with just minimal setup like below in React Native too.

import i18n from 'i18next';
import {initReactI18next} from 'react-i18next';

// the translations
// (tip move them in a JSON file and import them,
// or even better, manage them separated from your code: 
https://react.i18next.com/guides/multiple-translation-files)
const resources = {
  en: {
    "title": "Hello",
  },
  fr: {
    "title": "Bonjour",
  },
};

i18n
  .use(initReactI18next) // passes i18n down to react-i18next
  .init({
    resources,
    compatibilityJSON: 'v3',
    lng: 'en',  //default language.
    interpolation: {
      escapeValue: false, // react already safes from xss
    },
});

export default i18n;

Then you can easily use these translations in app via easy ways as described in the docs

Such as

const {t} = useTranslations();
....
return (
  <Text>{t('title')}</Text>
)

Update: Let user change the language from within the app

You can still use this i18next to let your users change the language from within the app by just calling a simple method on i18 instance as documented here.

i18n.changeLanguage('fr');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文