feat(theme): find best locale match to format last update

userquin/feat-use-best-lang-match-for-last-update
userquin 10 months ago
parent d200f4e125
commit 8d9b59a3bd

@ -13,9 +13,27 @@ const datetime = ref('')
// 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(() => {
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(() => { watchEffect(() => {
datetime.value = new Intl.DateTimeFormat( 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 ?? { theme.value.lastUpdated?.formatOptions ?? {
dateStyle: 'short', dateStyle: 'short',
timeStyle: 'short' timeStyle: 'short'

Loading…
Cancel
Save