|
@@ -5,9 +5,19 @@ import (
|
|
"errors"
|
|
"errors"
|
|
"net/http"
|
|
"net/http"
|
|
|
|
|
|
|
|
+ "github.com/nicksnyder/go-i18n/v2/i18n"
|
|
|
|
+ "golang.org/x/text/language"
|
|
"golang.org/x/text/message"
|
|
"golang.org/x/text/message"
|
|
|
|
+ "gopkg.in/yaml.v2"
|
|
)
|
|
)
|
|
|
|
|
|
|
|
+var Bundle = &i18n.Bundle{
|
|
|
|
+ DefaultLanguage: language.English,
|
|
|
|
+ UnmarshalFuncs: map[string]i18n.UnmarshalFunc{
|
|
|
|
+ "yaml": yaml.Unmarshal,
|
|
|
|
+ },
|
|
|
|
+}
|
|
|
|
+
|
|
/*
|
|
/*
|
|
WithPrinter() is a higher-order function injecting into the request context a
|
|
WithPrinter() is a higher-order function injecting into the request context a
|
|
printer for the language best matching the Accept-Language header in the incoming
|
|
printer for the language best matching the Accept-Language header in the incoming
|
|
@@ -18,9 +28,9 @@ configured for the best available language for the request.
|
|
*/
|
|
*/
|
|
func WithPrinter(h http.Handler) http.Handler {
|
|
func WithPrinter(h http.Handler) http.Handler {
|
|
h2 := func(w http.ResponseWriter, r *http.Request) {
|
|
h2 := func(w http.ResponseWriter, r *http.Request) {
|
|
- // Utilise le matcher de message.DefaultCatalog.
|
|
|
|
- tag := message.MatchLanguage(r.Header.Get("Accept-Language"))
|
|
|
|
- c := context.WithValue(r.Context(), "printer", message.NewPrinter(tag))
|
|
|
|
|
|
+ localizer := i18n.NewLocalizer(Bundle, r.Header.Get("Accept-Language"))
|
|
|
|
+
|
|
|
|
+ c := context.WithValue(r.Context(), "localizer", &localizer)
|
|
h.ServeHTTP(w, r.WithContext(c))
|
|
h.ServeHTTP(w, r.WithContext(c))
|
|
}
|
|
}
|
|
return http.HandlerFunc(h2)
|
|
return http.HandlerFunc(h2)
|
|
@@ -30,7 +40,7 @@ func WithPrinter(h http.Handler) http.Handler {
|
|
Printer() returns a message printer configured for the language best matching the
|
|
Printer() returns a message printer configured for the language best matching the
|
|
Accept-Language in the request, or panic if the handler invoking it was not
|
|
Accept-Language in the request, or panic if the handler invoking it was not
|
|
wrapped by a WithPrinter() call.
|
|
wrapped by a WithPrinter() call.
|
|
- */
|
|
|
|
|
|
+*/
|
|
func Printer(r *http.Request) *message.Printer {
|
|
func Printer(r *http.Request) *message.Printer {
|
|
p, ok := r.Context().Value("printer").(*message.Printer)
|
|
p, ok := r.Context().Value("printer").(*message.Printer)
|
|
if !ok {
|
|
if !ok {
|