Skip to content

Commit

Permalink
improve i18n with gview
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn committed Sep 8, 2019
1 parent 16cc3c0 commit 77a727e
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 19 deletions.
16 changes: 16 additions & 0 deletions .example/encoding/gini/gini.go
Original file line number Diff line number Diff line change
@@ -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 changes: 48 additions & 0 deletions .example/i18n/gi18n/http_view_i18n.go
Original file line number Diff line number Diff line change
@@ -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()
}
2 changes: 1 addition & 1 deletion .example/i18n/gi18n/i18n-dir/ru/hello.ini
Original file line number Diff line number Diff line change
@@ -1 +1 @@
hello = Привет
hello = "Привет"
2 changes: 1 addition & 1 deletion .example/i18n/gi18n/i18n-dir/ru/world.ini
Original file line number Diff line number Diff line change
@@ -1 +1 @@
world=мир
world = "мир"
4 changes: 2 additions & 2 deletions .example/i18n/gi18n/i18n-file/ru.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
hello = Привет
world = мир
hello = "Привет"
world = "мир"
1 change: 1 addition & 0 deletions .example/i18n/gi18n/template/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{T "hello" "你好"}} {{T "world" "世界"}}!
17 changes: 15 additions & 2 deletions frame/gmvc/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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,
}
Expand Down Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion os/gfile/gfile_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
5 changes: 3 additions & 2 deletions os/gview/gview.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package gview
import (
"errors"
"fmt"
"github.com/gogf/gf/i18n/gi18n"
"sync"

"github.com/gogf/gf/os/gres"
Expand All @@ -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.
}

Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 5 additions & 3 deletions os/gview/gview_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 changes: 6 additions & 7 deletions os/gview/gview_doparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

0 comments on commit 77a727e

Please sign in to comment.