From 77a727e1d70ee4d824a438531445df9e71691f33 Mon Sep 17 00:00:00 2001 From: john Date: Sun, 8 Sep 2019 21:12:11 +0800 Subject: [PATCH] improve i18n with gview --- .example/encoding/gini/gini.go | 16 ++++++++ .example/i18n/gi18n/http_view_i18n.go | 48 +++++++++++++++++++++++ .example/i18n/gi18n/i18n-dir/ru/hello.ini | 2 +- .example/i18n/gi18n/i18n-dir/ru/world.ini | 2 +- .example/i18n/gi18n/i18n-file/ru.ini | 4 +- .example/i18n/gi18n/template/index.html | 1 + frame/gmvc/view.go | 17 +++++++- os/gfile/gfile_search.go | 1 - os/gview/gview.go | 5 ++- os/gview/gview_config.go | 8 ++-- os/gview/gview_doparse.go | 13 +++--- 11 files changed, 98 insertions(+), 19 deletions(-) create mode 100644 .example/encoding/gini/gini.go create mode 100644 .example/i18n/gi18n/http_view_i18n.go create mode 100644 .example/i18n/gi18n/template/index.html diff --git a/.example/encoding/gini/gini.go b/.example/encoding/gini/gini.go new file mode 100644 index 000000000..758c5210b --- /dev/null +++ b/.example/encoding/gini/gini.go @@ -0,0 +1,16 @@ +package main + +import ( + "fmt" + "github.com/gogf/gf/encoding/gini" +) + +func main() { + s := ` +a = b + +` + m, err := gini.Decode([]byte(s)) + fmt.Println(err) + fmt.Println(m) +} diff --git a/.example/i18n/gi18n/http_view_i18n.go b/.example/i18n/gi18n/http_view_i18n.go new file mode 100644 index 000000000..952ecff7f --- /dev/null +++ b/.example/i18n/gi18n/http_view_i18n.go @@ -0,0 +1,48 @@ +package main + +import ( + "fmt" + "github.com/gogf/gf/frame/g" + "github.com/gogf/gf/frame/gmvc" + "github.com/gogf/gf/net/ghttp" +) + +type Controller struct { + gmvc.Controller +} + +func (c *Controller) Init(r *ghttp.Request) { + c.Controller.Init(r) + c.View.BindFunc("T", c.translate) +} + +func (c *Controller) Index() { + c.View.Display("index.html") +} + +func (c *Controller) translate(langKey, msg string) string { + lang := c.Request.Get("lang", "en") + g.I18n().SetLanguage(lang) + t := g.I18n().T(langKey, lang) + fmt.Println(t, lang, c.Request.Request.Header.Get("Accept-Language")) + return t +} + +func main() { + g.I18n().SetPath("i18n-file") + g.View().SetPath(`D:\Workspace\Go\GOPATH\src\github.com\gogf\gf\.example\i18n\gi18n\template`) + s := g.Server() + s.BindHandler("/", func(r *ghttp.Request) { + lang := r.Get("lang", "en") + g.I18n().SetLanguage(lang) + r.Response.WriteTplContent(`{#hello}{#world}`) + }) + s.BindHandler("/template", func(r *ghttp.Request) { + lang := r.Get("lang", "en") + g.I18n().SetLanguage(lang) + r.Response.WriteTplContent(`{#hello}{#world}`) + }) + s.BindController("/controller", new(Controller)) + s.SetPort(8199) + s.Run() +} diff --git a/.example/i18n/gi18n/i18n-dir/ru/hello.ini b/.example/i18n/gi18n/i18n-dir/ru/hello.ini index d2e2af1ca..c65ec2155 100644 --- a/.example/i18n/gi18n/i18n-dir/ru/hello.ini +++ b/.example/i18n/gi18n/i18n-dir/ru/hello.ini @@ -1 +1 @@ -hello = Привет \ No newline at end of file +hello = "Привет" \ No newline at end of file diff --git a/.example/i18n/gi18n/i18n-dir/ru/world.ini b/.example/i18n/gi18n/i18n-dir/ru/world.ini index fffaf192d..7e9e60499 100644 --- a/.example/i18n/gi18n/i18n-dir/ru/world.ini +++ b/.example/i18n/gi18n/i18n-dir/ru/world.ini @@ -1 +1 @@ -world=мир \ No newline at end of file +world = "мир" \ No newline at end of file diff --git a/.example/i18n/gi18n/i18n-file/ru.ini b/.example/i18n/gi18n/i18n-file/ru.ini index f04620fab..fa2df856c 100644 --- a/.example/i18n/gi18n/i18n-file/ru.ini +++ b/.example/i18n/gi18n/i18n-file/ru.ini @@ -1,2 +1,2 @@ -hello = Привет -world = мир \ No newline at end of file +hello = "Привет" +world = "мир" \ No newline at end of file diff --git a/.example/i18n/gi18n/template/index.html b/.example/i18n/gi18n/template/index.html new file mode 100644 index 000000000..e2a2a6b43 --- /dev/null +++ b/.example/i18n/gi18n/template/index.html @@ -0,0 +1 @@ +{{T "hello" "你好"}} {{T "world" "世界"}}! \ No newline at end of file diff --git a/frame/gmvc/view.go b/frame/gmvc/view.go index f37ec9e9d..9b9340942 100644 --- a/frame/gmvc/view.go +++ b/frame/gmvc/view.go @@ -9,7 +9,6 @@ package gmvc import ( "sync" - "github.com/gogf/gf/frame/gins" "github.com/gogf/gf/net/ghttp" "github.com/gogf/gf/os/gview" ) @@ -25,7 +24,7 @@ type View struct { // 创建一个MVC请求中使用的视图对象 func NewView(w *ghttp.Response) *View { return &View{ - view: gins.View(), + view: gview.New(), data: make(gview.Params), response: w, } @@ -77,6 +76,20 @@ func (view *View) RLockFunc(f func(data gview.Params)) { f(view.data) } +// BindFunc registers customized template function named +// with given function to current view object. +// The is the function name which can be called in template content. +func (view *View) BindFunc(name string, function interface{}) { + view.view.BindFunc(name, function) +} + +// BindFuncMap registers customized template functions by map to current view object. +// The key of map is the template function name +// and the value of map is the address of customized function. +func (view *View) BindFuncMap(funcMap gview.FuncMap) { + view.view.BindFuncMap(funcMap) +} + // 解析并显示指定模板 func (view *View) Display(file ...string) error { name := "index.tpl" diff --git a/os/gfile/gfile_search.go b/os/gfile/gfile_search.go index a9c473f50..2936f280a 100644 --- a/os/gfile/gfile_search.go +++ b/os/gfile/gfile_search.go @@ -23,7 +23,6 @@ func Search(name string, prioritySearchPaths ...string) (realPath string, err er if realPath != "" { return } - // TODO move search paths to internal package variable. // Search paths array. array := garray.NewStrArray() array.Append(prioritySearchPaths...) diff --git a/os/gview/gview.go b/os/gview/gview.go index 4683331df..982848fe2 100644 --- a/os/gview/gview.go +++ b/os/gview/gview.go @@ -10,6 +10,7 @@ package gview import ( "errors" "fmt" + "github.com/gogf/gf/i18n/gi18n" "sync" "github.com/gogf/gf/os/gres" @@ -28,7 +29,7 @@ type View struct { paths *garray.StrArray // Searching path array. data map[string]interface{} // Global template variables. funcMap map[string]interface{} // Global template function map. - i18nEnabled bool // Is i18n enabled in this template. + i18nManager *gi18n.Manager // I18n manager for this view. delimiters []string // Customized template delimiters. } @@ -63,7 +64,7 @@ func New(path ...string) *View { paths: garray.NewStrArray(true), data: make(map[string]interface{}), funcMap: make(map[string]interface{}), - i18nEnabled: true, + i18nManager: gi18n.Instance(), delimiters: make([]string, 2), } if len(path) > 0 && len(path[0]) > 0 { diff --git a/os/gview/gview_config.go b/os/gview/gview_config.go index 5b0da3d1a..4b5d3d1cd 100644 --- a/os/gview/gview_config.go +++ b/os/gview/gview_config.go @@ -6,6 +6,8 @@ package gview +import "github.com/gogf/gf/i18n/gi18n" + // Assign binds multiple template variables to current view object. // Each goroutine will take effect after the call, so it is concurrent-safe. func (view *View) Assigns(data Params) { @@ -52,9 +54,9 @@ func (view *View) BindFuncMap(funcMap FuncMap) { view.mu.Unlock() } -// SetI18nEnabled enables/disables i18n feature in this template. -func (view *View) SetI18nEnabled(enabled bool) { +// SetI18n binds i18n manager to view engine. +func (view *View) SetI18n(manager *gi18n.Manager) { view.mu.Lock() - view.i18nEnabled = enabled + view.i18nManager = manager view.mu.Unlock() } diff --git a/os/gview/gview_doparse.go b/os/gview/gview_doparse.go index fab21810d..97035e0cc 100644 --- a/os/gview/gview_doparse.go +++ b/os/gview/gview_doparse.go @@ -13,8 +13,6 @@ import ( "strings" "text/template" - "github.com/gogf/gf/i18n/gi18n" - "github.com/gogf/gf/os/gfcache" "github.com/gogf/gf/os/gres" @@ -61,7 +59,8 @@ func (view *View) getTemplate(path string, pattern string) (tpl *template.Templa return tpl } // Secondly checking the file system. - files, err := gfile.ScanDir(path, pattern, true) + files := ([]string)(nil) + files, err = gfile.ScanDir(path, pattern, true) if err != nil { return nil } @@ -211,8 +210,8 @@ func (view *View) Parse(file string, params ...Params) (parsed string, err error return "", err } result := gstr.Replace(buffer.String(), "", "") - if view.i18nEnabled { - result = gi18n.T(result) + if view.i18nManager != nil { + result = view.i18nManager.T(result) } return result, nil } @@ -267,8 +266,8 @@ func (view *View) ParseContent(content string, params ...Params) (string, error) return "", err } result := gstr.Replace(buffer.String(), "", "") - if view.i18nEnabled { - result = gi18n.T(result) + if view.i18nManager != nil { + result = view.i18nManager.T(result) } return result, nil }