From 8d9b59a3bd9f06bde565402f7c720fe17d321c52 Mon Sep 17 00:00:00 2001 From: userquin Date: Tue, 12 Nov 2024 14:57:21 +0100 Subject: [PATCH] feat(theme): find best locale match to format last update --- .../components/VPDocFooterLastUpdated.vue | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/client/theme-default/components/VPDocFooterLastUpdated.vue b/src/client/theme-default/components/VPDocFooterLastUpdated.vue index 5ec3cfeb..a4e6da61 100644 --- a/src/client/theme-default/components/VPDocFooterLastUpdated.vue +++ b/src/client/theme-default/components/VPDocFooterLastUpdated.vue @@ -13,9 +13,27 @@ const datetime = ref('') // set time on mounted hook to avoid hydration mismatch due to // potential differences in timezones of the server and clients onMounted(() => { + function findBestLocaleMatch(pageLocale: string): string | undefined { + return navigator.languages.find((userLang) => { + if (pageLocale === userLang) + return pageLocale + + // Edge browser: case for ca-valencia + if (pageLocale === 'ca-valencia' && userLang === 'ca-Es-VALENCIA') + return pageLocale + + // add iso-639 support for Latin America + if (userLang.startsWith('es-') && userLang !== 'es-ES' && pageLocale === 'es-419') + return pageLocale + + return userLang.startsWith(pageLocale) ? pageLocale : undefined + }) + } watchEffect(() => { datetime.value = new Intl.DateTimeFormat( - theme.value.lastUpdated?.formatOptions?.forceLocale ? lang.value : undefined, + theme.value.lastUpdated?.formatOptions?.forceLocale + ? lang.value + : findBestLocaleMatch(lang.value), theme.value.lastUpdated?.formatOptions ?? { dateStyle: 'short', timeStyle: 'short'