From f88b4656a1ff45d42b2638e617fa998f43e794d1 Mon Sep 17 00:00:00 2001 From: akuma06 Date: Sun, 7 May 2017 02:32:32 +0200 Subject: [PATCH] Fixed Typo + Added multi language support Multi language for User package --- .gitignore | 1 + main.go | 7 +++++++ router/templateFunctions.go | 4 +++- router/userHandler.go | 9 +++++---- service/user/locale/en-us.all.json | 4 ++++ templates/user/register.html | 2 +- util/languages/translation.go | 13 +++++++++++++ 7 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 util/languages/translation.go diff --git a/.gitignore b/.gitignore index c5382831..4d5b862f 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ nyaa-master.exe *.swp .vscode templates/*.html.go +*.bat \ No newline at end of file diff --git a/main.go b/main.go index e2ce0e97..8c7ed5f0 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "bufio" "flag" "fmt" + "github.com/nicksnyder/go-i18n/i18n" "github.com/ewhal/nyaa/config" "github.com/ewhal/nyaa/db" @@ -15,6 +16,11 @@ import ( "time" ) +func initI18N() { + /* Initialize the languages translation */ + i18n.MustLoadTranslationFile("service/user/locale/en-us.all.json") +} + func RunServer(conf *config.Config) { http.Handle("/", router.Router) @@ -45,6 +51,7 @@ func main() { log.CheckError(err) } db.ORM, _ = db.GormInit(conf) + initI18N() RunServer(conf) } } \ No newline at end of file diff --git a/router/templateFunctions.go b/router/templateFunctions.go index a01e73e6..50d42bf5 100644 --- a/router/templateFunctions.go +++ b/router/templateFunctions.go @@ -6,7 +6,8 @@ import ( "math" "net/url" "strconv" - + "github.com/nicksnyder/go-i18n/i18n" + "github.com/ewhal/nyaa/templates" ) @@ -68,4 +69,5 @@ var FuncMap = template.FuncMap{ "SearchAdvanced": func(nav templates.Navigation, s templates.SearchForm) template.HTML { return template.HTML(templates.SearchAdvanced(nav, s)) }, + "T": i18n.IdentityTfunc, } diff --git a/router/userHandler.go b/router/userHandler.go index 4b8ec24b..15bde3a2 100644 --- a/router/userHandler.go +++ b/router/userHandler.go @@ -7,21 +7,22 @@ import( "github.com/ewhal/nyaa/templates" "github.com/ewhal/nyaa/service/user/form" "github.com/ewhal/nyaa/util/modelHelper" + "github.com/ewhal/nyaa/util/languages" "github.com/gorilla/mux" ) -var viewRegisterTemplate = template.Must(template.New("view").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/user/register.html")) +var viewRegisterTemplate = template.Must(template.New("userRegister").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/user/register.html")) //var viewTemplate = template.Must(template.New("view").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/view.html")) //var viewTemplate = template.Must(template.New("view").Funcs(FuncMap).ParseFiles("templates/index.html", "templates/view.html")) // Getting View User Registration func UserRegisterFormHandler(w http.ResponseWriter, r *http.Request) { - b := form.RegistrationForm{} - modelHelper.BindValueForm(b, r) + modelHelper.BindValueForm(&b, r) + languages.SetTranslation("en-us", viewRegisterTemplate) htv := UserRegisterTemplateVariables{b, templates.NewSearchForm(), templates.Navigation{}, r.URL, mux.CurrentRoute(r)} - err := viewTemplate.ExecuteTemplate(w, "index.html", htv) + err := viewRegisterTemplate.ExecuteTemplate(w, "index.html", htv) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } diff --git a/service/user/locale/en-us.all.json b/service/user/locale/en-us.all.json index 28eaf0b1..cf5cf9af 100644 --- a/service/user/locale/en-us.all.json +++ b/service/user/locale/en-us.all.json @@ -18,5 +18,9 @@ { "id": "reset_password_content", "translation": "Please click below link to reset your password." + }, + { + "id":"register_title", + "translation": "Creating a new account" } ] diff --git a/templates/user/register.html b/templates/user/register.html index ad21db4b..e729d50e 100644 --- a/templates/user/register.html +++ b/templates/user/register.html @@ -1,4 +1,4 @@ -{{define "title"}}{{.Torrent.Name}}{{end}} +{{define "title"}}{{ T "register_title" }}{{end}} {{define "contclass"}}cont-view{{end}} {{define "content"}}
diff --git a/util/languages/translation.go b/util/languages/translation.go new file mode 100644 index 00000000..9d39a56a --- /dev/null +++ b/util/languages/translation.go @@ -0,0 +1,13 @@ +package languages + +import ( + "github.com/nicksnyder/go-i18n/i18n" + "html/template" + ) + + func SetTranslation(language string, template *template.Template) { + T, _ := i18n.Tfunc(language) + template.Funcs(map[string]interface{}{ + "T": T, + }) + } \ No newline at end of file