React-Intl如何从变量切换语言环境和消息 [英] React-Intl how to switch locale and messages from variable

查看:461
本文介绍了React-Intl如何从变量切换语言环境和消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何使用React-Intl更改语言.这是我的第一个React App,它是用create-react-app制作的,我没有使用Redux或Flux.

I'm trying to figure out how to change language using React-Intl. This is my first React App and it has been made with create-react-app, I'm not using Redux nor Flux.

在我的index.js中,我有以下代码:

In my index.js I have the following code:

import React from 'react';
import ReactDOM from 'react-dom';
import TodoApp from './components/TodoApp';
import registerServiceWorker from './registerServiceWorker';
import './index.css';

// Bootstrap CSS libraries
import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap/dist/css/bootstrap-theme.css';

import { IntlProvider, addLocaleData } from 'react-intl';
import intlEN from 'react-intl/locale-data/en';
import intlES from 'react-intl/locale-data/es';
import intlMessagesES from './i18n/locales/es.json';
import intlMessagesEN from './i18n/locales/en.json';

addLocaleData([...intlEN, ...intlES]);

/* Define your translations */
let i18nConfig = {
    locale: 'es',
    messages: intlMessagesES
};

let changeLanguage = (lang) => {
    i18nConfig = { locale: lang, messages: intlMessagesEN };
    return i18nConfig;
}

ReactDOM.render(
    <IntlProvider locale={ i18nConfig.locale } key={ i18nConfig.locale } messages={ i18nConfig.messages }>
        <TodoApp onChangeLanguage={changeLanguage} />
    </IntlProvider>,
    document.getElementById('root'));
registerServiceWorker();

TodoApp通过道具(即:'es'或'en')在'lang'参数上发送字符串,当我更改 i18nConfig 时,IntlProvider似乎没有任何变化.我的想法是先更改 i18nConfig 变量,然后我的所有应用程序也会更改语言.

TodoApp is sending a string on 'lang' parameter by props (i.e.: 'es' or 'en'), when I change i18nConfig nothing seems to change with IntlProvider. My thought was that change my i18nConfig variable then all my app would change language as well.

我在TodoApp中有FormattedMessages,我的两个JSON消息是这样填充的:

I have FormattedMessages in TodoApp and my two JSON messages are filled like this:

// i18n/locales/en.json
{
  "footer.add.placeholder": "Enter a name ...",
  "footer.add.priority0.text": "No priority",
  "footer.add.priority1.text": "Priority 1",
   ...
}

您知道我的代码中缺少什么吗?也许我对React-Intl不太了解.任何建议都会有所帮助,谢谢.

Do you know what am I missing on my code ?? Maybe I have not understand something right about React-Intl. Any advice will be helpful, thank you.

推荐答案

如果您将所有内容从根目录中删除,则它会起作用:

It works if you remove all from root:

ReactDOM.render(<TodoApp />, document.getElementById('root'));

但是现在我们像这样更改TodoApp组件:

But now we change TodoApp component like this:

1)我们将'locale'添加为组件状态并导入React-Intl:

1) We add 'locale' as component state and import React-Intl:

import { IntlProvider, addLocaleData } from 'react-intl';
import intlEN from 'react-intl/locale-data/en';
import intlES from 'react-intl/locale-data/es';
import intlMessagesES from './../i18n/locales/es.json';
import intlMessagesEN from './../i18n/locales/en.json';

addLocaleData([...intlEN, ...intlES]);

/* Define your default translations */
let i18nConfig = {
    locale: 'en',
    messages: intlMessagesEN
};

2)更改我们的changeLanguage函数(这次称为"onChangeLanguage"),此函数从子组件语言选择器接收"lang":

2) Change our changeLanguage function (this time called 'onChangeLanguage'), this function receives 'lang' from a subComponent language selector:

onChangeLanguage(lang) {
        switch (lang) {
            case 'ES': i18nConfig.messages = intlMessagesES; break;
            case 'EN': i18nConfig.messages = intlMessagesEN; break;
            default: i18nConfig.messages = intlMessagesEN; break;
        }
        this.setState({ locale: lang });
        i18nConfig.locale = lang;
}

最后渲染:

render() {
        return (
            <IntlProvider key={ i18nConfig.locale } locale={ i18nConfig.locale }  messages={ i18nConfig.messages }>
                <div>
                    <Header onChangeLanguage={this.onChangeLanguage} />
                    // Other components ...
                </div>
            </IntlProvider>
        );
    }

如果某人根本听不懂,请在评论中提问!感谢@TomásEhrich

If someone doesn't understand at all, ask in comments! Thanks to @TomásEhrich

这篇关于React-Intl如何从变量切换语言环境和消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆