diff --git a/router/router.go b/router/router.go
index 8f93b335..5964bac3 100755
--- a/router/router.go
+++ b/router/router.go
@@ -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)
}
diff --git a/router/template.go b/router/template.go
index 26314ea4..dab38d86 100644
--- a/router/template.go
+++ b/router/template.go
@@ -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")
diff --git a/router/templateVariables.go b/router/templateVariables.go
index cda04256..5e424088 100644
--- a/router/templateVariables.go
+++ b/router/templateVariables.go
@@ -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 {
diff --git a/templates/index.html b/templates/index.html
index 2477c7ae..93810b2f 100755
--- a/templates/index.html
+++ b/templates/index.html
@@ -95,6 +95,7 @@
+
{{ T "change_language" }}
diff --git a/translations/en-us.all.json b/translations/en-us.all.json
index f0d97427..bcdadec8 100644
--- a/translations/en-us.all.json
+++ b/translations/en-us.all.json
@@ -614,5 +614,9 @@
{
"id": "completed",
"translation": "Completed"
+ },
+ {
+ "id": "change_language",
+ "translation": "Change Language"
}
]
diff --git a/translations/pt-br.all.json b/translations/pt-br.all.json
index 590eaf33..3c29fc79 100644
--- a/translations/pt-br.all.json
+++ b/translations/pt-br.all.json
@@ -578,5 +578,9 @@
{
"id": "profile_edit_page",
"translation": "Editar o perfil de %s"
+ },
+ {
+ "id": "change_language",
+ "translation": "Mudar Idioma"
}
]
diff --git a/util/languages/translation.go b/util/languages/translation.go
index 2be97bfb..1bde223f 100644
--- a/util/languages/translation.go
+++ b/util/languages/translation.go
@@ -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
}