2019-06-10 01:33:19 -07:00
|
|
|
package home
|
2018-11-21 10:42:55 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2018-12-29 07:23:42 -07:00
|
|
|
|
2019-02-25 06:44:22 -07:00
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2018-11-21 10:42:55 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// --------------------
|
|
|
|
// internationalization
|
|
|
|
// --------------------
|
|
|
|
var allowedLanguages = map[string]bool{
|
2020-09-16 07:42:57 -07:00
|
|
|
"be": true,
|
2019-03-07 02:38:17 -07:00
|
|
|
"bg": true,
|
2019-10-03 08:41:48 -07:00
|
|
|
"cs": true,
|
|
|
|
"da": true,
|
|
|
|
"de": true,
|
2020-09-16 07:42:57 -07:00
|
|
|
"en": true,
|
|
|
|
"es": true,
|
|
|
|
"fa": true,
|
|
|
|
"fr": true,
|
|
|
|
"hr": true,
|
|
|
|
"hu": true,
|
2019-10-03 08:41:48 -07:00
|
|
|
"id": true,
|
|
|
|
"it": true,
|
2020-09-16 07:42:57 -07:00
|
|
|
"ja": true,
|
2019-10-03 08:41:48 -07:00
|
|
|
"ko": true,
|
|
|
|
"nl": true,
|
2020-09-16 07:42:57 -07:00
|
|
|
"no": true,
|
2019-10-03 08:41:48 -07:00
|
|
|
"pl": true,
|
2020-09-16 07:42:57 -07:00
|
|
|
"pt-br": true,
|
2019-10-03 08:41:48 -07:00
|
|
|
"pt-pt": true,
|
2020-09-16 07:42:57 -07:00
|
|
|
"ro": true,
|
|
|
|
"ru": true,
|
|
|
|
"si-lk": true,
|
2019-10-03 08:41:48 -07:00
|
|
|
"sk": true,
|
|
|
|
"sl": true,
|
2019-12-12 08:42:51 -07:00
|
|
|
"sr-cs": true,
|
2020-09-16 07:42:57 -07:00
|
|
|
"sv": true,
|
2020-03-11 07:06:37 -07:00
|
|
|
"th": true,
|
2020-09-16 07:42:57 -07:00
|
|
|
"tr": true,
|
|
|
|
"vi": true,
|
|
|
|
"zh-cn": true,
|
|
|
|
"zh-hk": true,
|
|
|
|
"zh-tw": true,
|
2018-11-21 10:42:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func isLanguageAllowed(language string) bool {
|
|
|
|
l := strings.ToLower(language)
|
2018-11-28 03:37:42 -07:00
|
|
|
return allowedLanguages[l]
|
2018-11-21 10:42:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleI18nCurrentLanguage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
2018-11-22 07:56:57 -07:00
|
|
|
log.Printf("config.Language is %s", config.Language)
|
|
|
|
_, err := fmt.Fprintf(w, "%s\n", config.Language)
|
2018-11-21 10:42:55 -07:00
|
|
|
if err != nil {
|
2020-11-06 02:15:08 -07:00
|
|
|
msg := fmt.Sprintf("Unable to write response json: %s", err)
|
|
|
|
log.Println(msg)
|
|
|
|
http.Error(w, msg, http.StatusInternalServerError)
|
2018-11-21 10:42:55 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleI18nChangeLanguage(w http.ResponseWriter, r *http.Request) {
|
2020-11-23 04:14:08 -07:00
|
|
|
// This use of ReadAll is safe, because request's body is now limited.
|
2018-11-21 10:42:55 -07:00
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2020-11-06 02:15:08 -07:00
|
|
|
msg := fmt.Sprintf("failed to read request body: %s", err)
|
|
|
|
log.Println(msg)
|
|
|
|
http.Error(w, msg, http.StatusBadRequest)
|
2018-11-21 10:42:55 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
language := strings.TrimSpace(string(body))
|
|
|
|
if language == "" {
|
2020-11-06 02:15:08 -07:00
|
|
|
msg := "empty language specified"
|
|
|
|
log.Println(msg)
|
|
|
|
http.Error(w, msg, http.StatusBadRequest)
|
2018-11-21 10:42:55 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !isLanguageAllowed(language) {
|
2020-11-06 02:15:08 -07:00
|
|
|
msg := fmt.Sprintf("unknown language specified: %s", language)
|
|
|
|
log.Println(msg)
|
|
|
|
http.Error(w, msg, http.StatusBadRequest)
|
2018-11-21 10:42:55 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-22 03:08:37 -07:00
|
|
|
config.Language = language
|
2019-10-30 01:52:58 -07:00
|
|
|
onConfigModified()
|
|
|
|
returnOK(w)
|
2018-11-21 10:42:55 -07:00
|
|
|
}
|