mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
improve i18n with gview
This commit is contained in:
16
.example/encoding/gini/gini.go
Normal file
16
.example/encoding/gini/gini.go
Normal 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)
|
||||
}
|
||||
48
.example/i18n/gi18n/http_view_i18n.go
Normal file
48
.example/i18n/gi18n/http_view_i18n.go
Normal 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()
|
||||
}
|
||||
@ -1 +1 @@
|
||||
hello = Привет
|
||||
hello = "Привет"
|
||||
@ -1 +1 @@
|
||||
world=мир
|
||||
world = "мир"
|
||||
@ -1,2 +1,2 @@
|
||||
hello = Привет
|
||||
world = мир
|
||||
hello = "Привет"
|
||||
world = "мир"
|
||||
1
.example/i18n/gi18n/template/index.html
Normal file
1
.example/i18n/gi18n/template/index.html
Normal file
@ -0,0 +1 @@
|
||||
{{T "hello" "你好"}} {{T "world" "世界"}}!
|
||||
@ -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"
|
||||
|
||||
@ -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...)
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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()
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user