mirror of
https://gitee.com/element-plus/element-plus.git
synced 2024-12-02 11:17:46 +08:00
0c92e8a8d2
* docs: cache the preferred lang pages in the PWA * chore: remove index.html in manifest * fix: no cache the '/' page * perf: optimize code * docs: add always refresh from PWA
35 lines
1018 B
JavaScript
35 lines
1018 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('/')
|
|
}
|
|
if (navigator && navigator.serviceWorker.controller) {
|
|
navigator.serviceWorker.controller.postMessage({
|
|
type: 'LANG',
|
|
lang: userPreferredLang,
|
|
})
|
|
}
|
|
})()
|