From e3c38ad01d0b7e61be97bf26fd13b31af4cf3da9 Mon Sep 17 00:00:00 2001 From: litaoq Date: Mon, 31 Jul 2023 19:50:37 +0800 Subject: [PATCH 1/2] refactor(Calendar): change locale from method to string --- src/components/calendar/calendar.tsx | 9 +++++---- src/locales/base.ts | 2 +- src/locales/da-DK.ts | 2 +- src/locales/es-ES.ts | 2 +- src/locales/fa-IR.ts | 2 +- src/locales/fr-FR.ts | 3 +-- src/locales/id-ID.ts | 3 +-- src/locales/it-IT.ts | 2 +- src/locales/ja-JP.ts | 2 +- src/locales/kk-KZ.ts | 3 +-- src/locales/ko-KR.ts | 3 +-- src/locales/nb-NO.ts | 2 +- src/locales/nl-NL.ts | 2 +- src/locales/ru-RU.ts | 2 +- src/locales/zh-CN.ts | 2 +- src/locales/zh-HK.ts | 2 +- src/locales/zh-TW.ts | 2 +- src/utils/replace-message.ts | 10 ++++++++++ 18 files changed, 31 insertions(+), 24 deletions(-) create mode 100644 src/utils/replace-message.ts diff --git a/src/components/calendar/calendar.tsx b/src/components/calendar/calendar.tsx index 2ff6d92275..e05e57b6de 100644 --- a/src/components/calendar/calendar.tsx +++ b/src/components/calendar/calendar.tsx @@ -15,6 +15,7 @@ import { useConfig } from '../config-provider' import isoWeek from 'dayjs/plugin/isoWeek' import { useUpdateEffect } from 'ahooks' import { usePropsValue } from '../../utils/use-props-value' +import { replaceMessage } from '../../utils/replace-message' import { convertValueToRange, convertPageToDayjs, @@ -171,10 +172,10 @@ export const Calendar = forwardRef((p, ref) => { {props.prevMonthButton}
- {locale.Calendar.renderYearAndMonth( - current.year(), - current.month() + 1 - )} + {replaceMessage(locale.Calendar.yearAndMonth, { + year: current.year().toString(), + month: (current.month() + 1).toString(), + })}
`${year}/${month}`, + yearAndMonth: '${year}/${month}', }, Cascader: { placeholder: 'Selecting', diff --git a/src/locales/da-DK.ts b/src/locales/da-DK.ts index 79683c193e..6e928f04ca 100644 --- a/src/locales/da-DK.ts +++ b/src/locales/da-DK.ts @@ -13,7 +13,7 @@ const daDK = mergeLocale(base, { }, Calendar: { markItems: ['ma', 'ti', 'on', 'to', 'fr', 'lø', 'sø'], - renderYearAndMonth: (year: number, month: number) => `${month}.${year}`, + yearAndMonth: '${month}.${year}', }, Cascader: { placeholder: 'Vælger', diff --git a/src/locales/es-ES.ts b/src/locales/es-ES.ts index fa75a67fee..b7fc56b8b9 100644 --- a/src/locales/es-ES.ts +++ b/src/locales/es-ES.ts @@ -12,7 +12,7 @@ const esES = mergeLocale(base, { }, Calendar: { markItems: ['Lun', 'Mar', 'Mie', 'Jue', 'Vie', 'Sab', 'Dom'], - renderYearAndMonth: (year: number, month: number) => `${year}/${month}`, + yearAndMonth: '${year}/${month}', }, Cascader: { placeholder: 'Seleccionando', diff --git a/src/locales/fa-IR.ts b/src/locales/fa-IR.ts index a703ed6d09..a79899abee 100644 --- a/src/locales/fa-IR.ts +++ b/src/locales/fa-IR.ts @@ -19,7 +19,7 @@ const faIR = mergeLocale(base, { 'پنج‌نشبه', 'جمعه', ], - renderYearAndMonth: (year: number, month: number) => `${year}/${month}`, + yearAndMonth: '${year}/${month}', }, Cascader: { placeholder: 'انتخاب کنید', diff --git a/src/locales/fr-FR.ts b/src/locales/fr-FR.ts index ce84d14acb..becc248234 100644 --- a/src/locales/fr-FR.ts +++ b/src/locales/fr-FR.ts @@ -12,8 +12,7 @@ const frFR = mergeLocale(base, { }, Calendar: { markItems: ['un', 'deux', 'trois', 'quatre', 'cinq', 'six', 'sept'], - renderYearAndMonth: (year: number, month: number) => - `${year}Année ${month}Mois`, + yearAndMonth: '${year}Année ${month}Mois', }, Cascader: { placeholder: 'Veuillez sélectionner', diff --git a/src/locales/id-ID.ts b/src/locales/id-ID.ts index b18f3fe7a4..545fd7a70d 100644 --- a/src/locales/id-ID.ts +++ b/src/locales/id-ID.ts @@ -12,8 +12,7 @@ const idID = mergeLocale(base, { }, Calendar: { markItems: ['Senin', 'Selasa', 'Rabu', 'Kamis', 'Jumat', 'Sabtu', 'Minggu'], - renderYearAndMonth: (year: number, month: number) => - `${year} Tahun ${month} Bulan`, + yearAndMonth: '${year} Tahun ${month} Bulan', }, Cascader: { placeholder: 'Silahkan Pilih', diff --git a/src/locales/it-IT.ts b/src/locales/it-IT.ts index cb2927995b..e3a303dc10 100644 --- a/src/locales/it-IT.ts +++ b/src/locales/it-IT.ts @@ -13,7 +13,7 @@ const itIT = mergeLocale(base, { }, Calendar: { markItems: ['lun', 'mar', 'mer', 'gio', 'ven', 'sab', 'dom'], - renderYearAndMonth: (year: number, month: number) => `${month}/${year}`, + yearAndMonth: '${month}/${year}', }, Cascader: { placeholder: 'Si prega di selezionare', diff --git a/src/locales/ja-JP.ts b/src/locales/ja-JP.ts index 096698a540..564d729d44 100644 --- a/src/locales/ja-JP.ts +++ b/src/locales/ja-JP.ts @@ -12,7 +12,7 @@ const jaJP = mergeLocale(base, { }, Calendar: { markItems: ['月', '火', '水', '木', '金', '土', '日'], - renderYearAndMonth: (year: number, month: number) => ` ${year}年${month}月`, + yearAndMonth: '${year}年${month}月', }, Cascader: { placeholder: '選択下さい', diff --git a/src/locales/kk-KZ.ts b/src/locales/kk-KZ.ts index f73ffb0bcf..638a56bfd5 100644 --- a/src/locales/kk-KZ.ts +++ b/src/locales/kk-KZ.ts @@ -12,8 +12,7 @@ const kkKZ = mergeLocale(base, { }, Calendar: { markItems: ['Дү', 'Се', 'Сә', 'Бе', 'Жұ', 'Се', 'Же'], - renderYearAndMonth: (year: number, month: number) => - `${year}жыл${month}-ші ай`, + yearAndMonth: '${year}жыл${month}-ші ай', }, Cascader: { placeholder: 'Таңдаңыз', diff --git a/src/locales/ko-KR.ts b/src/locales/ko-KR.ts index 02cecf8235..7434fab17b 100644 --- a/src/locales/ko-KR.ts +++ b/src/locales/ko-KR.ts @@ -13,7 +13,7 @@ const koKR = mergeLocale(base, { }, Calendar: { markItems: ['월', '화', '수', '목', '금', '토', '일'], - renderYearAndMonth: (year: number, month: number) => `${year}년 ${month}월`, + yearAndMonth: '${year}년 ${month}월', }, Cascader: { placeholder: '선택됨', @@ -132,7 +132,6 @@ const koKR = mergeLocale(base, { Switch: { name: '스위치', }, - }) export default koKR diff --git a/src/locales/nb-NO.ts b/src/locales/nb-NO.ts index 27c54cf6b1..5c2f213343 100644 --- a/src/locales/nb-NO.ts +++ b/src/locales/nb-NO.ts @@ -13,7 +13,7 @@ const nbNO = mergeLocale(base, { }, Calendar: { markItems: ['Man', 'Tir', 'Ons', 'Tor', 'Fre', 'Lør', 'Søn'], - renderYearAndMonth: (year: number, month: number) => `${month}.${year}`, + yearAndMonth: '${month}.${year}', }, Cascader: { placeholder: 'Velg', diff --git a/src/locales/nl-NL.ts b/src/locales/nl-NL.ts index 9cc5fb9c7d..5783129c37 100644 --- a/src/locales/nl-NL.ts +++ b/src/locales/nl-NL.ts @@ -13,7 +13,7 @@ const nlNL = mergeLocale(base, { }, Calendar: { markItems: ['Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za', 'Zo'], - renderYearAndMonth: (year: number, month: number) => `${month}-${year}`, + yearAndMonth: '${month}-${year}', }, Cascader: { placeholder: 'Selecteren', diff --git a/src/locales/ru-RU.ts b/src/locales/ru-RU.ts index 37d736257c..8baac01191 100644 --- a/src/locales/ru-RU.ts +++ b/src/locales/ru-RU.ts @@ -13,7 +13,7 @@ const ruRU = mergeLocale(base, { }, Calendar: { markItems: ['Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб', 'Вс'], - renderYearAndMonth: (year: number, month: number) => `${year}/${month}`, + yearAndMonth: '${year}/${month}', }, Cascader: { placeholder: 'Выбор', diff --git a/src/locales/zh-CN.ts b/src/locales/zh-CN.ts index 2721cffb4b..5600304134 100644 --- a/src/locales/zh-CN.ts +++ b/src/locales/zh-CN.ts @@ -13,7 +13,7 @@ const zhCN = mergeLocale(base, { }, Calendar: { markItems: ['一', '二', '三', '四', '五', '六', '日'], - renderYearAndMonth: (year: number, month: number) => `${year}年${month}月`, + yearAndMonth: '${year}年${month}月', }, Cascader: { placeholder: '请选择', diff --git a/src/locales/zh-HK.ts b/src/locales/zh-HK.ts index 430fd5be1c..6a2fdf808b 100644 --- a/src/locales/zh-HK.ts +++ b/src/locales/zh-HK.ts @@ -13,7 +13,7 @@ const zhHK = mergeLocale(base, { }, Calendar: { markItems: ['一', '二', '三', '四', '五', '六', '日'], - renderYearAndMonth: (year: number, month: number) => `${year}年${month}月`, + yearAndMonth: '${year}年${month}月', }, Cascader: { placeholder: '請選擇', diff --git a/src/locales/zh-TW.ts b/src/locales/zh-TW.ts index 36f9b74fa2..01ab77e09d 100644 --- a/src/locales/zh-TW.ts +++ b/src/locales/zh-TW.ts @@ -13,7 +13,7 @@ const zhTW = mergeLocale(base, { }, Calendar: { markItems: ['一', '二', '三', '四', '五', '六', '日'], - renderYearAndMonth: (year: number, month: number) => `${year}年${month}月`, + yearAndMonth: '${year}年${month}月', }, Cascader: { placeholder: '請選擇', diff --git a/src/utils/replace-message.ts b/src/utils/replace-message.ts new file mode 100644 index 0000000000..1b2298def9 --- /dev/null +++ b/src/utils/replace-message.ts @@ -0,0 +1,10 @@ +// 移植自 field-form https://github.com/react-component/field-form/blob/master/src/utils/validateUtil.ts#L21 +export function replaceMessage( + template: string, + kv: Record +): string { + return template.replace(/\$\{\w+\}/g, (str: string) => { + const key = str.slice(2, -1) + return kv[key] + }) +} From 4d5ba61c69d50527c82e9c720afb230672cf87e1 Mon Sep 17 00:00:00 2001 From: whwnow Date: Mon, 31 Jul 2023 22:11:02 +0800 Subject: [PATCH 2/2] test(ConfigProvider): remove spaces in Japanese translation in calendar --- .../tests/__snapshots__/config-provider.test.tsx.snap | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/config-provider/tests/__snapshots__/config-provider.test.tsx.snap b/src/components/config-provider/tests/__snapshots__/config-provider.test.tsx.snap index da49dbf8ce..2d97539be6 100644 --- a/src/components/config-provider/tests/__snapshots__/config-provider.test.tsx.snap +++ b/src/components/config-provider/tests/__snapshots__/config-provider.test.tsx.snap @@ -8618,7 +8618,7 @@ exports[`ConfigProvider should display the text as ja-JP 1`] = `
- 2022年3月 + 2022年3月