2019-06-10 11:33:19 +03:00
|
|
|
package home
|
2018-11-21 20:42:55 +03:00
|
|
|
|
|
|
|
import (
|
2022-09-29 19:04:26 +03:00
|
|
|
"encoding/json"
|
2018-11-21 20:42:55 +03:00
|
|
|
"net/http"
|
2018-12-29 17:23:42 +03:00
|
|
|
|
2021-12-16 20:54:59 +03:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
2019-02-25 16:44:22 +03:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2021-07-29 17:40:31 +03:00
|
|
|
"github.com/AdguardTeam/golibs/stringutil"
|
2018-11-21 20:42:55 +03:00
|
|
|
)
|
|
|
|
|
2021-11-15 14:35:46 +03:00
|
|
|
// TODO(a.garipov): Get rid of a global or generate from .twosky.json.
|
2021-07-29 17:40:31 +03:00
|
|
|
var allowedLanguages = stringutil.NewSet(
|
2022-08-10 21:03:13 +03:00
|
|
|
"ar",
|
2021-04-20 16:26:19 +03:00
|
|
|
"be",
|
|
|
|
"bg",
|
|
|
|
"cs",
|
|
|
|
"da",
|
|
|
|
"de",
|
|
|
|
"en",
|
|
|
|
"es",
|
|
|
|
"fa",
|
2021-11-15 14:35:46 +03:00
|
|
|
"fi",
|
2021-04-20 16:26:19 +03:00
|
|
|
"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",
|
2021-11-15 14:35:46 +03:00
|
|
|
"uk",
|
2021-04-20 16:26:19 +03:00
|
|
|
"vi",
|
|
|
|
"zh-cn",
|
|
|
|
"zh-hk",
|
|
|
|
"zh-tw",
|
|
|
|
)
|
2018-11-21 20:42:55 +03:00
|
|
|
|
2022-09-29 19:04:26 +03:00
|
|
|
// languageJSON is the JSON structure for language requests and responses.
|
|
|
|
type languageJSON struct {
|
|
|
|
Language string `json:"language"`
|
|
|
|
}
|
2022-08-10 21:03:13 +03:00
|
|
|
|
2023-01-13 19:24:17 +03:00
|
|
|
// TODO(d.kolyshev): Deprecated, remove it later.
|
2022-09-29 19:04:26 +03:00
|
|
|
func handleI18nCurrentLanguage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
log.Printf("home: language is %s", config.Language)
|
|
|
|
|
|
|
|
_ = aghhttp.WriteJSONResponse(w, r, &languageJSON{
|
|
|
|
Language: config.Language,
|
|
|
|
})
|
2018-11-21 20:42:55 +03:00
|
|
|
}
|
|
|
|
|
2023-01-13 19:24:17 +03:00
|
|
|
// TODO(d.kolyshev): Deprecated, remove it later.
|
2018-11-21 20:42:55 +03:00
|
|
|
func handleI18nChangeLanguage(w http.ResponseWriter, r *http.Request) {
|
2022-09-29 19:04:26 +03:00
|
|
|
if aghhttp.WriteTextPlainDeprecated(w, r) {
|
2018-11-21 20:42:55 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-29 19:04:26 +03:00
|
|
|
langReq := &languageJSON{}
|
|
|
|
err := json.NewDecoder(r.Body).Decode(langReq)
|
|
|
|
if err != nil {
|
|
|
|
aghhttp.Error(r, w, http.StatusInternalServerError, "reading req: %s", err)
|
2021-04-20 16:26:19 +03:00
|
|
|
|
2018-11-21 20:42:55 +03:00
|
|
|
return
|
|
|
|
}
|
2021-04-20 16:26:19 +03:00
|
|
|
|
2022-09-29 19:04:26 +03:00
|
|
|
lang := langReq.Language
|
|
|
|
if !allowedLanguages.Has(lang) {
|
|
|
|
aghhttp.Error(r, w, http.StatusBadRequest, "unknown language: %q", lang)
|
2021-04-20 16:26:19 +03:00
|
|
|
|
2018-11-21 20:42:55 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-17 16:50:02 +03:00
|
|
|
func() {
|
|
|
|
config.Lock()
|
|
|
|
defer config.Unlock()
|
|
|
|
|
2022-09-29 19:04:26 +03:00
|
|
|
config.Language = lang
|
|
|
|
log.Printf("home: language is set to %s", lang)
|
2021-05-17 16:50:02 +03:00
|
|
|
}()
|
|
|
|
|
2019-10-30 11:52:58 +03:00
|
|
|
onConfigModified()
|
2021-12-16 20:54:59 +03:00
|
|
|
aghhttp.OK(w)
|
2018-11-21 20:42:55 +03:00
|
|
|
}
|