This is in general the correct way of using LocaleProvider, yes, but...
You do have to be a careful when mixing an i18n librarys wrapper with antd's LocaleProvider, if you want language changes to propagate to both.
In the case of React-Boilerplate the locale is stored in Redux. For antd to be able to update locale when the app does, <LocaleProvider>
must be inserted inside the Redux provider, otherwise it will not have access to the locale state.
So your app.js
needs to become something like:
<Provider store={store}>
<LanguageProvider messages={messages}>
<LocaleProvider locale={...}>
<Router ... />
</LocaleProvider>
</LanguageProvider>
</Provider>,
Unfortunately this is not enough, since antd's LocaleProvider does not take a simple locale id string as its argument, but rather an object with the locale info itself.
This means that it is not possible to just insert <LocaleProvider>
in the wrapper chain as above. You must create you own component that takes the connects to the Redux locale prop and feeds <LocaleProvider>
with the correct locale object based on the string id from Redux.
Here is some (untested) code that I ripped out of a project with a similar structure and adapted to Intl (it was using i18next instead of Intl). Should give you an idea of one way to do it.
import React from 'react';
import { Router } from 'react-router';
import { Provider, connect } from 'react-redux';
import en_US from 'antd/lib/locale-provider/en_US';
import sv_SE from 'antd/lib/locale-provider/sv_SE';
import { LocaleProvidern } from 'antd';
class AntDesignPlusRouter extends React.Component {
state = {
antd_locale: en_US
}
componentWillUpdate( next ) {
let { locale } = next
if( !locale || locale === this.props.locale ) return;
switch( locale ) {
case 'sv':
this.setState( { antd_locale: sv_SE } );
break;
default:
case 'en':
this.setState( { antd_locale: en_US } );
break;
}
}
render() {
return (
<LocaleProvider locale={this.state.antd_locale}>
<Router {...this.props} />
</LocaleProvider>
)
}
}
const WrappedAntDesignPlusRouter = connect(
function mapStateToProps( state ) {
return {
locale: state.locale
}
},
function mapDispatchToProps( dispatch ) {
return {}
}
)( AntDesignPlusRouter );
class App extends React.Component {
render() {
return (
<Provider ...>
<LanguageProvider ...>
<WrappedAntDesignPlusRouter/>
</LanguageProvider >
</Provider>
);
}
}