AdGuardHome/internal/home/i18n.go
Ainar Garipov 756b14a61d Pull request: HOFTIX-csrf
Merge in DNS/adguard-home from HOFTIX-csrf to master

Squashed commit of the following:

commit 75ab27bf6c52b80ab4e7347d7c254fa659eac244
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Sep 29 18:45:54 2022 +0300

    all: imp cookie security; rm plain-text apis
2022-09-29 19:04:26 +03:00

95 lines
1.5 KiB
Go

package home
import (
"encoding/json"
"net/http"
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
"github.com/AdguardTeam/golibs/log"
"github.com/AdguardTeam/golibs/stringutil"
)
// TODO(a.garipov): Get rid of a global or generate from .twosky.json.
var allowedLanguages = stringutil.NewSet(
"ar",
"be",
"bg",
"cs",
"da",
"de",
"en",
"es",
"fa",
"fi",
"fr",
"hr",
"hu",
"id",
"it",
"ja",
"ko",
"nl",
"no",
"pl",
"pt-br",
"pt-pt",
"ro",
"ru",
"si-lk",
"sk",
"sl",
"sr-cs",
"sv",
"th",
"tr",
"uk",
"vi",
"zh-cn",
"zh-hk",
"zh-tw",
)
// languageJSON is the JSON structure for language requests and responses.
type languageJSON struct {
Language string `json:"language"`
}
func handleI18nCurrentLanguage(w http.ResponseWriter, r *http.Request) {
log.Printf("home: language is %s", config.Language)
_ = aghhttp.WriteJSONResponse(w, r, &languageJSON{
Language: config.Language,
})
}
func handleI18nChangeLanguage(w http.ResponseWriter, r *http.Request) {
if aghhttp.WriteTextPlainDeprecated(w, r) {
return
}
langReq := &languageJSON{}
err := json.NewDecoder(r.Body).Decode(langReq)
if err != nil {
aghhttp.Error(r, w, http.StatusInternalServerError, "reading req: %s", err)
return
}
lang := langReq.Language
if !allowedLanguages.Has(lang) {
aghhttp.Error(r, w, http.StatusBadRequest, "unknown language: %q", lang)
return
}
func() {
config.Lock()
defer config.Unlock()
config.Language = lang
log.Printf("home: language is set to %s", lang)
}()
onConfigModified()
aghhttp.OK(w)
}