简体   繁体   English

如何在 React 中设置 i18n 默认翻译语言

[英]How to set i18n default translation language in React

I have two translations in my app using i18n translator and I'm trying with no success to set the default language.我使用 i18n 翻译器在我的应用程序中有两个翻译,我尝试设置默认语言没有成功。

The preferred language (the default language) will be set at registration and will come from the database.首选语言(默认语言)将在注册时设置并来自数据库。

Where can I create some kind of logic that will tell i18n what is the language coming from the backend?我在哪里可以创建某种逻辑来告诉 i18n 来自后端的语言是什么?

The value will show something like this:该值将显示如下:

const defaultLang = currentUser.data.laiso //this will output a string eg. 'en'

The i18n configuration file looks something like this: i18n 配置文件如下所示:

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

import Backend from 'i18next-http-backend';
import LanguageDetector from 'i18next-browser-languagedetector';

const Languages = ['en', 'de'];

i18n

  .use(Backend)

  .use(LanguageDetector)

  .use(initReactI18next)

  .init({
    fallbackLng: 'en',
    debug: true,
    whitelist: Languages,

    interpolation: {
      escapeValue: false
    }
  });


export default i18n;

Should be something written under .use(Backend) ?应该写在.use(Backend)下吗? This is the first time using i18n so any hints would be appreciated.这是第一次使用 i18n,所以任何提示将不胜感激。

This is my component where I need the translation.这是我需要翻译的组件。 In it, I've tried to set the index of i18n.languages based on the language preferred, but this is not working:在其中,我尝试根据首选语言设置i18n.languages的索引,但这不起作用:

import React from 'react'
import { useTranslation } from 'react-i18next';


export default function Dashboard() {
    //the logged user
    const currentUser = AuthService.getCurrentUser();
    //value from database
    const defaultLang = currentUser.data.laiso //this will output a string eg. 'en'

    //translation
    const { t, i18n } = useTranslation();

    function handleClick(lang) {
        i18n.changeLanguage(lang);
    }

    const current = i18n.languages[defaultLang === 'en' ? 0 : 1]


    return (
        <>
        <div className={styles.languageSelector}>
        <a 
          onClick={ () => handleClick('en') }
          style={{ fontWeight: current === "en" ? 800 : 400}}
        >
          EN
        </a>
        <a 
          onClick={ () => handleClick('de') }
          style={{ fontWeight: current === "de" ? 800 : 400}}
        >    
          DE
        </a>
      </div>
      <div>{t('translated.stuff')}</div>
      </>
    )
}

I think you need a hook to rerender the page when the language was changed.我认为您需要一个钩子来在更改语言时重新呈现页面。 So you could add this to your Dashboard.jsx :因此,您可以将其添加到您的Dashboard.jsx中:

  React.useEffect(() => {
    const handleLanguageChange = () => {
      // Refresh your component, maybe use a hook for it.
    };

    i18next.on("languageChanged", handleLanguageChange);

    return () => {
      i18next.off("languageChanged", handleLanguageChange);
    };
  }, []);

And for the refresh logic, you could add a counter which increment when the language is changed, this should be enough to start a rerender of the component.对于刷新逻辑,您可以添加一个计数器,该计数器在更改语言时递增,这应该足以启动组件的重新渲染。

I've managed to solve this by using useEffect.我已经设法通过使用 useEffect 解决了这个问题。

//set the language from database
  useEffect(() => {
    i18n.changeLanguage(currentUser.data.laiso);
  }, []);

That was it, Thanks就是这样,谢谢

you can do that just by adding lng: 'en' inside.init()你可以通过添加lng: 'en' inside.init()

.init({
    fallbackLng: 'en',
    lng: 'en', // default language
    debug: true,
    whitelist: Languages,
    interpolation: {
        escapeValue: false
    }
});

https://www.i18next.com/overview/configuration-options https://www.i18next.com/overview/configuration-options

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM