Skip to content

Commit

Permalink
Feature (ant-design#18049)
Browse files Browse the repository at this point in the history
Feature
  • Loading branch information
ikobe authored Aug 2, 2019
2 parents 56309c0 + 3d91bb8 commit 4c23bc2
Show file tree
Hide file tree
Showing 162 changed files with 12,301 additions and 6,018 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2038,7 +2038,7 @@ timeline: true

```javascript
import { LocaleProvider } from 'antd';
import zhCN from 'antd/lib/locale-provider/zh_CN';
import zhCN from 'antd/lib/locale/zh_CN';

ReactDOM.render(
<LocaleProvider locale={zhCN}>
Expand Down
6 changes: 4 additions & 2 deletions components/_util/transButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import KeyCode from 'rc-util/lib/KeyCode';

interface TransButtonProps extends React.HTMLAttributes<HTMLDivElement> {
onClick?: (e?: React.MouseEvent<HTMLDivElement>) => void;
noStyle?: boolean;
}

const inlineStyle: React.CSSProperties = {
Expand Down Expand Up @@ -53,7 +54,8 @@ class TransButton extends React.Component<TransButtonProps> {
}

render() {
const { style } = this.props;
const { style, noStyle } = this.props;

return (
<div
role="button"
Expand All @@ -62,7 +64,7 @@ class TransButton extends React.Component<TransButtonProps> {
{...this.props}
onKeyDown={this.onKeyDown}
onKeyUp={this.onKeyUp}
style={{ ...inlineStyle, ...style }}
style={{ ...(!noStyle ? inlineStyle : null), ...style }}
/>
);
}
Expand Down
7 changes: 6 additions & 1 deletion components/_util/warning.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import warning from 'warning';

const warned: Record<string, boolean> = {};
let warned: Record<string, boolean> = {};

export function resetWarned() {
warned = {};
}

export default (valid: boolean, component: string, message: string): void => {
if (!valid && !warned[message]) {
warning(false, `[antd: ${component}] ${message}`);
Expand Down
2 changes: 2 additions & 0 deletions components/calendar/locale/ms_MY.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import ms_MY from '../../date-picker/locale/ms_MY';
export default ms_MY;
2 changes: 1 addition & 1 deletion components/collapse/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ cols: 1
| disabled | 禁用后的面板展开与否将无法通过用户交互改变 | boolean | false | |
| forceRender | 被隐藏时是否渲染 DOM 结构 | boolean | false | 3.2.0 |
| header | 面板头内容 | string\|ReactNode || |
| key | 对应 activeKey | string\|number || |
| key | 对应 activeKey | string\|number || |
| showArrow | 是否展示当前面板上的箭头 | boolean | `true` | 3.13.0 |
| extra | 自定义渲染每个面板右上角的内容 | ReactNode | - | 3.14.0 |
187 changes: 187 additions & 0 deletions components/config-provider/demo/locale.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
---
order: 1
title:
zh-CN: 国际化
en-US: Locale
---

## zh-CN

此处列出 Ant Design 中需要国际化支持的组件,你可以在演示里切换语言。

## en-US

Components which need localization support are listed here, you can toggle the language in the demo.

```jsx
import {
ConfigProvider,
Pagination,
DatePicker,
TimePicker,
Calendar,
Popconfirm,
Table,
Modal,
Button,
Select,
Transfer,
Radio,
} from 'antd';
import zhCN from 'antd/lib/locale/zh_CN';
import moment from 'moment';
import 'moment/locale/zh-cn';

moment.locale('en');

const { Option } = Select;
const { RangePicker } = DatePicker;

const columns = [
{
title: 'Name',
dataIndex: 'name',
filters: [
{
text: 'filter1',
value: 'filter1',
},
],
},
{
title: 'Age',
dataIndex: 'age',
},
];

class Page extends React.Component {
state = {
visible: false,
};

showModal = () => {
this.setState({ visible: true });
};

hideModal = () => {
this.setState({ visible: false });
};

render() {
const info = () => {
Modal.info({
title: 'some info',
content: 'some info',
});
};
const confirm = () => {
Modal.confirm({
title: 'some info',
content: 'some info',
});
};
return (
<div className="locale-components">
<div className="example">
<Pagination defaultCurrent={1} total={50} showSizeChanger />
</div>
<div className="example">
<Select showSearch style={{ width: 200 }}>
<Option value="jack">jack</Option>
<Option value="lucy">lucy</Option>
</Select>
<DatePicker />
<TimePicker />
<RangePicker style={{ width: 200 }} />
</div>
<div className="example">
<Button type="primary" onClick={this.showModal}>
Show Modal
</Button>
<Button onClick={info}>Show info</Button>
<Button onClick={confirm}>Show confirm</Button>
<Popconfirm title="Question?">
<a href="#">Click to confirm</a>
</Popconfirm>
</div>
<div className="example">
<Transfer dataSource={[]} showSearch targetKeys={[]} render={item => item.title} />
</div>
<div style={{ width: 319, border: '1px solid #d9d9d9', borderRadius: 4 }}>
<Calendar fullscreen={false} value={moment()} />
</div>
<div className="example">
<Table dataSource={[]} columns={columns} />
</div>
<Modal title="Locale Modal" visible={this.state.visible} onCancel={this.hideModal}>
<p>Locale Modal</p>
</Modal>
</div>
);
}
}

class App extends React.Component {
constructor() {
super();
this.state = {
locale: null,
};
}

changeLocale = e => {
const localeValue = e.target.value;
this.setState({ locale: localeValue });
if (!localeValue) {
moment.locale('en');
} else {
moment.locale('zh-cn');
}
};

render() {
const { locale } = this.state;
return (
<div>
<div className="change-locale">
<span style={{ marginRight: 16 }}>Change locale of components: </span>
<Radio.Group defaultValue={undefined} onChange={this.changeLocale}>
<Radio.Button key="en" value={undefined}>
English
</Radio.Button>
<Radio.Button key="cn" value={zhCN}>
中文
</Radio.Button>
</Radio.Group>
</div>
<ConfigProvider locale={locale}>
<Page
key={locale ? locale.locale : 'en' /* Have to refresh for production environment */}
/>
</ConfigProvider>
</div>
);
}
}

ReactDOM.render(<App />, mountNode);
```

```css
.locale-components {
border-top: 1px solid #d9d9d9;
padding-top: 16px;
}

.example {
margin: 16px 0;
}

.example > * {
margin-right: 8px;
}

.change-locale {
margin-bottom: 16px;
}
```
1 change: 1 addition & 0 deletions components/config-provider/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ Some component use dynamic style to support wave effect. You can config `csp` pr
| csp | Set [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) config | { nonce: string } | - | 3.13.1 |
| renderEmpty | set empty content of components. Ref [Empty](/components/empty/) | Function(componentName: string): ReactNode | - | 3.12.2 |
| getPopupContainer | to set the container of the popup element. The default is to create a `div` element in `body`. | Function(triggerNode) | `() => document.body` | 3.11.0 |
| locale | language package setting, you can find the packages in [antd/es/locale](http://unpkg.com/antd/es/locale/) | object | 3.21.0 |
| prefixCls | set prefix class | string | ant | 3.12.0 |
21 changes: 19 additions & 2 deletions components/config-provider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as React from 'react';
import createReactContext from '@ant-design/create-react-context';

import defaultRenderEmpty, { RenderEmptyHandler } from './renderEmpty';
import LocaleProvider, { Locale, ANT_MARK } from '../locale-provider';

export { RenderEmptyHandler };

Expand All @@ -16,6 +17,7 @@ export interface ConfigConsumerProps {
renderEmpty: RenderEmptyHandler;
csp?: CSPConfig;
autoInsertSpaceInButton?: boolean;
locale?: Locale;
}

export const configConsumerProps = [
Expand All @@ -25,6 +27,7 @@ export const configConsumerProps = [
'renderEmpty',
'csp',
'autoInsertSpaceInButton',
'locale',
];

export interface ConfigProviderProps {
Expand All @@ -34,6 +37,7 @@ export interface ConfigProviderProps {
renderEmpty?: RenderEmptyHandler;
csp?: CSPConfig;
autoInsertSpaceInButton?: boolean;
locale?: Locale;
}

const ConfigContext = createReactContext<ConfigConsumerProps>({
Expand All @@ -59,7 +63,14 @@ class ConfigProvider extends React.Component<ConfigProviderProps> {
};

renderProvider = (context: ConfigConsumerProps) => {
const { children, getPopupContainer, renderEmpty, csp, autoInsertSpaceInButton } = this.props;
const {
children,
getPopupContainer,
renderEmpty,
csp,
autoInsertSpaceInButton,
locale,
} = this.props;

const config: ConfigConsumerProps = {
...context,
Expand All @@ -75,7 +86,13 @@ class ConfigProvider extends React.Component<ConfigProviderProps> {
config.renderEmpty = renderEmpty;
}

return <ConfigContext.Provider value={config}>{children}</ConfigContext.Provider>;
return (
<ConfigContext.Provider value={config}>
<LocaleProvider locale={locale} _ANT_MARK__={ANT_MARK}>
{children}
</LocaleProvider>
</ConfigContext.Provider>
);
};

render() {
Expand Down
1 change: 1 addition & 0 deletions components/config-provider/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ return (
| csp | 设置 [Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) 配置 | { nonce: string } | - | 3.13.1 |
| renderEmpty | 自定义组件空状态。参考 [空状态](/components/empty/) | Function(componentName: string): ReactNode | - | 3.12.2 |
| getPopupContainer | 弹出框(Select, Tooltip, Menu 等等)渲染父节点,默认渲染到 body 上。 | Function(triggerNode) | () => document.body | 3.11.0 |
| locale | 语言包配置,语言包可到 [antd/es/locale](http://unpkg.com/antd/es/locale/) 目录下寻找 | object | - | 3.21.0 |
| prefixCls | 设置统一样式前缀 | string | ant | 3.12.0 |
2 changes: 1 addition & 1 deletion components/date-picker/index.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ There are four kinds of picker:

### Localization

The default locale is en-US, if you need to use other languages, recommend to use internationalized components provided by us at the entrance. Look at: [LocaleProvider](http://ant.design/components/locale-provider/).
The default locale is en-US, if you need to use other languages, recommend to use internationalized components provided by us at the entrance. Look at: [ConfigProvider](http://ant.design/components/config-provider/).

If there are special needs (only modifying single component language), Please use the property: local. Example: [default](https://github.com/ant-design/ant-design/blob/master/components/date-picker/locale/example.json).

Expand Down
2 changes: 1 addition & 1 deletion components/date-picker/index.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ subtitle: 日期选择框

### 国际化配置

默认配置为 en-US,如果你需要设置其他语言,推荐在入口处使用我们提供的国际化组件,详见:[LocaleProvider 国际化](http://ant.design/components/locale-provider-cn/)
默认配置为 en-US,如果你需要设置其他语言,推荐在入口处使用我们提供的国际化组件,详见:[ConfigProvider 国际化](http://ant.design/components/config-provider-cn/)

如有特殊需求(仅修改单一组件的语言),请使用 locale 参数,参考:[默认配置](https://github.com/ant-design/ant-design/blob/master/components/date-picker/locale/example.json)

Expand Down
19 changes: 19 additions & 0 deletions components/date-picker/locale/ms_MY.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import CalendarLocale from 'rc-calendar/lib/locale/ms_MY';
import TimePickerLocale from '../../time-picker/locale/ms_MY';

// Merge into a locale object
const locale = {
lang: {
placeholder: 'Pilih tarikh',
rangePlaceholder: ['Tarikh mula', 'Tarikh akhir'],
...CalendarLocale,
},
timePickerLocale: {
...TimePickerLocale,
},
};

// All settings at:
// https://github.com/ant-design/ant-design/blob/master/components/date-picker/locale/example.json

export default locale;
Loading

0 comments on commit 4c23bc2

Please sign in to comment.