How to use setLocale within i18n-js?

这一生的挚爱 提交于 2020-06-09 06:49:42

问题


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

This is how I configure it:

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

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 ?


回答1:


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/.

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

In App.js

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 />
  )
}

In 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

In 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

In fr.json

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


来源:https://stackoverflow.com/questions/59949722/how-to-use-setlocale-within-i18n-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!