-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
i18n.go
97 lines (83 loc) · 1.63 KB
/
i18n.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package home
import (
"encoding/json"
"net/http"
"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",
)
// languageJSON is the JSON structure for language requests and responses.
type languageJSON struct {
Language string `json:"language"`
}
// TODO(d.kolyshev): Deprecated, remove it later.
func handleI18nCurrentLanguage(w http.ResponseWriter, r *http.Request) {
log.Printf("home: language is %s", config.Language)
_ = aghhttp.WriteJSONResponse(w, r, &languageJSON{
Language: config.Language,
})
}
// TODO(d.kolyshev): Deprecated, remove it later.
func handleI18nChangeLanguage(w http.ResponseWriter, r *http.Request) {
if aghhttp.WriteTextPlainDeprecated(w, r) {
return
}
langReq := &languageJSON{}
err := json.NewDecoder(r.Body).Decode(langReq)
if err != nil {
aghhttp.Error(r, w, http.StatusInternalServerError, "reading req: %s", err)
return
}
lang := langReq.Language
if !allowedLanguages.Has(lang) {
aghhttp.Error(r, w, http.StatusBadRequest, "unknown language: %q", lang)
return
}
func() {
config.Lock()
defer config.Unlock()
config.Language = lang
log.Printf("home: language is set to %s", lang)
}()
onConfigModified()
aghhttp.OK(w)
}