React Redux Localize仅返回第一个字母

时间:2019-02-01 19:21:11

标签: reactjs localization react-redux

我正在尝试使用react redux,但是我遇到了一个问题。以下代码仅呈现第一个字母“ T”,而不呈现所有“ Test”。 Redux调试器显示所有“测试”。怎么会这样?

Index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './app';
import { localizeReducer } from 'react-localize-redux';
import { combineReducers, createStore } from 'redux';
import { LocalizeProvider } from "react-localize-redux";

const store = createStore(
    combineReducers({localize: localizeReducer})
);

ReactDOM.render(
    <LocalizeProvider store={store}>
        <App />
    </LocalizeProvider>,

    document.getElementById('root'));

App.js:

import React, { Component } from 'react';
import { Translate, withLocalize } from "react-localize-redux";
import { renderToStaticMarkup } from 'react-dom/server';

class App extends Component {
    constructor(props) {
        super(props);

        this.props.initialize({
            languages: [
              { name: "English", code: "en" }
            ],
            options: { renderToStaticMarkup }
        });

        let english = {
            abc: "Test"
        }

        this.props.addTranslation(english);
    }

    render() {
        return <Translate id="abc" />;
    }
}

export default withLocalize(App);

1 个答案:

答案 0 :(得分:0)

如果您只想添加一种语言的翻译,则可以使用addTranslationForLanguage。此一个采用单语言格式:

let english = {
  abc: "Test"
}

this.props.addTranslationForLanguage(english, 'en');
使用多种语言时,应使用

addTranslation。这个API需要在数据all-language-format,所以你会做这样的事情阵列支持多国语言:

let translations = {
  abc: ["Test"]
}

this.props.addTranslationForLanguage(translations);