1. <small id='DVbKy'></small><noframes id='DVbKy'>

      <legend id='DVbKy'><style id='DVbKy'><dir id='DVbKy'><q id='DVbKy'></q></dir></style></legend>
    2. <i id='DVbKy'><tr id='DVbKy'><dt id='DVbKy'><q id='DVbKy'><span id='DVbKy'><b id='DVbKy'><form id='DVbKy'><ins id='DVbKy'></ins><ul id='DVbKy'></ul><sub id='DVbKy'></sub></form><legend id='DVbKy'></legend><bdo id='DVbKy'><pre id='DVbKy'><center id='DVbKy'></center></pre></bdo></b><th id='DVbKy'></th></span></q></dt></tr></i><div id='DVbKy'><tfoot id='DVbKy'></tfoot><dl id='DVbKy'><fieldset id='DVbKy'></fieldset></dl></div>

        • <bdo id='DVbKy'></bdo><ul id='DVbKy'></ul>
        <tfoot id='DVbKy'></tfoot>

        如何在 i18n-js 中使用 setLocale?

        时间:2023-11-29
        <tfoot id='X7BZu'></tfoot>

            <bdo id='X7BZu'></bdo><ul id='X7BZu'></ul>
                <tbody id='X7BZu'></tbody>
            • <small id='X7BZu'></small><noframes id='X7BZu'>

            • <i id='X7BZu'><tr id='X7BZu'><dt id='X7BZu'><q id='X7BZu'><span id='X7BZu'><b id='X7BZu'><form id='X7BZu'><ins id='X7BZu'></ins><ul id='X7BZu'></ul><sub id='X7BZu'></sub></form><legend id='X7BZu'></legend><bdo id='X7BZu'><pre id='X7BZu'><center id='X7BZu'></center></pre></bdo></b><th id='X7BZu'></th></span></q></dt></tr></i><div id='X7BZu'><tfoot id='X7BZu'></tfoot><dl id='X7BZu'><fieldset id='X7BZu'></fieldset></dl></div>
              <legend id='X7BZu'><style id='X7BZu'><dir id='X7BZu'><q id='X7BZu'></q></dir></style></legend>

                  本文介绍了如何在 i18n-js 中使用 setLocale?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在我的 expo 项目中使用 i18n-js 来翻译我的应用程序.

                  I am using i18n-js within my expo project to translate my app.

                  这是我的配置方式:

                  import React from 'react';
                  import * as Localization from 'expo-localization';
                  import i18n from 'i18n-js';
                  
                  export default function configureI18n(translations) {
                    i18n.fallbacks = true;
                    i18n.translations = translations;
                    i18n.locale = Localization.locale;
                    const [locale, setLocale] = React.useState(Localization.locale);
                    const localizationContext = React.useMemo(() => ({
                      t: (scope, options) => i18n.t(scope, { locale, ...options }),
                      locale,
                      setLocale,
                    }), [locale]);
                  
                    return localizationContext;
                  }
                  

                  我将它传递给我的 AppContext 并尝试在我的视图中使用 setLocale:

                  I pass this to my AppContext and try to use setLocale within my view:

                  function HomeView(props) {
                    const { locale, setLocale } = useContext(AppContext);
                  
                    return (
                      <View>
                              <Button
                                style={{ marginTop: 4 }}
                                icon="translate"
                                mode="contained"
                                title="toggle navigation"
                                onPress={() => setLocale(locale.includes('en') ? 'fr' : 'en')}
                              >
                                {locale.includes('en') ? 'FR' : 'EN'}
                              </Button>
                      </View>
                    );
                  }
                  

                  调用了函数,但文字还是英文,我做错了什么?

                  The function is called, but the text is still in english, what am I doing wrong ?

                  推荐答案

                  您需要在顶级组件中设置翻译,例如 App.js.然后,您必须在 /src/locales/ 中创建 2 个 json 文件:fr.jsonen.json.

                  You need to setup the translation in your top level component, like App.js. Then, you have to create 2 json files: fr.json and en.json in /src/locales/.

                  最后,在任何屏幕中,您必须导入 i18n 并使用 t() 函数来翻译字符串.

                  Finally, in any screen, you have to import i18n and use the t() function to translate strings.

                  import React, { useEffect, useState } from 'react'
                  import { loadLocale } from './locales/i18n'
                  
                  export default function App() {
                    const [theme, setTheme] = useState(null)
                  
                    useEffect(() => {
                      init()
                    }, [])
                  
                    const init = async () => {
                      await loadLocale()
                    }
                  
                    return (
                      <AppContainer />
                    )
                  }
                  

                  在 i18n.js 中

                  import * as Localization from 'expo-localization'
                  import i18n from 'i18n-js'
                  
                  i18n.defaultLocale = 'fr'
                  i18n.locale = 'fr'
                  i18n.fallbacks = true
                  
                  export const loadLocale = async () => {
                    for (const locale of Localization.locales) {
                      if (i18n.translations[locale.languageCode] !== null) {
                        i18n.locale = locale.languageCode
                        switch (locale.languageCode) {
                          case 'en':
                            import('./en.json').then(en => {
                              i18n.translations = { en }
                            })
                            break
                          default:
                          case 'fr':
                            import('./fr.json').then(fr => {
                              i18n.translations = { fr }
                            })
                            break
                        }
                        break
                      }
                    }
                  }
                  
                  export default i18n
                  

                  在 HomeView.js 中

                  import React from 'react'
                  import i18n from '../locales/i18n'
                  
                  function HomeScreen({ navigation }) {
                  
                    return (
                      <View style={{ flex: 1 }}>
                        <Text>{i18n.t('home.welcome')}</Text>
                        <Text>{i18n.t('home.content')}</Text>
                      </View>
                    )
                  }
                  
                  export default HomeView
                  

                  在 fr.json 中

                  {
                    "home": {
                      "welcome": "Bienvenue",
                      "content": "Du contenu ici"
                    }
                  }
                  

                  这篇关于如何在 i18n-js 中使用 setLocale?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:React Native 得到这个错误'Unrecognized operator abs' 下一篇:世博会:“auth/operation-not-supported-in-this-environment"

                  相关文章

                  <i id='2pyNO'><tr id='2pyNO'><dt id='2pyNO'><q id='2pyNO'><span id='2pyNO'><b id='2pyNO'><form id='2pyNO'><ins id='2pyNO'></ins><ul id='2pyNO'></ul><sub id='2pyNO'></sub></form><legend id='2pyNO'></legend><bdo id='2pyNO'><pre id='2pyNO'><center id='2pyNO'></center></pre></bdo></b><th id='2pyNO'></th></span></q></dt></tr></i><div id='2pyNO'><tfoot id='2pyNO'></tfoot><dl id='2pyNO'><fieldset id='2pyNO'></fieldset></dl></div>
                1. <legend id='2pyNO'><style id='2pyNO'><dir id='2pyNO'><q id='2pyNO'></q></dir></style></legend>

                  1. <small id='2pyNO'></small><noframes id='2pyNO'>

                      <bdo id='2pyNO'></bdo><ul id='2pyNO'></ul>
                  2. <tfoot id='2pyNO'></tfoot>