Skip to content

Commit 6f049cb

Browse files
committed
feat: add locale data and useLocale hook
Add Locale type with en_US and zh_CN data covering DatePicker, TimePicker, Modal, PopConfirm, and Empty strings. Create useLocale hook that reads from ConfigContext -> IntlContext -> en_US fallback. Refactor IntlProvider to accept a Locale object instead of a string.
1 parent 11503cb commit 6f049cb

10 files changed

Lines changed: 108 additions & 4 deletions

File tree

components/_utils/use-locale.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { useContext } from 'react';
2+
import { Locale } from '../locale/types';
3+
import { ConfigContext } from '../config-provider/config-context';
4+
import IntlContext from '../intl-provider/intl-context';
5+
6+
export function useLocale(): Locale {
7+
const config = useContext(ConfigContext);
8+
const intl = useContext(IntlContext);
9+
return config.locale ?? intl;
10+
}

components/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ export { default as Typography } from './typography';
7272
export { default as Upload } from './upload';
7373

7474
export { withLocale } from './intl-provider/with-locale';
75+
export { en_US, zh_CN } from './locale';
76+
export type { Locale } from './locale';
7577

78+
export { useLocale } from './_utils/use-locale';
7679
export { useTheme } from './_utils/use-theme';
7780
export type { ThemeMode } from './_utils/use-theme';

components/intl-provider/__tests__/intl-provider.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { render } from '@testing-library/react';
22
import IntlProvider from '../index';
3+
import en_US from '../../locale/en_US';
34

45
describe('<IntlProvider />', () => {
56
it('should render children', () => {
67
const { getByText } = render(
7-
<IntlProvider locale="en">
8+
<IntlProvider locale={en_US}>
89
<div>Content</div>
910
</IntlProvider>
1011
);

components/intl-provider/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React from 'react';
2+
import { Locale } from '../locale/types';
23
import IntlContext from './intl-context';
34

45
export interface IntlProviderProps {
5-
locale: string;
6+
locale: Locale;
67
children: React.ReactNode;
78
}
89

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import React from 'react';
2+
import { Locale } from '../locale/types';
3+
import en_US from '../locale/en_US';
24

3-
export default React.createContext<string>('en_US');
5+
export default React.createContext<Locale>(en_US);

components/intl-provider/with-locale.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { useContext, ComponentType } from 'react';
2+
import { Locale } from '../locale/types';
23
import IntlContext from './intl-context';
34

4-
export function withLocale<T>(WrappedComponent: ComponentType<T>) {
5+
export function withLocale<T extends { locale?: Locale }>(WrappedComponent: ComponentType<T>) {
56
const WithLocale = (props: T) => {
67
const locale = useContext(IntlContext);
78
return <WrappedComponent {...props} locale={locale} />;

components/locale/en_US.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Locale } from './types';
2+
3+
const en_US: Locale = {
4+
locale: 'en_US',
5+
DatePicker: {
6+
weeks: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
7+
months: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
8+
today: 'Today',
9+
selectDate: 'Select date',
10+
selectMonth: 'Select month',
11+
selectYear: 'Select year',
12+
},
13+
TimePicker: {
14+
selectTime: 'Select time',
15+
},
16+
Modal: {
17+
okText: 'OK',
18+
cancelText: 'Cancel',
19+
},
20+
PopConfirm: {
21+
okText: 'Yes',
22+
cancelText: 'No',
23+
},
24+
Empty: {
25+
description: 'No Data',
26+
},
27+
};
28+
29+
export default en_US;

components/locale/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type { Locale } from './types';
2+
export { default as en_US } from './en_US';
3+
export { default as zh_CN } from './zh_CN';

components/locale/types.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export type Locale = {
2+
locale: string;
3+
DatePicker: {
4+
weeks: string[];
5+
months: string[];
6+
today: string;
7+
selectDate: string;
8+
selectMonth: string;
9+
selectYear: string;
10+
};
11+
TimePicker: {
12+
selectTime: string;
13+
};
14+
Modal: {
15+
okText: string;
16+
cancelText: string;
17+
};
18+
PopConfirm: {
19+
okText: string;
20+
cancelText: string;
21+
};
22+
Empty: {
23+
description: string;
24+
};
25+
};

components/locale/zh_CN.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Locale } from './types';
2+
3+
const zh_CN: Locale = {
4+
locale: 'zh_CN',
5+
DatePicker: {
6+
weeks: ['日', '一', '二', '三', '四', '五', '六'],
7+
months: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
8+
today: '今天',
9+
selectDate: '请选择日期',
10+
selectMonth: '请选择月份',
11+
selectYear: '请选择年份',
12+
},
13+
TimePicker: {
14+
selectTime: '请选择时间',
15+
},
16+
Modal: {
17+
okText: '确定',
18+
cancelText: '取消',
19+
},
20+
PopConfirm: {
21+
okText: '确定',
22+
cancelText: '取消',
23+
},
24+
Empty: {
25+
description: '暂无数据',
26+
},
27+
};
28+
29+
export default zh_CN;

0 commit comments

Comments
 (0)