Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

Add language selector without login

Cette révision appartient à :
ElegantMonkey 2017-05-12 19:17:34 -03:00
Parent 09811cc275
révision dfd9caeb31
7 fichiers modifiés avec 47 ajouts et 12 suppressions

Voir le fichier

@ -112,5 +112,8 @@ func init() {
//Router.HandleFunc("/moderation/report/delete", gzipTorrentReportDeleteHandler).Name("torrent_report_delete").Methods("POST")
//Router.HandleFunc("/moderation/torrent/delete", gzipTorrentDeleteHandler).Name("torrent_delete").Methods("POST")
Router.HandleFunc("/language", SeeLanguagesHandler).Methods("GET").Name("see_languages")
Router.HandleFunc("/language", ChangeLanguageHandler).Methods("POST").Name("change_language")
Router.NotFoundHandler = http.HandlerFunc(NotFoundHandler)
}

Voir le fichier

@ -7,7 +7,7 @@ import (
var TemplateDir = "templates"
var homeTemplate, searchTemplate, faqTemplate, uploadTemplate, viewTemplate, viewRegisterTemplate, viewLoginTemplate, viewRegisterSuccessTemplate, viewVerifySuccessTemplate, viewProfileTemplate, viewProfileEditTemplate, viewUserDeleteTemplate, notFoundTemplate *template.Template
var homeTemplate, searchTemplate, faqTemplate, uploadTemplate, viewTemplate, viewRegisterTemplate, viewLoginTemplate, viewRegisterSuccessTemplate, viewVerifySuccessTemplate, viewProfileTemplate, viewProfileEditTemplate, viewUserDeleteTemplate, notFoundTemplate, changeLanguageTemplate *template.Template
var panelIndex, panelTorrentList, panelUserList, panelCommentList, panelTorrentEd, panelTorrentReportList *template.Template
@ -86,6 +86,11 @@ func ReloadTemplates() {
name: "404",
file: "404.html",
},
templateLoader{
templ: &changeLanguageTemplate,
name: "change_language",
file: "change_language.html",
},
}
for idx := range pubTempls {
pubTempls[idx].indexFile = filepath.Join(TemplateDir, "index.html")

Voir le fichier

@ -113,6 +113,17 @@ type UploadTemplateVariables struct {
Route *mux.Route
}
type ChangeLanguageVariables struct {
Search SearchForm
Navigation Navigation
Language string
Languages map[string]string
User *model.User
URL *url.URL
Route *mux.Route
}
/* MODERATION Variables */
type PanelIndexVbs struct {

Voir le fichier

@ -95,6 +95,7 @@
<footer>
Powered by NyaaPantsu
</footer>
<center><a href="{{ .URL.Parse "/language" }}">{{ T "change_language" }}</a></center>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

Voir le fichier

@ -614,5 +614,9 @@
{
"id": "completed",
"translation": "Completed"
},
{
"id": "change_language",
"translation": "Change Language"
}
]

Voir le fichier

@ -578,5 +578,9 @@
{
"id": "profile_edit_page",
"translation": "Editar o perfil de %s"
},
{
"id": "change_language",
"translation": "Mudar Idioma"
}
]

Voir le fichier

@ -4,25 +4,26 @@ import (
"fmt"
"github.com/ewhal/nyaa/service/user"
"github.com/nicksnyder/go-i18n/i18n"
"github.com/nicksnyder/go-i18n/i18n/language"
"html/template"
"net/http"
)
// When go-i18n finds a language with >0 translations, it uses it as the Tfunc
// However, if said language has a missing translation, it won't fallback to the "main" language
func TfuncWithFallback(language string, languages ...string) (i18n.TranslateFunc, error) {
func TfuncAndLanguageWithFallback(language string, languages ...string) (i18n.TranslateFunc, *language.Language, error) {
// Use the last language on the args as the fallback one.
fallbackLanguage := language
if languages != nil {
fallbackLanguage = languages[len(languages)-1]
}
T, err1 := i18n.Tfunc(language, languages...)
fallbackT, err2 := i18n.Tfunc(fallbackLanguage)
T, Tlang, err1 := i18n.TfuncAndLanguage(language, languages...)
fallbackT, fallbackTlang, err2 := i18n.TfuncAndLanguage(fallbackLanguage)
if err1 != nil && err2 != nil {
// fallbackT is still a valid function even with the error, it returns translationID.
return fallbackT, err2
return fallbackT, fallbackTlang, err2
}
return func(translationID string, args ...interface{}) string {
@ -31,7 +32,7 @@ func TfuncWithFallback(language string, languages ...string) (i18n.TranslateFunc
}
return fallbackT(translationID, args...)
}, nil
}, Tlang, nil
}
func GetAvailableLanguages() (languages map[string]string) {
@ -50,8 +51,7 @@ func GetAvailableLanguages() (languages map[string]string) {
return
}
func SetTranslation(tmpl *template.Template, language string, languages ...string) i18n.TranslateFunc {
T, _ := TfuncWithFallback(language, languages...)
func setTranslation(tmpl *template.Template, T i18n.TranslateFunc) {
tmpl.Funcs(map[string]interface{}{
"T": func(str string, args ...interface{}) template.HTML {
return template.HTML(fmt.Sprintf(T(str), args...))
@ -60,10 +60,9 @@ func SetTranslation(tmpl *template.Template, language string, languages ...strin
return fmt.Sprintf(T(str), args...)
},
})
return T
}
func SetTranslationFromRequest(tmpl *template.Template, r *http.Request, defaultLanguage string) i18n.TranslateFunc {
func GetTfuncAndLanguageFromRequest(r *http.Request, defaultLanguage string) (T i18n.TranslateFunc, Tlang *language.Language) {
userLanguage := ""
user, _, err := userService.RetrieveCurrentUser(r)
if err == nil {
@ -78,6 +77,14 @@ func SetTranslationFromRequest(tmpl *template.Template, r *http.Request, default
// go-i18n supports the format of the Accept-Language header, thankfully.
headerLanguage := r.Header.Get("Accept-Language")
r.Header.Add("Vary", "Accept-Encoding")
return SetTranslation(tmpl, userLanguage, cookieLanguage, headerLanguage, defaultLanguage)
T, Tlang, _ = TfuncAndLanguageWithFallback(userLanguage, cookieLanguage, headerLanguage, defaultLanguage)
return
}
func SetTranslationFromRequest(tmpl *template.Template, r *http.Request, defaultLanguage string) i18n.TranslateFunc {
r.Header.Add("Vary", "Accept-Encoding")
T, _ := GetTfuncAndLanguageFromRequest(r, defaultLanguage)
setTranslation(tmpl, T)
return T
}