mirror of
https://github.com/ant-design/ant-design.git
synced 2024-11-24 02:59:58 +08:00
Add LocalProvider, ref #1055
This commit is contained in:
parent
2e823f3c5b
commit
d8f599fd3a
29
components/locale-provider/demo/basic.md
Normal file
29
components/locale-provider/demo/basic.md
Normal file
@ -0,0 +1,29 @@
|
||||
# 国际化
|
||||
|
||||
- order: 1
|
||||
|
||||
用 `LocaleProvider` 包裹你的应用,并引用对应的语言包。
|
||||
|
||||
---
|
||||
|
||||
````jsx
|
||||
import { Pagination, DatePicker, LocaleProvider } from 'antd';
|
||||
import enUS from 'antd/lib/locale-provider/en_US';
|
||||
|
||||
const App = React.createClass({
|
||||
render() {
|
||||
return (
|
||||
<div>
|
||||
<DatePicker />
|
||||
<Pagination defaultCurrent={1} total={50} showSizeChanger />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
ReactDOM.render(
|
||||
<LocaleProvider locale={enUS}>
|
||||
<App />
|
||||
</LocaleProvider>
|
||||
, mountNode);
|
||||
````
|
3
components/locale-provider/en_US.js
Normal file
3
components/locale-provider/en_US.js
Normal file
@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
Pagination: require('rc-pagination/lib/locale/en_US'),
|
||||
};
|
20
components/locale-provider/index.jsx
Normal file
20
components/locale-provider/index.jsx
Normal file
@ -0,0 +1,20 @@
|
||||
import React from 'react';
|
||||
|
||||
export default class LocaleProvider extends React.Component {
|
||||
getChildContext() {
|
||||
return {
|
||||
locale: this.props.locale,
|
||||
};
|
||||
}
|
||||
render() {
|
||||
return React.cloneElement(this.props.children);
|
||||
}
|
||||
}
|
||||
|
||||
LocaleProvider.childContextTypes = {
|
||||
locale: React.PropTypes.object,
|
||||
};
|
||||
|
||||
LocaleProvider.propTypes = {
|
||||
locale: React.PropTypes.object,
|
||||
};
|
21
components/locale-provider/index.md
Normal file
21
components/locale-provider/index.md
Normal file
@ -0,0 +1,21 @@
|
||||
# LocaleProvider
|
||||
|
||||
- category: Components
|
||||
- chinese: 国际化
|
||||
- cols: 1
|
||||
|
||||
---
|
||||
|
||||
为组件文案提供统一的国际化配置功能。
|
||||
|
||||
## API
|
||||
|
||||
Wrap your App with `LocaleProvider` once.
|
||||
|
||||
```jsx
|
||||
<LocaleProvider locale={enUS}>
|
||||
<App />
|
||||
</LocaleProvider>
|
||||
```
|
||||
|
||||
### Add a language
|
@ -7,10 +7,12 @@
|
||||
---
|
||||
|
||||
````jsx
|
||||
import { Pagination } from 'antd';
|
||||
import enUS from 'antd/lib/pagination/locale/en_US';
|
||||
import { Pagination, LocaleProvider } from 'antd';
|
||||
import enUS from 'antd/lib/locale-provider/en_US';
|
||||
|
||||
ReactDOM.render(
|
||||
<Pagination defaultCurrent={1} total={50} locale={enUS} />,
|
||||
<LocaleProvider locale={enUS}>
|
||||
<Pagination defaultCurrent={1} total={50} />
|
||||
</LocaleProvider>,
|
||||
mountNode);
|
||||
````
|
||||
|
@ -16,6 +16,13 @@ class AntPagination extends React.Component {
|
||||
let className = this.props.className;
|
||||
let selectComponentClass = Select;
|
||||
|
||||
let locale;
|
||||
if (this.context.locale && this.context.locale.Pagination) {
|
||||
locale = this.context.locale.Pagination;
|
||||
} else {
|
||||
locale = this.props.locale;
|
||||
}
|
||||
|
||||
if (this.props.size === 'small') {
|
||||
className += ' mini';
|
||||
selectComponentClass = MiniSelect;
|
||||
@ -25,6 +32,7 @@ class AntPagination extends React.Component {
|
||||
<Pagination selectComponentClass={selectComponentClass}
|
||||
selectPrefixCls="ant-select"
|
||||
{...this.props}
|
||||
locale={locale}
|
||||
className={className} />
|
||||
);
|
||||
}
|
||||
@ -36,4 +44,8 @@ AntPagination.defaultProps = {
|
||||
prefixCls: 'ant-pagination',
|
||||
};
|
||||
|
||||
AntPagination.contextTypes = {
|
||||
locale: React.PropTypes.object,
|
||||
};
|
||||
|
||||
export default AntPagination;
|
||||
|
1
index.js
1
index.js
@ -46,6 +46,7 @@ const antd = {
|
||||
Transfer: require('./components/transfer'),
|
||||
Cascader: require('./components/cascader'),
|
||||
Card: require('./components/card'),
|
||||
LocaleProvider: require('./components/locale-provider'),
|
||||
};
|
||||
|
||||
module.exports = antd;
|
||||
|
@ -61,6 +61,8 @@ antd.Pagination.locale = {
|
||||
zh_CN: require('../components/pagination/locale/zh_CN'),
|
||||
};
|
||||
|
||||
antd.LocaleProvider['en_US'] = require('../components/locale-provider/en_US'),
|
||||
|
||||
InstantClickChangeFns.push(function () {
|
||||
// auto complete for components
|
||||
var Select = antd.Select;
|
||||
|
Loading…
Reference in New Issue
Block a user