Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Add route and template files

Cette révision appartient à :
ElegantMonkey 2017-05-12 19:28:56 -03:00
Parent dfd9caeb31
révision 7808615c6a
2 fichiers modifiés avec 70 ajouts et 0 suppressions

Voir le fichier

@ -0,0 +1,46 @@
package router;
import (
"net/http"
"github.com/ewhal/nyaa/util/languages"
"github.com/ewhal/nyaa/service/user"
"github.com/gorilla/mux"
)
func SeeLanguagesHandler(w http.ResponseWriter, r *http.Request) {
_, Tlang := languages.GetTfuncAndLanguageFromRequest(r, "en-us")
availableLanguages := languages.GetAvailableLanguages()
clv := ChangeLanguageVariables{NewSearchForm(), Navigation{}, Tlang.Tag, availableLanguages, GetUser(r), r.URL, mux.CurrentRoute(r)}
languages.SetTranslationFromRequest(changeLanguageTemplate, r, "en-us")
err := changeLanguageTemplate.ExecuteTemplate(w, "index.html", clv)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func ChangeLanguageHandler(w http.ResponseWriter, r *http.Request) {
lang := r.FormValue("language")
availableLanguages := languages.GetAvailableLanguages()
if _, exists := availableLanguages[lang]; !exists {
http.Error(w, "Language not available", http.StatusInternalServerError)
return
}
// If logged in, update user language; if not, set cookie.
user, err := userService.CurrentUser(r)
if err == nil {
user.Language = lang
// I don't know if I should use this...
userService.UpdateUserCore(&user)
} else {
http.SetCookie(w, &http.Cookie{Name: "lang", Value: lang})
}
url, _ := Router.Get("home").URL()
http.Redirect(w, r, url.String(), http.StatusSeeOther)
}

24
templates/change_language.html Fichier normal
Voir le fichier

@ -0,0 +1,24 @@
{{define "title"}}{{T "change_language"}}{{end}}
{{define "content"}}
<div class="blockBody">
<hr>
<form role="form" method="POST">
<div class="form-group">
<label for="language">{{T "language"}}</label>
<div class="ui-select">
<select id="language" name="language" class="form-control">
{{ $currentLanguage := .Language }}
{{ range $tag, $translatedName := $.Languages }}
<option value="{{ $tag }}" {{ if eq $currentLanguage $tag }}selected{{end}}>{{ $translatedName }}</option>
{{ end }}
</select>
</div>
</div>
<button type="submit" class="btn btn-success">{{T "save_changes"}}</button>
</form>
<div style="padding-bottom: 1em"></div>
</div>
{{end}}