2021-09-23 11:08:19 +08:00
|
|
|
;(() => {
|
|
|
|
const supportedLangs = window.supportedLangs
|
2021-09-26 16:34:15 +08:00
|
|
|
const cacheKey = 'preferred_lang'
|
|
|
|
const defaultLang = 'en-US'
|
|
|
|
// docs supported languages
|
|
|
|
const langAlias = {
|
|
|
|
en: 'en-US',
|
|
|
|
fr: 'fr-FR',
|
|
|
|
es: 'es-ES',
|
2021-09-24 01:19:18 +08:00
|
|
|
}
|
2021-09-26 16:34:15 +08:00
|
|
|
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}`)) {
|
2021-09-24 01:19:18 +08:00
|
|
|
const toPath = [`/${userPreferredLang}`]
|
2021-09-23 11:08:19 +08:00
|
|
|
.concat(location.pathname.split('/').slice(2))
|
|
|
|
.join('/')
|
2021-09-26 16:34:15 +08:00
|
|
|
location.pathname =
|
|
|
|
toPath.endsWith('.html') || toPath.endsWith('/')
|
|
|
|
? toPath
|
|
|
|
: toPath.concat('/')
|
2021-09-23 11:08:19 +08:00
|
|
|
}
|
2022-05-12 22:48:38 +08:00
|
|
|
if (navigator && navigator.serviceWorker.controller) {
|
|
|
|
navigator.serviceWorker.controller.postMessage({
|
|
|
|
type: 'LANG',
|
|
|
|
lang: userPreferredLang,
|
|
|
|
})
|
|
|
|
}
|
2021-09-23 11:08:19 +08:00
|
|
|
})()
|