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