AdGuardHome/internal/home/i18n.go

95 lines
1.9 KiB
Go
Raw Normal View History

package home
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/AdguardTeam/golibs/log"
)
// --------------------
// internationalization
// --------------------
var allowedLanguages = map[string]bool{
2020-09-16 17:42:57 +03:00
"be": true,
"bg": true,
2019-10-03 18:41:48 +03:00
"cs": true,
"da": true,
"de": true,
2020-09-16 17:42:57 +03:00
"en": true,
"es": true,
"fa": true,
"fr": true,
"hr": true,
"hu": true,
2019-10-03 18:41:48 +03:00
"id": true,
"it": true,
2020-09-16 17:42:57 +03:00
"ja": true,
2019-10-03 18:41:48 +03:00
"ko": true,
"nl": true,
2020-09-16 17:42:57 +03:00
"no": true,
2019-10-03 18:41:48 +03:00
"pl": true,
2020-09-16 17:42:57 +03:00
"pt-br": true,
2019-10-03 18:41:48 +03:00
"pt-pt": true,
2020-09-16 17:42:57 +03:00
"ro": true,
"ru": true,
"si-lk": true,
2019-10-03 18:41:48 +03:00
"sk": true,
"sl": true,
"sr-cs": true,
2020-09-16 17:42:57 +03:00
"sv": true,
"th": true,
2020-09-16 17:42:57 +03:00
"tr": true,
"vi": true,
"zh-cn": true,
"zh-hk": true,
"zh-tw": true,
}
func isLanguageAllowed(language string) bool {
l := strings.ToLower(language)
2018-11-28 13:37:42 +03:00
return allowedLanguages[l]
}
func handleI18nCurrentLanguage(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
2018-11-22 17:56:57 +03:00
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) {
body, err := ioutil.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 !isLanguageAllowed(language) {
msg := fmt.Sprintf("unknown language specified: %s", language)
log.Println(msg)
http.Error(w, msg, http.StatusBadRequest)
return
}
config.Language = language
onConfigModified()
returnOK(w)
}