AdGuardHome/home/i18n.go
Simon Zolin aa202277fb Merge: * update translations
Close #1250

Squashed commit of the following:

commit e722a3e3b4b1049c5df22b3f11b5826992d3cc6c
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Thu Dec 12 18:27:51 2019 +0300

    update ru

commit 086e722a4dc1922ac7b8aff537050689900a8bd0
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Thu Dec 12 18:15:15 2019 +0300

    fix cloudflare -> quad9, update es

commit 86c235936d2f86e536f45fc475f29b43eb695e3d
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Thu Dec 12 18:01:20 2019 +0300

    * update es, ru, sr-cs

commit 2b6b510b998b7e7b2f4c3fa5d7af57dc5413a611
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Thu Dec 12 17:43:05 2019 +0300

    * update 'no'

commit 1de6215aa90374ee147bbd0a685643232e98e4c8
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Thu Dec 12 17:41:37 2019 +0300

    * use dnsproxy v0.23.2

commit c545a1bf7e4e706b45b4670b804a50bf8d75bae3
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Thu Dec 12 17:36:37 2019 +0300

    update ru

commit c22eec9bcfad67284e21cfc4e6b06fb63a64d25d
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Thu Dec 12 17:22:03 2019 +0300

    * update translations;  add sr-cs, fa, hr
2019-12-12 18:42:51 +03:00

88 lines
1.9 KiB
Go

package home
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/AdguardTeam/golibs/log"
)
// --------------------
// internationalization
// --------------------
var allowedLanguages = map[string]bool{
"en": true,
"ru": true,
"vi": true,
"es": true,
"fr": true,
"ja": true,
"sv": true,
"pt-br": true,
"zh-tw": true,
"bg": true,
"zh-cn": true,
"cs": true,
"da": true,
"de": true,
"id": true,
"it": true,
"ko": true,
"no": true,
"nl": true,
"pl": true,
"pt-pt": true,
"sk": true,
"sl": true,
"tr": true,
"sr-cs": true,
"hr": true,
"fa": true,
}
func isLanguageAllowed(language string) bool {
l := strings.ToLower(language)
return allowedLanguages[l]
}
func handleI18nCurrentLanguage(w http.ResponseWriter, r *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 {
errorText := fmt.Sprintf("Unable to write response json: %s", err)
log.Println(errorText)
http.Error(w, errorText, http.StatusInternalServerError)
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
}
config.Language = language
onConfigModified()
returnOK(w)
}