feat(i18n,a11y): change last update logic

pull/4935/head
userquin 2 weeks ago
parent c2eaccd0d2
commit ca096c9d1b

@ -1,8 +1,12 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed, watchEffect, onMounted } from 'vue' import { ref, computed, watchEffect, onMounted, useTemplateRef } from 'vue'
import { useData } from '../composables/data' import { useData } from '../composables/data'
import { useNavigatorLanguage } from '@vueuse/core'
const { theme, page, lang } = useData() const { theme, page, lang } = useData()
const { language } = useNavigatorLanguage()
const timeRef = useTemplateRef('timeRef')
const date = computed( const date = computed(
() => new Date(page.value.lastUpdated!) () => new Date(page.value.lastUpdated!)
@ -14,13 +18,20 @@ const datetime = ref('')
// potential differences in timezones of the server and clients // potential differences in timezones of the server and clients
onMounted(() => { onMounted(() => {
watchEffect(() => { watchEffect(() => {
const browserLang = theme.value.lastUpdated?.formatOptions?.forceLocale ? lang.value : language.value
datetime.value = new Intl.DateTimeFormat( datetime.value = new Intl.DateTimeFormat(
theme.value.lastUpdated?.formatOptions?.forceLocale ? lang.value : undefined, browserLang,
theme.value.lastUpdated?.formatOptions ?? { theme.value.lastUpdated?.formatOptions ?? {
dateStyle: 'short', dateStyle: 'medium',
timeStyle: 'short' timeStyle: 'medium'
} }
).format(date.value) ).format(date.value)
if (browserLang && lang.value !== browserLang) {
timeRef.value?.setAttribute('lang', browserLang)
}
else {
timeRef.value?.removeAttribute('lang')
}
}) })
}) })
</script> </script>
@ -28,7 +39,7 @@ onMounted(() => {
<template> <template>
<p class="VPLastUpdated"> <p class="VPLastUpdated">
{{ theme.lastUpdated?.text || theme.lastUpdatedText || 'Last updated' }}: {{ theme.lastUpdated?.text || theme.lastUpdatedText || 'Last updated' }}:
<time :datetime="isoDatetime">{{ datetime }}</time> <time ref="timeRef" :datetime="isoDatetime">{{ datetime }}</time>
</p> </p>
</template> </template>

Loading…
Cancel
Save