Read the user language preferences from the HTTP request
Cette révision appartient à :
Parent
d3de3fb2ee
révision
b10cdde34a
2 fichiers modifiés avec 29 ajouts et 16 suppressions
|
@ -21,7 +21,7 @@ func UserRegisterFormHandler(w http.ResponseWriter, r *http.Request) {
|
|||
b := form.RegistrationForm{}
|
||||
modelHelper.BindValueForm(&b, r)
|
||||
b.CaptchaID = captcha.GetID()
|
||||
languages.SetTranslation("en-us", viewRegisterTemplate)
|
||||
languages.SetTranslationFromRequest(viewRegisterTemplate, r, "en-us")
|
||||
htv := UserRegisterTemplateVariables{b, form.NewErrors(), NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)}
|
||||
err := viewRegisterTemplate.ExecuteTemplate(w, "index.html", htv)
|
||||
if err != nil {
|
||||
|
@ -36,7 +36,7 @@ func UserRegisterFormHandler(w http.ResponseWriter, r *http.Request) {
|
|||
func UserLoginFormHandler(w http.ResponseWriter, r *http.Request) {
|
||||
b := form.LoginForm{}
|
||||
modelHelper.BindValueForm(&b, r)
|
||||
languages.SetTranslation("en-us", viewLoginTemplate)
|
||||
languages.SetTranslationFromRequest(viewLoginTemplate, r, "en-us")
|
||||
htv := UserLoginFormVariables{b, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)}
|
||||
err := viewLoginTemplate.ExecuteTemplate(w, "index.html", htv)
|
||||
if err != nil {
|
||||
|
@ -75,7 +75,7 @@ func UserRegisterPostHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
if (len(err) == 0) {
|
||||
b := form.RegistrationForm{}
|
||||
languages.SetTranslation("en-us", viewRegisterSuccessTemplate)
|
||||
languages.SetTranslationFromRequest(viewRegisterSuccessTemplate, r, "en-us")
|
||||
htv := UserRegisterTemplateVariables{b, err, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)}
|
||||
errorTmpl := viewRegisterSuccessTemplate.ExecuteTemplate(w, "index.html", htv)
|
||||
if errorTmpl != nil {
|
||||
|
@ -87,7 +87,7 @@ func UserRegisterPostHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
if (len(err) > 0) {
|
||||
b.CaptchaID = captcha.GetID()
|
||||
languages.SetTranslation("en-us", viewRegisterTemplate)
|
||||
languages.SetTranslationFromRequest(viewRegisterTemplate, r, "en-us")
|
||||
htv := UserRegisterTemplateVariables{b, err, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)}
|
||||
errorTmpl := viewRegisterTemplate.ExecuteTemplate(w, "index.html", htv)
|
||||
if errorTmpl != nil {
|
||||
|
@ -104,7 +104,7 @@ func UserVerifyEmailHandler(w http.ResponseWriter, r *http.Request) {
|
|||
if (errEmail != nil) {
|
||||
err["errors"] = append(err["errors"], errEmail.Error())
|
||||
}
|
||||
languages.SetTranslation("en-us", viewVerifySuccessTemplate)
|
||||
languages.SetTranslationFromRequest(viewVerifySuccessTemplate, r, "en-us")
|
||||
htv := UserVerifyTemplateVariables{err, NewSearchForm(), Navigation{}, r.URL, mux.CurrentRoute(r)}
|
||||
errorTmpl := viewVerifySuccessTemplate.ExecuteTemplate(w, "index.html", htv)
|
||||
if errorTmpl != nil {
|
||||
|
|
|
@ -1,15 +1,28 @@
|
|||
package languages
|
||||
|
||||
import (
|
||||
"github.com/nicksnyder/go-i18n/i18n"
|
||||
"html/template"
|
||||
)
|
||||
"fmt"
|
||||
"github.com/nicksnyder/go-i18n/i18n"
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func SetTranslation(language string, tmpl *template.Template) {
|
||||
T, _ := i18n.Tfunc(language)
|
||||
tmpl.Funcs(map[string]interface{}{
|
||||
"T": func (str string) template.HTML {
|
||||
return template.HTML(T(str))
|
||||
},
|
||||
})
|
||||
}
|
||||
func SetTranslation(tmpl *template.Template, language string, languages ...string) {
|
||||
T, _ := i18n.Tfunc(language, languages...)
|
||||
tmpl.Funcs(map[string]interface{}{
|
||||
"T": func(str string) template.HTML {
|
||||
return template.HTML(T(str))
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func SetTranslationFromRequest(tmpl *template.Template, r *http.Request, defaultLanguage string) {
|
||||
cookie, err := r.Cookie("lang")
|
||||
cookieLanguage := ""
|
||||
if err == nil {
|
||||
cookieLanguage = cookie.Value
|
||||
}
|
||||
// go-i18n supports the format of the Accept-Language header, thankfully.
|
||||
headerLanguage := r.Header.Get("Accept-Language")
|
||||
SetTranslation(tmpl, cookieLanguage, headerLanguage, defaultLanguage)
|
||||
}
|
||||
|
|
Référencer dans un nouveau ticket