refactor: add locale plugin (#2261)

* refactor: add locale plugin

* fix: dayjs locale
This commit is contained in:
chenos 2023-07-18 15:30:58 +08:00 committed by GitHub
parent a17c1ad4e4
commit d8529e1d06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 99 additions and 26 deletions

View File

@ -4,6 +4,76 @@ export type LocaleOptions = {
export { default as cron } from '../schema-component/antd/cron/locale';
export const dayjsLocale = {
'ar-EG': 'ar',
'az-AZ': 'az',
'bg-BG': 'bg',
'bn-BD': 'bn-bd',
'by-BY': 'en',
'ca-ES': 'ca',
'cs-CZ': 'cs',
'da-DK': 'da',
'de-DE': 'de',
'el-GR': 'el',
'en-GB': 'en-gb',
'en-US': 'en',
'es-ES': 'es',
'et-EE': 'et',
'fa-IR': 'fa',
'fi-FI': 'fi',
'fr-BE': 'fr',
'fr-CA': 'fr',
'fr-FR': 'fr',
'ga-IE': 'ga',
'gl-ES': 'gl',
'he-IL': 'he',
'hi-IN': 'hi',
'hr-HR': 'hr',
'hu-HU': 'hu',
'hy-AM': 'hy-am',
'id-ID': 'id',
'is-IS': 'is',
'it-IT': 'it',
'ja-JP': 'ja',
'ka-GE': 'ka',
'kk-KZ': 'kk',
'km-KH': 'km',
// 'kmr-IQ': { label: 'kmr_IQ' },
'kn-IN': 'kn',
'ko-KR': 'kko',
'ku-IQ': 'ku',
'lt-LT': 'lt',
'lv-LV': 'lv',
'mk-MK': 'mk',
'ml-IN': 'ml',
'mn-MN': 'mn',
'ms-MY': 'ms',
'nb-NO': 'nb',
'ne-NP': 'ne',
'nl-BE': 'nl-be',
'nl-NL': 'nl',
'pl-PL': 'pl',
'pt-BR': 'pt-br',
'pt-PT': 'pt',
'ro-RO': 'ro',
'ru-RU': 'ru',
'si-LK': 'si',
'sk-SK': 'sk',
'sl-SI': 'sl',
'sr-RS': 'sr',
'sv-SE': 'sv',
'ta-IN': 'ta',
'th-TH': 'th',
'tk-TK': 'tk',
'tr-TR': 'tr',
'uk-UA': 'uk',
'ur-PK': 'ur',
'vi-VN': 'vi',
'zh-CN': 'zh-cn',
'zh-HK': 'zh-hk',
'zh-TW': 'zh-tw',
};
export default {
'ar-EG': { label: 'العربية' },
'az-AZ': { label: 'Azərbaycan dili' },

View File

@ -2,6 +2,7 @@ import { dayjs } from '@nocobase/utils/client';
import { ConfigProvider } from 'antd';
import { loadConstrueLocale } from '../../antd-config-provider/loadConstrueLocale';
import { Plugin } from '../../application/Plugin';
import { dayjsLocale } from '../../locale';
export class LocalePlugin extends Plugin {
locales: any = {};
@ -25,6 +26,8 @@ export class LocalePlugin extends Plugin {
this.app.i18n.addResources(data?.data?.lang, key, data?.data?.resources[key] || {});
});
loadConstrueLocale(data?.data);
const dayjsLang = dayjsLocale[data?.data?.lang] || 'en';
await import(`dayjs/locale/${dayjsLang}`);
dayjs.locale(data?.data?.moment);
window['cronLocale'] = data?.data?.cron;
} catch (error) {

View File

@ -21,8 +21,8 @@ import type { ToolbarProps } from './types';
import { formatDate } from './utils';
const Weeks = ['month', 'week', 'day'] as View[];
const localizer = dayjsLocalizer(dayjs);
export const DeleteEventContext = React.createContext({
close: () => {},
});
@ -46,31 +46,6 @@ function Toolbar(props: ToolbarProps) {
);
}
const messages: any = {
allDay: '',
previous: (
<div>
<LeftOutlined />
</div>
),
next: (
<div>
<RightOutlined />
</div>
),
today: i18n.t('Today'),
month: i18n.t('Month'),
week: i18n.t('Week'),
work_week: i18n.t('Work week'),
day: i18n.t('Day'),
agenda: i18n.t('Agenda'),
date: i18n.t('Date'),
time: i18n.t('Time'),
event: i18n.t('Event'),
noEventsInRange: i18n.t('None'),
showMore: (count) => i18n.t('{{count}} more items', { count }),
};
const useEvents = (dataSource: any, fieldNames: any, date: Date, view: (typeof Weeks)[number]) => {
const { t } = useTranslation();
return useMemo(() => {
@ -214,6 +189,31 @@ export const Calendar: any = observer(
};
}, [showLunar]);
const messages: any = {
allDay: '',
previous: (
<div>
<LeftOutlined />
</div>
),
next: (
<div>
<RightOutlined />
</div>
),
today: i18n.t('Today'),
month: i18n.t('Month'),
week: i18n.t('Week'),
work_week: i18n.t('Work week'),
day: i18n.t('Day'),
agenda: i18n.t('Agenda'),
date: i18n.t('Date'),
time: i18n.t('Time'),
event: i18n.t('Event'),
noEventsInRange: i18n.t('None'),
showMore: (count) => i18n.t('{{count}} more items', { count }),
};
return wrapSSR(
<div className={`${hashId} ${containerClassName}`} style={{ height: fixedBlock ? '100%' : 700 }}>
<GlobalStyle />