2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2018-12-07 20:07:33 +08:00
|
|
|
import defaultLocaleData from './default';
|
2020-03-30 22:54:12 +08:00
|
|
|
import LocaleContext from './context';
|
2020-08-21 12:58:14 +08:00
|
|
|
import { Locale } from '.';
|
2018-12-07 20:07:33 +08:00
|
|
|
|
2021-09-13 12:15:18 +08:00
|
|
|
export type LocaleComponentName = Exclude<keyof Locale, 'locale'>;
|
|
|
|
|
|
|
|
export interface LocaleReceiverProps<C extends LocaleComponentName = LocaleComponentName> {
|
2021-06-03 09:36:04 +08:00
|
|
|
componentName: C;
|
|
|
|
defaultLocale?: Locale[C] | (() => Locale[C]);
|
2021-09-13 12:15:18 +08:00
|
|
|
children: (
|
|
|
|
locale: Exclude<Locale[C], undefined>,
|
|
|
|
localeCode?: string,
|
|
|
|
fullLocale?: object,
|
|
|
|
) => React.ReactNode;
|
2017-10-20 14:54:38 +08:00
|
|
|
}
|
|
|
|
|
2021-09-13 12:15:18 +08:00
|
|
|
export default class LocaleReceiver<
|
|
|
|
C extends LocaleComponentName = LocaleComponentName,
|
|
|
|
> extends React.Component<LocaleReceiverProps<C>> {
|
2018-12-07 20:07:33 +08:00
|
|
|
static defaultProps = {
|
2018-12-07 21:02:05 +08:00
|
|
|
componentName: 'global',
|
2018-12-07 20:07:33 +08:00
|
|
|
};
|
|
|
|
|
2020-03-30 22:54:12 +08:00
|
|
|
static contextType = LocaleContext;
|
2017-10-20 14:54:38 +08:00
|
|
|
|
2021-06-03 09:36:04 +08:00
|
|
|
getLocale(): Exclude<Locale[C], undefined> {
|
2017-10-20 14:54:38 +08:00
|
|
|
const { componentName, defaultLocale } = this.props;
|
2021-09-13 12:15:18 +08:00
|
|
|
const locale = defaultLocale || defaultLocaleData[componentName ?? 'global'];
|
2020-03-30 22:54:12 +08:00
|
|
|
const antLocale = this.context;
|
2018-12-07 20:07:33 +08:00
|
|
|
const localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
|
2017-10-20 14:54:38 +08:00
|
|
|
return {
|
2021-06-03 09:36:04 +08:00
|
|
|
...(locale instanceof Function ? locale() : locale),
|
2017-10-20 14:54:38 +08:00
|
|
|
...(localeFromContext || {}),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getLocaleCode() {
|
2020-03-30 22:54:12 +08:00
|
|
|
const antLocale = this.context;
|
2017-10-20 14:54:38 +08:00
|
|
|
const localeCode = antLocale && antLocale.locale;
|
|
|
|
// Had use LocaleProvide but didn't set locale
|
|
|
|
if (antLocale && antLocale.exist && !localeCode) {
|
2018-12-07 20:07:33 +08:00
|
|
|
return defaultLocaleData.locale;
|
2017-10-20 14:54:38 +08:00
|
|
|
}
|
|
|
|
return localeCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2020-03-30 22:54:12 +08:00
|
|
|
return this.props.children(this.getLocale(), this.getLocaleCode(), this.context);
|
2017-10-20 14:54:38 +08:00
|
|
|
}
|
|
|
|
}
|
2020-08-21 12:58:14 +08:00
|
|
|
|
2021-09-13 12:15:18 +08:00
|
|
|
export function useLocaleReceiver<T extends LocaleComponentName>(
|
2020-08-21 12:58:14 +08:00
|
|
|
componentName: T,
|
|
|
|
defaultLocale?: Locale[T] | Function,
|
|
|
|
): [Locale[T]] {
|
|
|
|
const antLocale = React.useContext(LocaleContext);
|
|
|
|
|
|
|
|
const componentLocale = React.useMemo(() => {
|
|
|
|
const locale = defaultLocale || defaultLocaleData[componentName || 'global'];
|
|
|
|
const localeFromContext = componentName && antLocale ? antLocale[componentName] : {};
|
2020-08-22 22:31:20 +08:00
|
|
|
|
2020-08-21 12:58:14 +08:00
|
|
|
return {
|
2020-08-22 22:31:20 +08:00
|
|
|
...(typeof locale === 'function' ? (locale as Function)() : locale),
|
2020-08-21 12:58:14 +08:00
|
|
|
...(localeFromContext || {}),
|
|
|
|
};
|
|
|
|
}, [componentName, defaultLocale, antLocale]);
|
|
|
|
|
|
|
|
return [componentLocale];
|
|
|
|
}
|