element-plus/docs/.vitepress/lang.js
DDDDD12138 ba59b5d20e
docs: improve language matching logic (#18412)
fix(docs): improve language matching logic
2024-10-12 10:14:39 +08:00

32 lines
1.0 KiB
JavaScript

;(() => {
const supportedLangs = window.supportedLangs
const cacheKey = 'preferred_lang'
const defaultLang = 'en-US'
const handleNavigatorLang = (navLang) => {
const { language, region } = new Intl.Locale(navLang).maximize()
return `${language}-${region}`
}
let userPreferredLang =
localStorage.getItem(cacheKey) || handleNavigatorLang(navigator.language)
const language = supportedLangs.includes(userPreferredLang)
? userPreferredLang
: defaultLang
localStorage.setItem(cacheKey, language)
userPreferredLang = language
if (!location.pathname.startsWith(`/${userPreferredLang}`)) {
const toPath = [`/${userPreferredLang}`]
.concat(location.pathname.split('/').slice(2))
.join('/')
location.pathname =
toPath.endsWith('.html') || toPath.endsWith('/')
? toPath
: toPath.concat('/')
}
if (navigator && navigator.serviceWorker.controller) {
navigator.serviceWorker.controller.postMessage({
type: 'LANG',
lang: userPreferredLang,
})
}
})()