locale.js 1.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
import 'moment/locale/zh-cn';

import React from 'react';

import { ConfigProvider } from 'antd';
import moment from 'moment';

import {
  getDirection,
  getIntl,
  getLocale,
  localeInfo,
  RawIntlProvider,
  setIntl,
} from './localeExports';

export const LANG_CHANGE_EVENT = Symbol('LANG_CHANGE');

export function onCreate() {
  const locale = getLocale();
  if (moment.locale) {
    moment.locale(
      (localeInfo[locale] && localeInfo[locale].momentLocale) || '',
    );
  }
  setIntl(locale);
}

export const LocaleContainer = props => {
  const [locale, setLocale] = React.useState(() => getLocale());
  const [intl, setContainerIntl] = React.useState(() => getIntl(locale, true));
  const defaultAntdLocale = {};
  const direcition = getDirection();
  return (
    <ConfigProvider
      direction={direcition}
      locale={
        (localeInfo[locale] && localeInfo[locale].antd) || defaultAntdLocale
      }
    >
      <RawIntlProvider value={intl}>{props.children}</RawIntlProvider>
    </ConfigProvider>
  );
};