mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-26 06:55:48 +03:00
14fd995ae9
Merge in DNS/adguard-home from add-ar-i18n to master Squashed commit of the following: commit 6ef7c70bceb6f6ebabd81011154022a75fc91bd3 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Wed Aug 10 20:55:39 2022 +0300 client: add ar locale
104 lines
1.8 KiB
Go
104 lines
1.8 KiB
Go
package home
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"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",
|
|
)
|
|
|
|
func handleI18nCurrentLanguage(w http.ResponseWriter, _ *http.Request) {
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
log.Printf("config.Language is %s", config.Language)
|
|
_, err := fmt.Fprintf(w, "%s\n", config.Language)
|
|
if err != nil {
|
|
msg := fmt.Sprintf("Unable to write response json: %s", err)
|
|
log.Println(msg)
|
|
http.Error(w, msg, http.StatusInternalServerError)
|
|
|
|
return
|
|
}
|
|
}
|
|
|
|
func handleI18nChangeLanguage(w http.ResponseWriter, r *http.Request) {
|
|
// This use of ReadAll is safe, because request's body is now limited.
|
|
body, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
msg := fmt.Sprintf("failed to read request body: %s", err)
|
|
log.Println(msg)
|
|
http.Error(w, msg, http.StatusBadRequest)
|
|
|
|
return
|
|
}
|
|
|
|
language := strings.TrimSpace(string(body))
|
|
if language == "" {
|
|
msg := "empty language specified"
|
|
log.Println(msg)
|
|
http.Error(w, msg, http.StatusBadRequest)
|
|
|
|
return
|
|
}
|
|
|
|
if !allowedLanguages.Has(language) {
|
|
msg := fmt.Sprintf("unknown language specified: %s", language)
|
|
log.Println(msg)
|
|
http.Error(w, msg, http.StatusBadRequest)
|
|
|
|
return
|
|
}
|
|
|
|
func() {
|
|
config.Lock()
|
|
defer config.Unlock()
|
|
|
|
config.Language = language
|
|
}()
|
|
|
|
onConfigModified()
|
|
aghhttp.OK(w)
|
|
}
|