简体   繁体   English

如何在 React 中使用 i18next 更改语言

[英]How to change language with i18next in React

I am trying to add the i18next translation system to my React app and I've already configured everything, but I can't seem to find how to change the language (using a select or a button, for example).我正在尝试将 i18next 翻译系统添加到我的 React 应用程序中,并且我已经配置了所有内容,但我似乎无法找到如何更改语言(例如,使用选择或按钮)。

I've followed this easy guide to set the whole thing up.我已经按照这个简单的指南来设置整个事情。 But there's nothing on how to manually change the language.但是没有关于如何手动更改语言的内容。 And, as you can see here , there's a function to change the language, but it's inside a function component:而且,正如您在此处看到的,有一个用于更改语言的函数,但它位于函数组件中:

function Page() {
const { t, i18n } = useTranslation();

  const changeLanguage = lng => {
    i18n.changeLanguage(lng);
  };

  return ([...])
}

However, my component is a class extending from component and it doesn't seem to work the same way:但是,我的组件是从组件扩展的类,它的工作方式似乎不同:

class Profile extends Component {
  [...]
}
export default withTranslation()(Profile);

How should I do it then?那我该怎么做呢? I've no clue.我不知道。

It throws some errors that I have not solved yet, but at least it works.它引发了一些我尚未解决的错误,但至少它有效。 Apparently, when in a function component (such as the one listed before), when using useTranslation, your t and i18n variables are obtained by calling useTranslation().显然,当在一个函数组件中(比如前面列出的那个),当使用 useTranslation 时,你的 t 和 i18n 变量是通过调用 useTranslation() 获得的。 However, when using a class, t and i18n are obtained from this.props.但是,在使用类时,t 和 i18n 是从 this.props 中获取的。

So, now the class looks like this:所以,现在这个类看起来像这样:

import React, { Component } from "react";
import { withTranslation } from 'react-i18next';

class Profile extends Component {

  render() {
    const { t, i18n } = this.props;
    const changeLanguage = lng => {          
      i18n.changeLanguage(lng);
    };

    return (
      [...]
      <button onClick={() => changeLanguage('en')}>EN</button>
      [...]
    )
  }

  export default withTranslation()(UserProfile);

I'll edit the answer when I solve the warnings, but I'll leve this solution here in case someone has the same issue.当我解决警告时,我会编辑答案,但如果有人遇到同样的问题,我会在这里留下这个解决方案。

The error that I get is the following:我得到的错误如下:

Uncaught Error: MobX Provider: The set of provided stores has changed.未捕获的错误:MobX 提供程序:提供的商店集已更改。 See: https://github.com/mobxjs/mobx-react#the-set-of-provided-stores-has-changed-error .请参阅: https : //github.com/mobxjs/mobx-react#the-set-of-provided-stores-has-changed-error

import { serverSideTranslations } from "next-i18next/serverSideTranslations";
import { useTranslation } from "next-i18next";

// Use getServerSideProps or getStaticProps as convenience
export async function getServerSideProps({ locale }) {

  return {
    props: {
       ...(await serverSideTranslations(locale, ["common"]))
      },
  };

}

function YourComponent(props) {
   const { t, i18n } = useTranslation("common");
   console.log(i18n.language);
   return (<div>{i18n.language}</div>);
}

export default YourComponent;

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

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