feat(i18n): add English and Simplified Chinese
This commit is contained in:
1 parent
38c4c8b781
commit
1e9e0678ee
149 files changed
+5356
-1144
No files matched your search
@@ -0,0 +1,36 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"rawmark/internal/i18n"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func localized(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasPrefix(r.URL.Path, "/api/") {
|
||||
w.Header().Set("Content-Language", string(i18n.Negotiate(r.Header.Get("Accept-Language"))))
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
func responseLocale(w http.ResponseWriter) i18n.Locale {
|
||||
return i18n.Negotiate(w.Header().Get("Content-Language"))
|
||||
}
|
||||
func message(w http.ResponseWriter, key string, args ...any) string {
|
||||
return i18n.Text(responseLocale(w), key, args...)
|
||||
}
|
||||
func validateLocale(settings map[string]json.RawMessage) error {
|
||||
if raw, ok := settings["locale"]; ok {
|
||||
if bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
|
||||
return nil
|
||||
}
|
||||
var locale string
|
||||
if err := json.Unmarshal(raw, &locale); err != nil || (locale != "en" && locale != "zh-CN") {
|
||||
return i18n.New("invalid_locale")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user