Skip to content

Commit

Permalink
[add] widget table i18n support
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Oct 7, 2022
1 parent c0e7f88 commit 92cc28d
Show file tree
Hide file tree
Showing 16 changed files with 562 additions and 111 deletions.
276 changes: 211 additions & 65 deletions data/bindata.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ require (
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gopkg.in/yaml.v3 v3.0.1
rogchap.com/v8go v0.7.0 // indirect
)

Expand Down
71 changes: 70 additions & 1 deletion lang/lang.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package lang

import (
"fmt"
"os"
"path/filepath"

"github.com/yaoapp/gou/lang"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/yao/data"
"gopkg.in/yaml.v3"
)

func init() {
Expand All @@ -21,8 +24,13 @@ func init() {

// Load language packs
func Load(cfg config.Config) error {
err := loadFromBin()
if err != nil {
return err
}

root := filepath.Join(cfg.Root, "langs")
err := lang.Load(root)
err = lang.Load(root)
if err != nil {
return err
}
Expand All @@ -40,3 +48,64 @@ func Load(cfg config.Config) error {

return nil
}

func loadFromBin() error {

dirs := map[string][]struct {
File string
Widget string
Instance string
IsGlobal bool
}{
"zh-cn": {
{File: "yao/langs/zh-cn/global.yml", IsGlobal: true},
{File: "yao/langs/zh-cn/logins/admin.login.yml", Widget: "login", Instance: "admin"},
{File: "yao/langs/zh-cn/logins/user.login.yml", Widget: "login", Instance: "user"},
},
"zh-hk": {
{File: "yao/langs/zh-hk/global.yml", IsGlobal: true},
{File: "yao/langs/zh-hk/logins/admin.login.yml", Widget: "login", Instance: "admin"},
{File: "yao/langs/zh-hk/logins/user.login.yml", Widget: "login", Instance: "user"},
},
}

for langName, files := range dirs {

dict := &lang.Dict{
Name: langName,
Global: lang.Words{},
Widgets: map[string]lang.Widget{},
}

for _, f := range files {
data, err := data.Read(f.File)
if err != nil {
return fmt.Errorf("%s: %s", f.File, err.Error())
}

words := lang.Words{}
err = yaml.Unmarshal(data, &words)
if err != nil {
return fmt.Errorf("%s: %s", f.File, err.Error())
}

if f.IsGlobal {
dict.Global = words
continue
}

if _, has := dict.Widgets[f.Widget]; !has {
dict.Widgets[f.Widget] = map[string]lang.Words{}
}
dict.Widgets[f.Widget][f.Instance] = words
}

if _, has := lang.Dicts[langName]; has {
lang.Dicts[langName].Merge(dict)
continue
}

lang.Dicts[langName] = dict
}
return nil
}
5 changes: 4 additions & 1 deletion lang/lang_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (

func TestLoad(t *testing.T) {
share.DBConnect(config.Conf.DB)
Load(config.Conf)
err := Load(config.Conf)
if err != nil {
t.Fatal(err)
}
assert.Len(t, lang.Dicts, 2)
}
74 changes: 74 additions & 0 deletions widgets/component/lang.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package component

// Trans trans
func (column *DSL) Trans(widgetName string, inst string, trans func(widget string, inst string, value *string) bool) bool {
return transMap(column.Props, widgetName, inst, trans)
}

func transMap(value map[string]interface{}, widgetName string, inst string, trans func(widget string, inst string, value *string) bool) bool {
res := false
for key, val := range value {

switch val.(type) {
case map[string]interface{}:
if transMap(val.(map[string]interface{}), widgetName, inst, trans) {
res = true
}
break

case []interface{}:
if transArr(val.([]interface{}), widgetName, inst, trans) {
res = true
}
break

case string:
new := val.(string)
if trans(widgetName, inst, &new) {
val = new
res = true
}
break
}

if trans(widgetName, inst, &key) {
res = true
}

value[key] = val
}

return res
}

func transArr(value []interface{}, widgetName string, inst string, trans func(widget string, inst string, value *string) bool) bool {
res := false
for idx, val := range value {

switch val.(type) {
case map[string]interface{}:
if transMap(val.(map[string]interface{}), widgetName, inst, trans) {
res = true
}
break

case []interface{}:
if transArr(val.([]interface{}), widgetName, inst, trans) {
res = true
}
break

case string:
new := val.(string)
if trans(widgetName, inst, &new) {
val = new
res = true
}
break
}

value[idx] = val
}

return res
}
36 changes: 36 additions & 0 deletions widgets/field/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,42 @@ func (column ColumnDSL) Replace(data map[string]interface{}) (*ColumnDSL, error)
return &new, nil
}

// Trans trans
func (column *ColumnDSL) Trans(widgetName string, inst string, trans func(widget string, inst string, value *string) bool) bool {
res := false
if column.Edit != nil {
if column.Edit.Trans(widgetName, inst, trans) {
res = true
}
}

if column.View != nil {
if column.View.Trans(widgetName, inst, trans) {
res = true
}
}

return res
}

// Trans column trans
func (columns Columns) Trans(widgetName string, inst string, trans func(widget string, inst string, value *string) bool) bool {
res := false

for key, column := range columns {
if trans(widgetName, inst, &key) {
res = true
}
newPtr := &column
if newPtr.Trans(widgetName, inst, trans) {
res = true
}
columns[key] = *newPtr
}

return res
}

// Map cast to map[string]inteface{}
func (column ColumnDSL) Map() map[string]interface{} {
res := map[string]interface{}{
Expand Down
27 changes: 27 additions & 0 deletions widgets/field/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,33 @@ func (filter FilterDSL) Replace(data map[string]interface{}) (*FilterDSL, error)
return &new, nil
}

// Trans trans
func (filter *FilterDSL) Trans(widgetName string, inst string, trans func(widget string, inst string, value *string) bool) bool {
res := false
if filter.Edit != nil {
if filter.Edit.Trans(widgetName, inst, trans) {
res = true
}
}
return res
}

// Trans column trans
func (filters Filters) Trans(widgetName string, inst string, trans func(widget string, inst string, value *string) bool) bool {
res := false
for key, filter := range filters {
if trans(widgetName, inst, &key) {
res = true
}
newPtr := &filter
if newPtr.Trans(widgetName, inst, trans) {
res = true
}
filters[key] = *newPtr
}
return res
}

// Map cast to map[string]inteface{}
func (filter FilterDSL) Map() map[string]interface{} {
res := map[string]interface{}{
Expand Down
6 changes: 3 additions & 3 deletions widgets/field/transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestTransformFilter(t *testing.T) {
}
assert.Equal(t, "Bar", fi.Key)
assert.Equal(t, "where.Foo.match", fi.Bind)
assert.Equal(t, "::please input Bar", fi.Edit.Props["placeholder"])
assert.Equal(t, "$L(please input) Bar", fi.Edit.Props["placeholder"])

fi, err = tr.Filter("not-found", data)
assert.True(t, IsNotFound(err))
Expand All @@ -52,7 +52,7 @@ func TestTransformTable(t *testing.T) {
}
assert.Equal(t, "Bar", tab.Key)
assert.Equal(t, "Foo", tab.Bind)
assert.Equal(t, "::please input Bar", tab.Edit.Props["placeholder"])
assert.Equal(t, "$L(please input) Bar", tab.Edit.Props["placeholder"])

tab, err = tr.Table("not-found", data)
assert.True(t, IsNotFound(err))
Expand All @@ -67,7 +67,7 @@ func TestTransformForm(t *testing.T) {
}
assert.Equal(t, "Bar", form.Key)
assert.Equal(t, "Foo", form.Bind)
assert.Equal(t, "::please input Bar", form.Edit.Props["placeholder"])
assert.Equal(t, "$L(please input) Bar", form.Edit.Props["placeholder"])

form, err = tr.Form("not-found", data)
assert.True(t, IsNotFound(err))
Expand Down
4 changes: 4 additions & 0 deletions widgets/table/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ package table
func (dsl *DSL) Lang(trans func(widget string, inst string, value *string) bool) {
widget := "table"
trans(widget, dsl.ID, &dsl.Name)
if dsl.Fields != nil {
dsl.Fields.Filter.Trans(widget, dsl.ID, trans)
dsl.Fields.Table.Trans(widget, dsl.ID, trans)
}
}
Loading

0 comments on commit 92cc28d

Please sign in to comment.