mirror of
https://gitee.com/element-plus/element-plus.git
synced 2024-12-14 17:31:02 +08:00
29 lines
846 B
JavaScript
29 lines
846 B
JavaScript
;(() => {
|
|
const supportedLangs = window.supportedLangs
|
|
const cacheKey = 'preferred_lang'
|
|
const defaultLang = 'en-US'
|
|
// docs supported languages
|
|
const langAlias = {
|
|
en: 'en-US',
|
|
fr: 'fr-FR',
|
|
es: 'es-ES',
|
|
}
|
|
let userPreferredLang = localStorage.getItem(cacheKey) || navigator.language
|
|
const language =
|
|
langAlias[userPreferredLang] ||
|
|
(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('/')
|
|
}
|
|
})()
|