Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Add language selector (#298)

Cette révision appartient à :
Ramon Dantas 2017-05-10 16:45:39 -03:00 révisé par Austin
Parent 755a426931
révision c84140964b
4 fichiers modifiés avec 34 ajouts et 4 suppressions

Voir le fichier

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

Voir le fichier

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

Voir le fichier

@ -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")}}

Voir le fichier

@ -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)
}