mirror of
https://github.com/element-plus/element-plus.git
synced 2024-11-27 02:01:15 +08:00
ba59b5d20e
fix(docs): improve language matching logic
32 lines
1.0 KiB
JavaScript
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,
|
|
})
|
|
}
|
|
})()
|