feat(i18n): 增强生理周期模块的国际化支持,添加多语言格式和翻译
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import dayjs, { Dayjs } from 'dayjs';
|
||||
import 'dayjs/locale/en';
|
||||
import 'dayjs/locale/zh-cn';
|
||||
import { MenstrualFlowSample } from './health';
|
||||
|
||||
export type MenstrualDayStatus = 'period' | 'predicted-period' | 'fertile' | 'ovulation-day';
|
||||
@@ -153,6 +155,9 @@ export const buildMenstrualTimeline = (options?: {
|
||||
monthsAfter?: number;
|
||||
defaultCycleLength?: number;
|
||||
defaultPeriodLength?: number;
|
||||
locale?: 'zh' | 'en';
|
||||
monthTitleFormat?: string;
|
||||
monthSubtitleFormat?: string;
|
||||
}): MenstrualTimeline => {
|
||||
const today = dayjs();
|
||||
const monthsBefore = options?.monthsBefore ?? 2;
|
||||
@@ -267,6 +272,11 @@ export const buildMenstrualTimeline = (options?: {
|
||||
const months: MenstrualMonth[] = [];
|
||||
let monthCursor = startMonth.startOf('month');
|
||||
|
||||
const locale = options?.locale ?? 'zh';
|
||||
const localeKey = locale === 'en' ? 'en' : 'zh-cn';
|
||||
const monthTitleFormat = options?.monthTitleFormat ?? (locale === 'en' ? 'MMM' : 'M月');
|
||||
const monthSubtitleFormat = options?.monthSubtitleFormat ?? (locale === 'en' ? 'YYYY' : 'YYYY年');
|
||||
|
||||
while (monthCursor.isBefore(endMonth) || monthCursor.isSame(endMonth, 'month')) {
|
||||
const firstDay = monthCursor.startOf('month');
|
||||
const daysInMonth = firstDay.daysInMonth();
|
||||
@@ -298,10 +308,12 @@ export const buildMenstrualTimeline = (options?: {
|
||||
});
|
||||
}
|
||||
|
||||
const formattedMonth = firstDay.locale(localeKey);
|
||||
|
||||
months.push({
|
||||
id: firstDay.format('YYYY-MM'),
|
||||
title: firstDay.format('M月'),
|
||||
subtitle: firstDay.format('YYYY年'),
|
||||
title: formattedMonth.format(monthTitleFormat),
|
||||
subtitle: formattedMonth.format(monthSubtitleFormat),
|
||||
cells,
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user