Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Merge pull request #165 from ElegantMonkey/read-language-from-request

Read the user language from the HTTP request
Cette révision appartient à :
Eliot Whalan 2017-05-08 07:18:22 +10:00 révisé par GitHub
révision 74d27b0086
2 fichiers modifiés avec 28 ajouts et 16 suppressions

Voir le fichier

@ -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 {

Voir le fichier

@ -1,15 +1,27 @@
package languages
import (
"github.com/nicksnyder/go-i18n/i18n"
"html/template"
)
"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)
}