mirror of
https://github.com/Luzifer/ots.git
synced 2025-04-19 06:55:51 -04:00
Move language detection to frontend to make frontend cachable
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
ec5728b1c3
commit
8bf7a72f54
38
main.go
38
main.go
@ -102,38 +102,14 @@ func handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if err = tpl.Execute(w, struct{ Vars map[string]string }{Vars: getJSVars(r)}); err != nil {
|
||||
if err = tpl.Execute(w, struct {
|
||||
Vars map[string]string
|
||||
}{
|
||||
Vars: map[string]string{
|
||||
"version": version,
|
||||
},
|
||||
}); err != nil {
|
||||
http.Error(w, errors.Wrap(err, "parsing template").Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func getJSVars(r *http.Request) map[string]string {
|
||||
cookie, _ := r.Cookie("lang")
|
||||
|
||||
cookieLang := ""
|
||||
if cookie != nil {
|
||||
cookieLang = cookie.Value
|
||||
}
|
||||
acceptLang := r.Header.Get("Accept-Language")
|
||||
defaultLang := "en" // known valid language
|
||||
|
||||
vars := map[string]string{
|
||||
"version": version,
|
||||
}
|
||||
|
||||
switch {
|
||||
case cookieLang != "":
|
||||
vars["locale"] = normalizeLang(cookieLang)
|
||||
case acceptLang != "":
|
||||
vars["locale"] = normalizeLang(strings.Split(acceptLang, ",")[0])
|
||||
default:
|
||||
vars["locale"] = defaultLang
|
||||
}
|
||||
|
||||
return vars
|
||||
}
|
||||
|
||||
func normalizeLang(lang string) string {
|
||||
return strings.ToLower(strings.Split(lang, "-")[0])
|
||||
}
|
||||
|
@ -13,8 +13,12 @@ import messages from './langs/langs.js'
|
||||
Vue.use(BootstrapVue)
|
||||
Vue.use(VueI18n)
|
||||
|
||||
const cookieSet = Object.fromEntries(document.cookie.split('; ')
|
||||
.map(el => el.split('=')
|
||||
.map(el => decodeURIComponent(el))))
|
||||
|
||||
const i18n = new VueI18n({
|
||||
locale,
|
||||
locale: cookieSet.lang?.split(/[_-]/)[0] || navigator?.language?.split(/[_-]/)[0] || 'en',
|
||||
fallbackLocale: 'en',
|
||||
messages,
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user