Skip to content

Commit

Permalink
feature: add i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
chenqinghe committed Apr 7, 2020
1 parent f73a26b commit 3018927
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 38 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
# redis-desktop
# Redis Desktop
a redis-cli with gui.

![demo](screenshoot/tabpage.png)


# features
# Features
* [x] save sessions
* [x] multi tabpages
* [x] no any dependencies
* [x] i18n
* [ ] batch exec commands
* [ ] other charming features.....

# thanks
# Thanks
the project is powered by [walk](https://github.com/lxn/walk) and [redigo](https://github.com/gomodule/redigo)

# license
# License
the porject is under [MIT license](LICENSE) which can be found in LICENSE file.
34 changes: 34 additions & 0 deletions i18n/en_us.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package i18n

func init() {
AddLang(en_us)
}

var en_us = Lang{
name: "en_us",
Translation: &Translation{
sections: make([]string, 0),
words: map[string]string{
"mainwindow.title": "Redis Cli Desktop",
"mainwindow.menu.file": "File",
"mainwindow.menu.file.export": "export",
"mainwindow.menu.file.import": "import",
"mainwindow.menu.edit": "Edit",
"mainwindow.menu.edit.clear": "clear",
"mainwindow.menu.setting": "Setting",
"mainwindow.menu.setting.theme": "theme",
"mainwindow.menu.logpath": "Log Path",
"mainwindow.menu.run": "Run",
"mainwindow.menu.run.batch": "run batch command",
"mainwindow.menu.help": "Help",
"mainwindow.menu.help.source": "view source code",
"mainwindow.menu.help.bug": "new issue",
"mainwindow.menu.help.donate": "donate",
"mainwindow.LBsessions.menu.deletesession": "delete session",
"mainwindow.labelhost": "Host",
"mainwindow.labelport": "Port",
"mainwindow.labelpassword": "Password",
"mainwindow.PBconnect": "Connect",
},
},
}
1 change: 0 additions & 1 deletion i18n/english.go

This file was deleted.

114 changes: 114 additions & 0 deletions i18n/i18n.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package i18n

import (
"fmt"
"reflect"
"strings"
)

type Lang struct {
name string
*Translation
}

type langStore struct {
defaultLang Lang
langs map[string]Lang
}

var defaultStore = langStore{
langs: make(map[string]Lang),
}

func SetDefaultLang(lang Lang) {
defaultStore.defaultLang = lang
}

func AddLang(lang Lang) error {
if _, ok := defaultStore.langs[strings.ToLower(lang.name)]; ok {
return fmt.Errorf("the lang `%s` already exist", lang.name)
}
defaultStore.langs[strings.ToLower(lang.name)] = lang
if defaultStore.defaultLang.name == "" {
defaultStore.defaultLang = lang
}
return nil
}

func GetLang(name string) (Lang, bool) {
if lang, ok := defaultStore.langs[strings.ToLower(name)]; ok {
return lang, true
}
return Lang{}, false
}

func (l Lang) Tr(format string, args ...interface{}) string {
return Tr(l.name, format, args...)
}

type Translation struct {
sections []string
words map[string]string
}

func (t *Translation) clone() *Translation {
secs := make([]string, len(t.sections))
copy(secs, t.sections)
return &Translation{
sections: secs,
words: t.words,
}
}

func (t *Translation) Section(sec string) *Translation {
t0 := t.clone()
t0.sections = append(t0.sections, sec)
return t0
}

func (t *Translation) Get(name string) (string, bool) {
sections := t.sections
sections = append(sections, name)
key := strings.Join(sections, ".")
v, ok := t.words[key]
return v, ok
}

// Tr translates content to target language.
func Tr(lang, format string, args ...interface{}) string {
language, ok := defaultStore.langs[lang]
if !ok {
language = defaultStore.defaultLang
}

value, ok := language.Get(format)
if ok {
format = value
} else {
// try default language
value, ok = defaultStore.defaultLang.Get(format)
if ok {
format = value
}
}

if len(args) > 0 {
params := make([]interface{}, 0, len(args))
for _, arg := range args {
if arg == nil {
continue
}

val := reflect.ValueOf(arg)
if val.Kind() == reflect.Slice {
for i := 0; i < val.Len(); i++ {
params = append(params, val.Index(i).Interface())
}
} else {
params = append(params, arg)
}
}
return fmt.Sprintf(format, params...)
}
return format
}
33 changes: 33 additions & 0 deletions i18n/zh_cn.go
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
package i18n

func init() {
AddLang(zh_ch)
}

var zh_ch = Lang{
name: "zh_cn",
Translation: &Translation{
sections: make([]string, 0),
words: map[string]string{
"mainwindow.title": "redis命令行工具",
"mainwindow.menu.file": "文件",
"mainwindow.menu.file.export": "导出会话",
"mainwindow.menu.file.import": "导入会话",
"mainwindow.menu.edit": "编辑",
"mainwindow.menu.edit.clear": "清屏",
"mainwindow.menu.setting": "设置",
"mainwindow.menu.setting.theme": "主题",
"mainwindow.menu.logpath": "日志路径",
"mainwindow.menu.run": "运行",
"mainwindow.menu.run.batch": "批量运行命令",
"mainwindow.menu.help": "帮助",
"mainwindow.menu.help.source": "查看源码",
"mainwindow.menu.help.bug": "提bug",
"mainwindow.menu.help.donate": "捐赠",
"mainwindow.LBsessions.menu.deletesession": "删除会话",
"mainwindow.labelhost": "主机",
"mainwindow.labelport": "端口",
"mainwindow.labelpassword": "密码",
"mainwindow.PBconnect": "连接",
},
},
}
15 changes: 10 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/chenqinghe/redis-desktop/i18n"
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
Expand Down Expand Up @@ -32,14 +33,18 @@ func main() {
// coredump(err.Error())
//}

mw := createMainWindow()
mw.sessionFile = filepath.Join(rootPath, "RedisDesktop", "data")
sessions, err := loadSession(mw.sessionFile)
if err != nil {
// TODO: how to config languages?
lang, ok := i18n.GetLang("zh_cn")
if !ok {
return
}
mw := createMainWindow(lang)

mw.SetSessionFile(filepath.Join(rootPath, "RedisDesktop", "data"))
if err := mw.LoadSession(); err != nil {
walk.MsgBox(nil, "ERROR", "加载会话文件失败:"+err.Error(), walk.MsgBoxIconError)
return
}
mw.LB_sessions.AddSessions(sessions)

mw.Run()
}
Loading

0 comments on commit 3018927

Please sign in to comment.