improve i18n with gview

This commit is contained in:
john
2019-09-08 21:12:11 +08:00
parent 16cc3c0fca
commit 77a727e1d7
11 changed files with 98 additions and 19 deletions

View File

@ -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)
}

View File

@ -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()
}

View File

@ -1 +1 @@
hello = Привет
hello = "Привет"

View File

@ -1 +1 @@
world=мир
world = "мир"

View File

@ -1,2 +1,2 @@
hello = Привет
world = мир
hello = "Привет"
world = "мир"

View File

@ -0,0 +1 @@
{{T "hello" "你好"}} {{T "world" "世界"}}!

View File

@ -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 <name>
// with given function <function> to current view object.
// The <name> 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"

View File

@ -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...)

View File

@ -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 {

View File

@ -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()
}

View File

@ -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(), "<no value>", "")
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(), "<no value>", "")
if view.i18nEnabled {
result = gi18n.T(result)
if view.i18nManager != nil {
result = view.i18nManager.T(result)
}
return result, nil
}