feat(i18n,a11y): change last update logic (#4935)

---------

Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com>
pull/4735/merge
Joaquín Sánchez 2 weeks ago committed by GitHub
parent c2eaccd0d2
commit 187bf250e6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,26 +1,38 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, computed, watchEffect, onMounted } from 'vue' import { useNavigatorLanguage } from '@vueuse/core'
import { computed, onMounted, shallowRef, useTemplateRef, watchEffect } from 'vue'
import { useData } from '../composables/data' import { useData } from '../composables/data'
const { theme, page, lang } = useData() const { theme, page, lang: pageLang } = useData()
const { language: browserLang } = useNavigatorLanguage()
const date = computed( const timeRef = useTemplateRef('timeRef')
() => new Date(page.value.lastUpdated!)
) const date = computed(() => new Date(page.value.lastUpdated!))
const isoDatetime = computed(() => date.value.toISOString()) const isoDatetime = computed(() => date.value.toISOString())
const datetime = ref('') const datetime = shallowRef('')
// set time on mounted hook to avoid hydration mismatch due to // set time on mounted hook to avoid hydration mismatch due to
// potential differences in timezones of the server and clients // potential differences in timezones of the server and clients
onMounted(() => { onMounted(() => {
watchEffect(() => { watchEffect(() => {
const lang = theme.value.lastUpdated?.formatOptions?.forceLocale
? pageLang.value
: browserLang.value
datetime.value = new Intl.DateTimeFormat( datetime.value = new Intl.DateTimeFormat(
theme.value.lastUpdated?.formatOptions?.forceLocale ? lang.value : undefined, lang,
theme.value.lastUpdated?.formatOptions ?? { theme.value.lastUpdated?.formatOptions ?? {
dateStyle: 'short', dateStyle: 'medium',
timeStyle: 'short' timeStyle: 'medium'
} }
).format(date.value) ).format(date.value)
if (lang && pageLang.value !== lang) {
timeRef.value?.setAttribute('lang', lang)
} else {
timeRef.value?.removeAttribute('lang')
}
}) })
}) })
</script> </script>
@ -28,7 +40,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