time format, optimize i18n

This commit is contained in:
KUN1007 2023-06-08 23:19:32 +08:00
parent 3230802a40
commit c1446c024f
4 changed files with 30 additions and 14 deletions

View file

@ -1,14 +1,11 @@
import { createI18n } from 'vue-i18n' import { createI18n } from 'vue-i18n'
// 读取本地存储中的语言配置
import { lang } from '@/utils/localStorageLang'
// 引入语言文件 // 引入语言文件
import zh from './zh' import zh from './zh'
import en from './en' import en from './en'
// 读取本地存储中的语言配置
const localStorageString = localStorage.getItem('KUNGalgame-settings')
const lang = localStorageString
? JSON.parse(localStorageString).showKUNGalgameLanguage
: 'en'
const i18n = createI18n({ const i18n = createI18n({
locale: lang, locale: lang,
// 支持 Vue3 composition API // 支持 Vue3 composition API

View file

@ -1,23 +1,34 @@
import dayjs from 'dayjs' import dayjs from 'dayjs'
export const formatPublishTime = (time: number) => { // 读取本地存储中的语言配置
import { lang } from '@/utils/localStorageLang'
const language = lang === 'en' ? true : false
/* vue-i18n t setup
mins, years */
export const formatTime = (time: number) => {
const publishTime = dayjs(time) const publishTime = dayjs(time)
const now = dayjs() const now = dayjs()
const diffInSeconds = now.diff(publishTime, 'second') const diffInSeconds = now.diff(publishTime, 'second')
if (diffInSeconds < 60) { if (diffInSeconds < 60) {
return `${diffInSeconds}秒前` return language ? `${diffInSeconds}s ago` : `${diffInSeconds}秒前`
} else if (diffInSeconds < 3600) { } else if (diffInSeconds < 3600) {
const diffInMinutes = Math.floor(diffInSeconds / 60) const diffInMinutes = Math.floor(diffInSeconds / 60)
return `${diffInMinutes}分钟前` return language ? `${diffInMinutes}min ago` : `${diffInMinutes}分钟前`
} else if (diffInSeconds < 86400) { } else if (diffInSeconds < 86400) {
const diffInHours = Math.floor(diffInSeconds / 3600) const diffInHours = Math.floor(diffInSeconds / 3600)
return `${diffInHours}时前` return language ? `${diffInHours}h ago` : `${diffInHours}时前`
} else if (diffInSeconds < 2592000) { } else if (diffInSeconds < 2592000) {
const diffInDays = Math.floor(diffInSeconds / 86400) const diffInDays = Math.floor(diffInSeconds / 86400)
return `${diffInDays}天前` return language ? `${diffInDays}day ago` : `${diffInDays}天前`
} else { } else if (diffInSeconds < 31536000) {
const diffInMonths = Math.floor(diffInSeconds / 2592000) const diffInMonths = Math.floor(diffInSeconds / 2592000)
return `${diffInMonths}月前` return language ? `${diffInMonths}month ago` : `${diffInMonths}月前`
} else {
const diffInMonths = Math.floor(diffInSeconds / 31536000)
return language ? `${diffInMonths}year ago` : `${diffInMonths}年前`
} }
} }

View file

@ -0,0 +1,5 @@
// 读取本地存储中的语言配置
const localStorageString = localStorage.getItem('KUNGalgame-settings')
export const lang = localStorageString
? JSON.parse(localStorageString).showKUNGalgameLanguage
: 'en'

View file

@ -1,6 +1,9 @@
<script setup lang="ts"> <script setup lang="ts">
import { Icon } from '@iconify/vue' import { Icon } from '@iconify/vue'
// i18n
import { formatTime } from '@/utils/formatTime'
defineProps(['data']) defineProps(['data'])
</script> </script>
<template> <template>
@ -34,7 +37,7 @@ defineProps(['data'])
</div> </div>
<!-- 帖子的状态点赞数等 --> <!-- 帖子的状态点赞数等 -->
<div class="topic-post-date"> <div class="topic-post-date">
<span>10小时前</span> <span>{{ formatTime($props.data.topicPublishTime) }}</span>
</div> </div>
</div> </div>
<!-- 帖子的预览介绍 --> <!-- 帖子的预览介绍 -->