Add language selector (#298)
Cette révision appartient à :
Parent
755a426931
révision
c84140964b
4 fichiers modifiés avec 34 ajouts et 4 suppressions
|
@ -59,6 +59,7 @@ type UserProfileEditVariables struct {
|
|||
UserForm userForms.UserForm
|
||||
FormErrors map[string][]string
|
||||
FormInfos map[string][]string
|
||||
Languages map[string]string
|
||||
Search SearchForm
|
||||
Navigation Navigation
|
||||
User *model.User
|
||||
|
|
|
@ -115,7 +115,8 @@ func UserDetailsHandler(w http.ResponseWriter, r *http.Request) {
|
|||
languages.SetTranslationFromRequest(viewProfileEditTemplate, r, "en-us")
|
||||
searchForm := NewSearchForm()
|
||||
searchForm.HideAdvancedSearch = true
|
||||
htv := UserProfileEditVariables{&userProfile, b, form.NewErrors(), form.NewInfos(), searchForm, Navigation{}, currentUser, r.URL, mux.CurrentRoute(r)}
|
||||
availableLanguages := languages.GetAvailableLanguages()
|
||||
htv := UserProfileEditVariables{&userProfile, b, form.NewErrors(), form.NewInfos(), availableLanguages, searchForm, Navigation{}, currentUser, r.URL, mux.CurrentRoute(r)}
|
||||
err := viewProfileEditTemplate.ExecuteTemplate(w, "index.html", htv)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
|
@ -158,7 +159,8 @@ func UserProfileFormHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
}
|
||||
}
|
||||
htv := UserProfileEditVariables{&userProfile, b, err, infos, NewSearchForm(), Navigation{}, currentUser, r.URL, mux.CurrentRoute(r)}
|
||||
availableLanguages := languages.GetAvailableLanguages()
|
||||
htv := UserProfileEditVariables{&userProfile, b, err, infos, availableLanguages, NewSearchForm(), Navigation{}, currentUser, r.URL, mux.CurrentRoute(r)}
|
||||
errorTmpl := viewProfileEditTemplate.ExecuteTemplate(w, "index.html", htv)
|
||||
if errorTmpl != nil {
|
||||
http.Error(w, errorTmpl.Error(), http.StatusInternalServerError)
|
||||
|
|
|
@ -23,7 +23,10 @@
|
|||
<div class="col-lg-8">
|
||||
<div class="ui-select">
|
||||
<select id="language" name="language" class="form-control">
|
||||
<option value="en-us" {{ if eq .Language "en-us" }}selected{{end}}>{{T "english"}} ({{ T "default" }})</option>
|
||||
{{ $userLanguage := .Language }}
|
||||
{{ range $tag, $translatedName := $.Languages }}
|
||||
<option value="{{ $tag }}" {{ if or (eq $userLanguage $tag) (and (eq $userLanguage "") (eq $tag "en-us")) }}selected{{end}}>{{ $translatedName }} {{if eq $tag "en-us"}}({{ T "default" }}){{end}}</option>
|
||||
{{ end }}
|
||||
</select>
|
||||
</div>
|
||||
{{ range (index $.FormErrors "language")}}
|
||||
|
|
|
@ -3,6 +3,7 @@ package languages
|
|||
import (
|
||||
"fmt"
|
||||
"github.com/nicksnyder/go-i18n/i18n"
|
||||
"github.com/ewhal/nyaa/service/user"
|
||||
"html/template"
|
||||
"net/http"
|
||||
)
|
||||
|
@ -33,6 +34,22 @@ func TfuncWithFallback(language string, languages ...string) (i18n.TranslateFunc
|
|||
}, nil
|
||||
}
|
||||
|
||||
func GetAvailableLanguages() (languages map[string]string) {
|
||||
languages = make(map[string]string)
|
||||
var T i18n.TranslateFunc
|
||||
for _, languageTag := range i18n.LanguageTags() {
|
||||
T, _ = i18n.Tfunc(languageTag)
|
||||
/* Translation files should have an ID with the translated language name.
|
||||
If they don't, just use the languageTag */
|
||||
if languageName := T("language_name"); languageName != "language_name" {
|
||||
languages[languageTag] = languageName;
|
||||
} else {
|
||||
languages[languageTag] = languageTag
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func SetTranslation(tmpl *template.Template, language string, languages ...string) i18n.TranslateFunc {
|
||||
T, _ := TfuncWithFallback(language, languages...)
|
||||
tmpl.Funcs(map[string]interface{}{
|
||||
|
@ -44,12 +61,19 @@ func SetTranslation(tmpl *template.Template, language string, languages ...strin
|
|||
}
|
||||
|
||||
func SetTranslationFromRequest(tmpl *template.Template, r *http.Request, defaultLanguage string) i18n.TranslateFunc {
|
||||
userLanguage := ""
|
||||
user, _, err := userService.RetrieveCurrentUser(r)
|
||||
if err == nil {
|
||||
userLanguage = user.Language;
|
||||
}
|
||||
|
||||
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")
|
||||
return SetTranslation(tmpl, cookieLanguage, headerLanguage, defaultLanguage)
|
||||
return SetTranslation(tmpl, userLanguage, cookieLanguage, headerLanguage, defaultLanguage)
|
||||
}
|
||||
|
|
Référencer dans un nouveau ticket