修复切换语言时间格式没有变化

This commit is contained in:
eoao
2025-09-21 19:26:50 +08:00
parent 6dc126d3b0
commit ea8ad14049
7 changed files with 358 additions and 226 deletions

View File

@@ -166,6 +166,7 @@ async function copyEmail(email) {
}
function changeLang(lang) {
setExtend(lang === 'en' ? 'en' : 'zh-cn')
settingStore.lang = lang
}

View File

@@ -6,7 +6,7 @@ import {useSettingStore} from "@/store/setting.js";
const settingStore = useSettingStore();
dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.locale(settingStore.lang === 'zh' ? 'zh-cn' : '')
dayjs.locale(settingStore.lang === 'en' ? 'en' : 'zh-cn')
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
export function fromNow(date) {
@@ -16,25 +16,7 @@ export function fromNow(date) {
const diffMinutes = now.diff(d, 'minute');
const diffHours = now.diff(d, 'hour');
const isToday = now.isSame(d, 'day');
if (settingStore.lang === 'zh') {
if (isToday) {
if (diffSeconds < 60) return `几秒前`;
if (diffMinutes < 60) return `${diffMinutes}分钟前`;
if (diffHours >= 1 && diffHours < 2) return '1小时前';
return d.format('HH:mm');
}
else if (now.subtract(1, 'day').isSame(d, 'day')) {
return `昨天 ${d.format('HH:mm')}`;
}
else if (now.subtract(2, 'day').isSame(d, 'day')) {
return `前天 ${d.format('HH:mm')}`;
}
return d.year() === now.year()
? d.format('M月D日')
: d.format('YYYY/M/D');
} else {
if (settingStore.lang === 'en') {
if (isToday) {
if (diffSeconds < 60) return `Just now`;
@@ -52,6 +34,24 @@ export function fromNow(date) {
: d.format('YYYY/MM/DD');
} else {
if (isToday) {
if (diffSeconds < 60) return `几秒前`;
if (diffMinutes < 60) return `${diffMinutes}分钟前`;
if (diffHours >= 1 && diffHours < 2) return '1小时前';
return d.format('HH:mm');
}
else if (now.subtract(1, 'day').isSame(d, 'day')) {
return `昨天 ${d.format('HH:mm')}`;
}
else if (now.subtract(2, 'day').isSame(d, 'day')) {
return `前天 ${d.format('HH:mm')}`;
}
return d.year() === now.year()
? d.format('M月D日')
: d.format('YYYY/M/D');
}
}
@@ -63,12 +63,12 @@ export function formatDetailDate(time) {
const isSameYear = now.year() === d.year();
if (settingStore.lang === 'zh') {
return d.format('YYYY年M月D日 ddd AH:mm');
} else {
if (settingStore.lang === 'en') {
return isSameYear
? d.format('ddd, MMM D, h:mm A')
: d.format('ddd, MMM D, YYYY, h:mm A');
} else {
return d.format('YYYY年M月D日 ddd AH:mm');
}
}
@@ -78,4 +78,8 @@ export function tzDayjs(time) {
export function toUtc(time) {
return dayjs(time).utc()
}
}
export function setExtend(lang) {
dayjs.locale(lang)
}