ReactJS i18n连接-TypeError:locale.get不是函数

时间:2019-05-25 11:34:56

标签: reactjs react-redux internationalization

我正在尝试设置一个可以用两种语言访问的应用程序。我正在跟踪另一个应用程序的代码,它正好在那里,只是我的代码有错误:

  

TypeError:locale.get不是函数

这是 App.js

import React, { Component } from 'react'
import './App.scss'
import { PrivateRoute, history } from './routes'
import { Router, Route } from 'react-router-dom'
import { HomeContainer, LoginContainer } from './containers'
import { addLocaleData } from 'react-intl'
import ConnectedIntlProvider from './ConnectedIntlProvider'

import en from 'react-intl/locale-data/en'
import ro from 'react-intl/locale-data/ro'

addLocaleData([...en, ...ro])

class App extends Component {
  state = {
    persisted: false
  }

  renderRoutes() {
    return (
      <ConnectedIntlProvider>
        <Router history={history}>
          <div className="pageLayoutContainer">
            <PrivateRoute exact path="/" component={HomeContainer} />
            <Route exact path="/login" component={LoginContainer} />
          </div>
        </Router>
      </ConnectedIntlProvider>
    )
  }

  render() {
    return <div className="appContainer">{this.renderRoutes()}</div>
  }
}

export default App

,这是 ConnectedIntlProvider.js

import { connect } from 'react-redux'
import { IntlProvider } from 'react-intl'
import Immutable from 'immutable'

function mapStateToProps(state) {
  const locale = state.locale || Immutable.fromJS({})
  return {
    locale: locale.get('lang'),
    key: locale.get('lang'),
    messages: locale.get('messages').toJS()
  }
}

export default connect(mapStateToProps)(IntlProvider)

错误涉及此行

 locale: locale.get('lang'),

但是,即使删除它,我也会在密钥和消息上遇到相同的错误。

您知道为什么会这样吗?关于如何解决该问题的任何想法?

谢谢

0 个答案:

没有答案