2018-11-21 10:42:55 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
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{
|
2018-11-26 05:00:56 -07:00
|
|
|
"en": true,
|
|
|
|
"ru": true,
|
|
|
|
"vi": true,
|
|
|
|
"es": true,
|
|
|
|
"fr": true,
|
|
|
|
"ja": true,
|
|
|
|
"sv": true,
|
|
|
|
"pt-br": true,
|
2019-01-03 02:55:15 -07:00
|
|
|
"zh-tw": true,
|
2019-03-07 02:38:17 -07:00
|
|
|
"bg": true,
|
2019-03-12 01:02:19 -07:00
|
|
|
"zh-cn": 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 {
|
2019-01-24 10:11:01 -07:00
|
|
|
errorText := fmt.Sprintf("Unable to write response json: %s", err)
|
|
|
|
log.Println(errorText)
|
|
|
|
http.Error(w, errorText, http.StatusInternalServerError)
|
2018-11-21 10:42:55 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleI18nChangeLanguage(w http.ResponseWriter, r *http.Request) {
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
errorText := fmt.Sprintf("failed to read request body: %s", err)
|
|
|
|
log.Println(errorText)
|
|
|
|
http.Error(w, errorText, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
language := strings.TrimSpace(string(body))
|
|
|
|
if language == "" {
|
|
|
|
errorText := fmt.Sprintf("empty language specified")
|
|
|
|
log.Println(errorText)
|
|
|
|
http.Error(w, errorText, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !isLanguageAllowed(language) {
|
|
|
|
errorText := fmt.Sprintf("unknown language specified: %s", language)
|
|
|
|
log.Println(errorText)
|
|
|
|
http.Error(w, errorText, http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-11-22 03:08:37 -07:00
|
|
|
config.Language = language
|
|
|
|
|
2018-11-21 10:42:55 -07:00
|
|
|
httpUpdateConfigReloadDNSReturnOK(w, r)
|
|
|
|
}
|