Skip to content

Commit

Permalink
支持翻译驼峰命名的变量和函数名
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericwyn committed Nov 22, 2021
1 parent 36449d5 commit ef4cfc7
Show file tree
Hide file tree
Showing 18 changed files with 202 additions and 162 deletions.
18 changes: 18 additions & 0 deletions EzeTranslate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ func TestFormatAnnotation(t *testing.T) {
`))
}

func TestFormatCamelCaseText(t *testing.T) {
a := "hello friends"
fmt.Println(a, "->", strutils.FormatCamelCaseText(a))

b := "requestLocationUpdate"
fmt.Println(b, "->", strutils.FormatCamelCaseText(b))

c := "format_linux_function"
fmt.Println(c, "->", strutils.FormatCamelCaseText(c))

d := "CONFIG_KEY_TRANSLATE"
fmt.Println(d, "->", strutils.FormatCamelCaseText(d))

e := "Con_F_Key"
fmt.Println(e, "->", strutils.FormatCamelCaseText(e))

}

func TestGoogleTrans(t *testing.T) {
conf.InitConfig()

Expand Down
18 changes: 5 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- 针对代码注释进行格式化
- 去除 `//` `/*` `#` 之类的符号
- 去除回车, 多余空格等
- 驼峰命名法的变量和函数名翻译
- 划词翻译 (仅支持 Linux, 需要 xclip )
- 简约窗口(无菜单/设置等,只有简单卡片显示翻译结果)

Expand Down Expand Up @@ -56,7 +57,7 @@ sh build.sh
### Linux 划词翻译支持
- 安装 xclip
-`EzeTranslate -x` 命令设定快捷键
- `-x` 参数可以在程序启动之后直接调用 xclip 获取屏幕划词并翻译
- 此处 `-x` 参数可以在程序启动之后直接调用 xclip 获取屏幕划词并翻译


## 4. 待实现功能
Expand All @@ -67,22 +68,13 @@ sh build.sh
- All
- 窗口创建的时候位置跟随鼠标
- 鼠标移开时候自动退出
- 驼峰命名的变量和函数名编译
- 多个翻译任务的时候, 停止之前的任务
- ...

# 5. 程序截图

## Ubuntu 20.04 运行效果

| 启动页面 | 翻译效果 |
| ---- | ---- |
| ![windows](./res-static/screenshot/linux.png) | ![windows-2](./res-static/screenshot/linux2.png) |


## Windows 运行效果

| 启动页面 | 翻译效果 |
| ---- | ---- |
| ![windows](./res-static/screenshot/windows.png) | ![windows-2](./res-static/screenshot/windows-2.png) |

| 启动页面 | 翻译效果 | 迷你窗口 |
| ---- | ---- | ---- |
| ![linux](./res-static/screenshot/linux.png) | ![linux-2](./res-static/screenshot/linux2.png) | ![linux-3](./res-static/screenshot/linux3.png) |
4 changes: 3 additions & 1 deletion conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package conf

import (
"github.com/Ericwyn/EzeTranslate/log"
"github.com/Ericwyn/EzeTranslate/resource"
"github.com/Ericwyn/EzeTranslate/ui/resource"
"github.com/spf13/viper"
)

Expand All @@ -19,6 +19,7 @@ const ConfigKeyYouDaoTransAppSecret = "youdaoTransAppSecret"
const ConfigKeyFormatSpace = "formatSpace"
const ConfigKeyFormatCarriageReturn = "formatCarriageReturn"
const ConfigKeyFormatAnnotation = "formatAnnotation"
const ConfigKeyFormatCamelCase = "formatCamelCase"

// google, baidu,
const ConfigKeyTranslateSelect = "translateSelect"
Expand All @@ -39,6 +40,7 @@ func InitConfig() {
viper.SetDefault(ConfigKeyFormatSpace, false)
viper.SetDefault(ConfigKeyFormatCarriageReturn, false)
viper.SetDefault(ConfigKeyFormatAnnotation, false)
viper.SetDefault(ConfigKeyFormatCamelCase, false)

viper.SetConfigName("config")
viper.SetConfigType("yaml")
Expand Down
1 change: 1 addition & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ youdaoTransSecret: youdaoTransAppSecret-xxxxxxxxxxxxxxx
formatannotation: false
formatcarriagereturn: false
formatspace: false
formatcamelcase: false

# 翻译接口选择, 默认选择 google 接口
translateSelect: google
Expand Down
38 changes: 19 additions & 19 deletions ipc/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import (

type UnixSocket struct {
filename string
bufsize int
handler func(string) string
bufSize int
}

func NewUnixSocket(filename string, size ...int) *UnixSocket {
size1 := 10480
if size != nil {
size1 = size[0]
}
us := UnixSocket{filename: filename, bufsize: size1}
us := UnixSocket{filename: filename, bufSize: size1}
return &us
}

func (this *UnixSocket) createServer() {
os.Remove(this.filename)
addr, err := net.ResolveUnixAddr("unix", this.filename)
func (socket *UnixSocket) createServer() {
os.Remove(socket.filename)
addr, err := net.ResolveUnixAddr("unix", socket.filename)
if err != nil {
panic("Cannot resolve unix addr: " + err.Error())
}
Expand All @@ -40,48 +40,48 @@ func (this *UnixSocket) createServer() {
if err != nil {
panic("Accept: " + err.Error())
}
go this.HandleServerConn(c)
go socket.HandleServerConn(c)
}

}

//接收连接并处理
func (this *UnixSocket) HandleServerConn(c net.Conn) {
func (socket *UnixSocket) HandleServerConn(c net.Conn) {
defer c.Close()
buf := make([]byte, this.bufsize)
buf := make([]byte, socket.bufSize)
nr, err := c.Read(buf)
if err != nil {
panic("Read: " + err.Error())
}
// 这里,你需要 parse buf 里的数据来决定返回什么给客户端
// 假设 respnoseData 是你想返回的文件内容
result := this.HandleServerContext(string(buf[0:nr]))
result := socket.HandleServerContext(string(buf[0:nr]))
_, err = c.Write([]byte(result))
if err != nil {
panic("Writes failed.")
}
}

func (this *UnixSocket) SetContextHandler(f func(string) string) {
this.handler = f
func (socket *UnixSocket) SetContextHandler(f func(string) string) {
socket.handler = f
}

//接收内容并返回结果
func (this *UnixSocket) HandleServerContext(context string) string {
if this.handler != nil {
return this.handler(context)
func (socket *UnixSocket) HandleServerContext(context string) string {
if socket.handler != nil {
return socket.handler(context)
}
now := time.Now().String()
return now
}

func (this *UnixSocket) StartServer() {
this.createServer()
func (socket *UnixSocket) StartServer() {
socket.createServer()
}

//客户端
func (this *UnixSocket) ClientSendContext(context string) (string, error) {
addr, err := net.ResolveUnixAddr("unix", this.filename)
func (socket *UnixSocket) ClientSendContext(context string) (string, error) {
addr, err := net.ResolveUnixAddr("unix", socket.filename)
if err != nil {
log.D("Cannot resolve unix addr: " + err.Error())
return "", err
Expand All @@ -99,7 +99,7 @@ func (this *UnixSocket) ClientSendContext(context string) (string, error) {
return "", err
}
//读结果
buf := make([]byte, this.bufsize)
buf := make([]byte, socket.bufSize)
nr, err := c.Read(buf)
if err != nil {
log.D("Read: " + err.Error())
Expand Down
Binary file modified res-static/screenshot/linux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified res-static/screenshot/linux2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res-static/screenshot/linux3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions strutils/strutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,67 @@ func FormatInputBoxText(formatText string) string {
formatText = strings.ReplaceAll(formatText, " ", " ")
formatText = strings.Trim(formatText, " ")
}
if viper.GetBool(conf.ConfigKeyFormatCamelCase) {
// 因为 FormatCamelCaseText 会要求输入的 formatText 符合函数名格式才可以
formatText = FormatCamelCaseText(formatText)
}

return formatText
}

// 将一个驼峰命名的函数名拆开来
// 将第二个开始的大写字母拆成小写 + 空格
// 还得判断是否全为大写
func FormatCamelCaseText(str string) string {

// 判断是否含有空格(有空格或者是标点符号的话是一个句子,应该直接返回)
if strings.Contains(strings.Trim(str, " "), " ") ||
strings.Contains(str, ",") ||
strings.Contains(str, ".") {
return str
}

runeStr := []rune(strings.Trim(str, " "))

// 判断是否全为大写,如果是的话就直接返回,因为有可能是静态变量名
upperCount := 0
charCount := 0

for _, c := range runeStr {
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' {
charCount++
if (c >= 'A' && c <= 'Z') || c == '_' {
upperCount++
}
}
}

if upperCount == charCount {
// 全部都是大写字母和下划线,有可能是静态变量的命名
// 我们可以把大写字母全部转成小写,之后再拆开
runeStr = []rune(strings.ToLower(strings.Trim(str, " ")))
}

if charCount < len(runeStr) {
// 除了大小写和下划线之外还有别的字符,不做驼峰命名拆解,直接返回
return str
}

res := ""
for i, c := range runeStr {
if i == 0 {
res += string(c)
continue
}

if c >= 'A' && c <= 'Z' {
res += " " + strings.ToLower(string(c))
} else if c == '_' {
res += " "
} else {
res += string(c)
}
}

return res
}
2 changes: 1 addition & 1 deletion ui/about.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"github.com/Ericwyn/EzeTranslate/conf"
"github.com/Ericwyn/EzeTranslate/resource"
"github.com/Ericwyn/EzeTranslate/ui/resource"
"net/url"
)

Expand Down
68 changes: 68 additions & 0 deletions ui/assembly.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ui

import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"github.com/Ericwyn/EzeTranslate/conf"
"github.com/Ericwyn/EzeTranslate/log"
"github.com/Ericwyn/EzeTranslate/ui/resource/cusWidget"
"github.com/spf13/viper"
)

func buildFormatCheckBox() *fyne.Container {
return container.NewHBox(
widget.NewLabel("输入优化 "),
cusWidget.CreateCheckGroup(
[]cusWidget.LabelAndInit{
{"注释", viper.GetBool(conf.ConfigKeyFormatAnnotation)},
{"空格", viper.GetBool(conf.ConfigKeyFormatSpace)},
{"回车", viper.GetBool(conf.ConfigKeyFormatCarriageReturn)},
{"驼峰", viper.GetBool(conf.ConfigKeyFormatCamelCase)},
},
true, // 横向
false, // 单选
func(label string, checked bool) {
if label == "注释" {
viper.Set(conf.ConfigKeyFormatAnnotation, checked)
} else if label == "空格" {
viper.Set(conf.ConfigKeyFormatSpace, checked)
} else if label == "回车" {
viper.Set(conf.ConfigKeyFormatCarriageReturn, checked)
} else if label == "驼峰" {
viper.Set(conf.ConfigKeyFormatCamelCase, checked)
}
conf.SaveConfig()
},
),
)
}

func buildTransApiCheckBox() *fyne.Container {
return container.NewHBox(
widget.NewLabel("翻译结果 "),
cusWidget.CreateCheckGroup(
[]cusWidget.LabelAndInit{
{"Google", viper.GetString(conf.ConfigKeyTranslateSelect) == "google"},
{"Baidu", viper.GetString(conf.ConfigKeyTranslateSelect) == "baidu"},
{"Youdao", viper.GetString(conf.ConfigKeyTranslateSelect) == "youdao"},
},
true, // 横向
true, // 单选
func(label string, checked bool) {
if label == "Google" {
viper.Set(conf.ConfigKeyTranslateSelect, "google")
} else if label == "Baidu" {
viper.Set(conf.ConfigKeyTranslateSelect, "baidu")
} else if label == "Youdao" {
viper.Set(conf.ConfigKeyTranslateSelect, "youdao")
}
e := viper.WriteConfig()
if e != nil {
log.E("配置文件保存失败")
log.E(e)
}
},
),
)
}
Loading

0 comments on commit ef4cfc7

Please sign in to comment.