fix: make sure en|ru locale import after zh-CN (#3246)

* fix: make sure en|ru locale import after zh-CN

* refactor: simplify code
This commit is contained in:
Benjy Cui 2016-10-01 00:17:31 +08:00 committed by 偏右
parent e4ed800b61
commit 5705f72fb8
6 changed files with 42 additions and 37 deletions

View File

@ -0,0 +1,14 @@
import assign from 'object-assign';
export default function getLocale(props, context, component, getDefaultLocale) {
let locale = null;
if (context && context.antLocale && context.antLocale[component]) {
locale = context.antLocale[component];
} else {
locale = getDefaultLocale();
}
// 统一合并为完整的 Locale
const result = assign({}, locale, props.locale);
result.lang = assign({}, locale.lang, props.locale.lang);
return result;
}

View File

@ -2,10 +2,10 @@ import React from 'react';
import { PropTypes } from 'react';
import moment from 'moment';
import FullCalendar from 'rc-calendar/lib/FullCalendar';
import defaultLocale from './locale/zh_CN';
import { PREFIX_CLS } from './Constants';
import Header from './Header';
import assign from 'object-assign';
import getLocale from '../_util/getLocale';
declare const require: Function;
function noop() { return null; }
@ -81,19 +81,6 @@ export default class Calendar extends React.Component<CalendarProps, any> {
}
}
getLocale = () => {
const props = this.props;
const context = this.context;
let locale = defaultLocale;
if (context && context.antLocale && context.antLocale.Calendar) {
locale = context.antLocale.Calendar;
}
// 统一合并为完整的 Locale
const result = assign({}, locale, props.locale);
result.lang = assign({}, locale.lang, props.locale.lang);
return result;
}
monthCellRender = (value) => {
const prefixCls = this.props.prefixCls;
return (
@ -142,7 +129,10 @@ export default class Calendar extends React.Component<CalendarProps, any> {
const { value, mode } = this.state;
const { prefixCls, style, className, fullscreen } = props;
const type = (mode === 'year') ? 'month' : 'date';
const locale = this.getLocale();
const locale = getLocale(
this.props, this.context, 'Calendar',
() => require('./locale/zh_CN')
);
let cls = className || '';
if (fullscreen) {

View File

@ -2,9 +2,9 @@ import React from 'react';
import { PropTypes } from 'react';
import TimePickerPanel from 'rc-time-picker/lib/Panel';
import classNames from 'classnames';
import defaultLocale from './locale/zh_CN';
import assign from 'object-assign';
import warning from 'warning';
import getLocale from '../_util/getLocale';
declare const require: Function;
export default function wrapPicker(Picker, defaultFormat?) {
const PickerWrapper = React.createClass({
@ -32,19 +32,6 @@ export default function wrapPicker(Picker, defaultFormat?) {
antLocale: PropTypes.object,
},
getLocale() {
const props = this.props;
const context = this.context;
let locale = defaultLocale;
if (context.antLocale && context.antLocale.DatePicker) {
locale = context.antLocale.DatePicker;
}
// 统一合并为完整的 Locale
const result = assign({}, locale, props.locale);
result.lang = assign({}, locale.lang, props.locale.lang);
return result;
},
handleOpenChange(open) {
const { onOpenChange, toggleOpen } = this.props;
onOpenChange(open);
@ -72,7 +59,10 @@ export default function wrapPicker(Picker, defaultFormat?) {
[`${inputPrefixCls}-sm`]: props.size === 'small',
});
const locale = this.getLocale();
const locale = getLocale(
this.props, this.context, 'DatePicker',
() => require('./locale/zh_CN')
);
const timeFormat = (props.showTime && props.showTime.format) || 'HH:mm:ss';
const rcTimePickerProps = {

View File

@ -1,3 +1,6 @@
import moment from 'moment';
moment.locale('en');
import Pagination from 'rc-pagination/lib/locale/en_US';
import DatePicker from '../date-picker/locale/en_US';
import TimePicker from '../time-picker/locale/en_US';

View File

@ -2,6 +2,10 @@
* Created by Andrey Gayvoronsky on 13/04/16.
*/
import moment from 'moment';
import 'moment/locale/ru';
moment.locale('ru');
import Pagination from 'rc-pagination/lib/locale/ru_RU';
import DatePicker from '../date-picker/locale/ru_RU';
import TimePicker from '../time-picker/locale/ru_RU';

View File

@ -1,6 +1,8 @@
import React from 'react';
import ReactDOM from 'react-dom';
import { addLocaleData, IntlProvider } from 'react-intl';
import { LocaleProvider } from 'antd';
import enUS from 'antd/lib/locale-provider/en_US';
import Header from './Header';
import Footer from './Footer';
import enLocale from '../../en-US';
@ -47,11 +49,13 @@ export default class Layout extends React.Component {
const { children, ...restProps } = this.props;
return (
<IntlProvider locale={appLocale.locale} messages={appLocale.messages}>
<div className="page-wrapper">
<Header {...restProps} />
{children}
<Footer />
</div>
<LocaleProvider locale={enUS}>
<div className="page-wrapper">
<Header {...restProps} />
{children}
<Footer />
</div>
</LocaleProvider>
</IntlProvider>
);
}