Skip to content

Commit

Permalink
feat: add variable naming feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Coder-ZJQ committed May 13, 2022
1 parent 8ad0b9b commit 7701bc0
Show file tree
Hide file tree
Showing 22 changed files with 121 additions and 104 deletions.
60 changes: 8 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,12 @@

# YoudaoTranslate | 有道翻译
# variable-naming.alfredworkflow

<p align="left">
<img alt="GitHub stars" src="https://visitor-badge.laobi.icu/badge?page_id=wensonsmith.YoudaoTranslate"/>
<img alt="GitHub stars" src="https://img.shields.io/github/stars/wensonsmith/YoudaoTranslate?style=social"/>
</p>

![screenshot_1](screenshots/screenshot_1.png)
-[YoudaoTranslator](https://github.com/wensonsmith/YoudaoTranslator) 的基础上,将翻译的文本生成变量名;
- 支持 `小驼峰``大驼峰``小写下划线``大写下划线` 四种变量命名规范;
- 如果翻译结果不理想可以 `cmd + y` 组合键 Quick Look 网页查看其它结果;
- 或者按住 `alt` 键回车,浏览器会打开 [Codelf](https://unbug.github.io/codelf/)
- 配置参考 [YoudaoTranslator Wiki](https://github.com/wensonsmith/YoudaoTranslator/wiki)
- 下载:[Releases](https://github.com/Coder-ZJQ/variable-naming.alfredworkflow/releases)

## ⚠️ V3 更新说明
该版本使用 TS 重构,自带运行环境,不再依赖 PHP。同时支持多个平台的 API。

macOS Monterey 请使用 V3 版本!

标记为施工中 (🚧) 特性 V3 尚未支持,如果需要使用,请切换到 V2 使用。

## 特性
- 🌟 [**无系统环境依赖**]() - 自带 [txiki](https://github.com/saghul/txiki.js) 运行环境,不再需要 PHP
- 🌟 [**多平台支持**]() - 支持百度的翻译API
- 🌐 [**中英文自动互翻**]() - 支持 `CamelCase` 驼峰短语翻译,长句自动换行
- 🎭 [**多语言支持**](screenshots/multi.jpg) - 可以识别中文、英文、日文、韩文、法文、俄文等
- 🎹 [**快捷键支持**]() - 双击 `⌥ Alt` 直接翻译选中内容
- 📢 [**英文发音**](screenshots/screenshot_3.png) - `⌘ Command` + `↩︎ Enter` 本地发音,`⌥ Alt` + `↩︎ Enter` 调用有道在线语音发音
- 🚧 [**有道翻译生词本**](screenshots/word-book.jpg) - 可以将陌生单词加入有道生词本
- 📃 [**回车复制**]() - 在选项上 `↩︎ Enter` 回车复制翻译结果
- 🚧 [**查询历史**](screenshots/translate_history.gif) - `yd *` 查询最近的翻译记录
- 🔮 [**网页预览**](screenshots/screenshot_4.gif) - 翻译结果上按 `⇧ Shift` 直接预览有道网页
- 🚧 [**自动更新**](screenshots/update.png) - 输入 `update` 检查更新 Workflow

## 🚀 开始使用

🌚 遇到问题不要怕,扫码加群来解答,[**点击扫码**](screenshots/wechat-group.png)

### 1. 下载安装

- [GitHub Releases 下载](https://github.com/wensonsmith/YoudaoTranslate/releases)
- [又拍云下载 v3.1.0](https://img.seekbetter.me/workflows/YoudaoTranslator-3.1.zip)

### 2. 配置Workflow

[👉 请参考 wiki 进行配置](https://github.com/wensonsmith/YoudaoTranslator/wiki)
## Contributors

<a href="https://iwenson.com" target="_blank"><img src="https://avatars1.githubusercontent.com/u/2544185?s=120&v=4" height="60"/></a>
<a href="https://blog.zthxxx.me" target="_blank"><img src="https://avatars0.githubusercontent.com/u/15135943?s=120&v=4" height="60"/></a>
<a href="https://www.zzaning.com/#/" target="_blank"><img src="https://avatars2.githubusercontent.com/u/12035097?s=88&u=7e419cd2eb7b9fec5ba061d8135c4875a4c32323&v=4" height="60"/></a>

## ~~Buy me a coffee~~ 🍼 冲奶粉!
![微信支付](./screenshots/sponsor.PNG)

## 使用库和参考资料

- https://github.com/joetannenbaum/alfred-workflow
- https://www.alfredapp.com/help/workflows/inputs/script-filter/json/
- https://www.alfredapp.com/help/workflows/
![](./screenshots/usage.gif)
Binary file modified assets/icon.png
100755 → 100644
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed assets/translate-say.png
Binary file not shown.
Binary file removed assets/translate.png
Binary file not shown.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "youdao.translator",
"version": "3.0.0",
"description": "Elegant translation tool",
"name": "alfred-workflow-variable-naming",
"version": "0.1.0",
"description": "Alfred Variable Naming Workflow",
"main": "index.js",
"scripts": {
"build": "rimraf dist && rollup -c rollup.config.js",
"test": "./runtime/quick ./dist/index.js"
},
"author": "Wenson",
"author": "jqz",
"license": "MIT",
"devDependencies": {
"rimraf": "^3.0.2",
Expand Down
Binary file removed screenshots/config-1.jpg
Binary file not shown.
Binary file removed screenshots/config-2.jpg
Binary file not shown.
Binary file removed screenshots/multi.jpg
Binary file not shown.
Binary file removed screenshots/screenshot_1.png
Binary file not shown.
Binary file removed screenshots/screenshot_3.png
Binary file not shown.
Binary file removed screenshots/screenshot_4.gif
Binary file not shown.
Binary file removed screenshots/sponsor.PNG
Binary file not shown.
Binary file removed screenshots/translate_history.gif
Binary file not shown.
Binary file removed screenshots/update.png
Binary file not shown.
Binary file added screenshots/usage.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed screenshots/wechat-group.png
Binary file not shown.
Binary file removed screenshots/word-book.jpg
Binary file not shown.
6 changes: 5 additions & 1 deletion src/adapters/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Formatter } from "./formatter";

export interface Result {
word: string;
title: string;
subtitle: string;
arg: string;
pronounce: string;
quicklookurl?: string;
}

Expand All @@ -15,6 +17,8 @@ export interface Adapter {

isChinese: boolean;

formatters: Formatter[]

url :(word: string) => string;

parse: (response: any) => Result[]
Expand Down
27 changes: 21 additions & 6 deletions src/adapters/baidu.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Adapter, Result } from "./adapter";
import md5 from "../libs/md5";
import { Formatter, LowerCamelCase, UpperCamelCase, LowerUnderline, UpperUnderline } from './formatter';

class Baidu implements Adapter {
key: string;
Expand All @@ -14,6 +15,13 @@ class Baidu implements Adapter {

phonetic: string = "";

formatters: Formatter[] = [
new LowerCamelCase(),
new UpperCamelCase(),
new LowerUnderline(),
new UpperUnderline()
]

constructor(key: string, secret: string) {
this.key = key;
this.secret = secret;
Expand Down Expand Up @@ -48,14 +56,20 @@ class Baidu implements Adapter {
}

const { trans_result:result } = data;
result.forEach(item => {
const pronounce = this.isChinese ? item.dst : this.word;
this.addResult(item.dst, item.src, pronounce, pronounce);
result.filter(this.isAllLetter).forEach(item => {
this.formatters.forEach(formatter => {
const formatted = formatter.format(item.dst)
this.addResult(formatted, formatter.name, formatted);
})
});

return this.results;
}

private isAllLetter(translation: string): boolean {
return /^[a-zA-Z ]+$/.test(translation)
}

private parseError(code: number): Result[] {
const messages = {
54000: "缺少必填的参数",
Expand All @@ -74,9 +88,10 @@ class Baidu implements Adapter {
return this.addResult("👻 翻译出错啦", message, "Ooops...");
}

private addResult( title: string, subtitle: string, arg: string = "", pronounce: string = ""): Result[] {
private addResult( title: string, subtitle: string, arg: string = ""): Result[] {
// quicklook 无法打开 codelf,所以展示使用翻译页面
// const quicklookurl = "https://unbug.github.io/codelf/#" + encodeURI(this.word);
const quicklookurl = "https://fanyi.baidu.com/#auto/auto/" + this.word;

const maxLength = this.detectChinese(title) ? 27 : 60;

if (title.length > maxLength) {
Expand All @@ -85,7 +100,7 @@ class Baidu implements Adapter {
subtitle = copy.slice(maxLength);
}

this.results.push({ title, subtitle, arg, pronounce, quicklookurl });
this.results.push({ word: this.word, title, subtitle, arg, quicklookurl });
return this.results;
}

Expand Down
45 changes: 45 additions & 0 deletions src/adapters/formatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
export interface Formatter {
name: string;
format: (word: string) => string;
}

export class LowerCamelCase implements Formatter {
name: string = "小驼峰"

format(word: string): string {
return word.split(" ").map((string, index) => {
if (index > 0) {
return string.slice(0, 1).toUpperCase() + string.slice(1).toLowerCase()
} else {
return string.toLowerCase()
}
}).join("")
}
}
export class UpperCamelCase implements Formatter {
name: string = "大驼峰"

format(word: string): string {
return word.split(" ").map((string, index) => {
return string.slice(0, 1).toUpperCase() + string.slice(1).toLowerCase()
}).join("")
}
}
export class LowerUnderline implements Formatter {
name: string = "小写下划线"

format(word: string): string {
return word.split(" ").map((string, index) => {
return string.toLowerCase()
}).join("_")
}
}
export class UpperUnderline implements Formatter {
name: string = "大写下划线"

format(word: string): string {
return word.split(" ").map((string, index) => {
return string.toUpperCase()
}).join("_")
}
}
73 changes: 36 additions & 37 deletions src/adapters/youdao.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Adapter, Result } from "./adapter";
import md5 from "../libs/md5";
import { Formatter, LowerCamelCase, UpperCamelCase, LowerUnderline, UpperUnderline } from './formatter';

class Youdao implements Adapter {
key: string;
Expand All @@ -14,6 +15,13 @@ class Youdao implements Adapter {

phonetic: string = "";

formatters: Formatter[] = [
new LowerCamelCase(),
new UpperCamelCase(),
new LowerUnderline(),
new UpperUnderline()
]

constructor(key: string, secret: string) {
this.key = key;
this.secret = secret;
Expand Down Expand Up @@ -47,61 +55,51 @@ class Youdao implements Adapter {

const { translation, basic, web } = data;

this.parseTranslation(translation);
this.parseBasic(basic);
this.parseWeb(web);
if (this.isChinese) {
// 因为是生成变量名(代码),普通翻译词义可能不怎么符合情况,所以将网络释义提前
this.parseWeb(web);
this.parseTranslation(translation);
this.parseBasic(basic);
}

return this.results;
}

private parseTranslation(translation: object) {
if (translation) {
const pronounce = this.isChinese ? translation[0] : this.word;
this.addResult( translation[0], this.word, translation[0], pronounce );
if (translation && this.isAllLetter(translation[0])) {
this.formatters.forEach(formatter => {
const formatted = formatter.format(translation[0])
this.addResult(formatted, formatter.name, formatted);
})
}
}

private parseBasic(basic: any) {
if (basic) {
let pronounce;
basic.explains.forEach((explain) => {
pronounce = this.isChinese ? explain : this.word;
this.addResult(explain, this.word, explain, pronounce);
basic.explains.filter(this.isAllLetter).forEach((explain) => {
this.formatters.forEach(formatter => {
const formatted = formatter.format(explain)
this.addResult(formatted, formatter.name, formatted);
})
});

if (basic.phonetic) {
// 获取音标,同时确定要发音的单词
const phonetic: string = this.parsePhonetic(basic);
this.addResult( phonetic, "回车可听发音", "~" + pronounce, pronounce );
}
}
}

private parseWeb(web: any) {
if (web) {
web.forEach((item, index) => {
let pronounce = this.isChinese ? item.value[0] : item.key;
this.addResult( item.value.join(", "), item.key, item.value[0], pronounce);
item.value.filter(this.isAllLetter).forEach(element => {
this.formatters.forEach(formatter => {
const formatted = formatter.format(element)
this.addResult(formatted, formatter.name, formatted);
})
});
});
}
}

private parsePhonetic(basic: any): string {
let phonetic: string = '';

if (this.isChinese && basic.phonetic) {
phonetic = "[" + basic.phonetic + "] ";
}

if (basic["us-phonetic"]) {
phonetic += " [美: " + basic["us-phonetic"] + "] ";
}

if (basic["uk-phonetic"]) {
phonetic += " [英: " + basic["uk-phonetic"] + "]";
}

return phonetic;
private isAllLetter(translation: string): boolean {
return /^[a-zA-Z ]+$/.test(translation)
}

private parseError(code: number): Result[] {
Expand All @@ -124,9 +122,10 @@ class Youdao implements Adapter {
return this.addResult("👻 翻译出错啦", message, "Ooops...");
}

private addResult( title: string, subtitle: string, arg: string = "", pronounce: string = ""): Result[] {
private addResult( title: string, subtitle: string, arg: string = ""): Result[] {
// quicklook 无法打开 codelf,所以展示使用翻译页面
// const quicklookurl = "https://unbug.github.io/codelf/#" + encodeURI(this.word);
const quicklookurl = "https://www.youdao.com/w/" + this.word;

const maxLength = this.detectChinese(title) ? 27 : 60;

if (title.length > maxLength) {
Expand All @@ -135,7 +134,7 @@ class Youdao implements Adapter {
subtitle = copy.slice(maxLength);
}

this.results.push({ title, subtitle, arg, pronounce, quicklookurl });
this.results.push({ word: this.word, title, subtitle, arg, quicklookurl });
return this.results;
}

Expand Down
6 changes: 2 additions & 4 deletions src/workflow/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ class Workflow {

compose(results: Result[]): this {
this.results = results.map(r => {
const icon = r.arg.startsWith("~") ? 'assets/translate-say.png' : 'assets/translate.png'

return new Item().setTitle(r.title)
.setSubtitle(r.subtitle)
.setArg(r.arg)
.setIcon(icon)
.setCmd('🔊 ' + r.pronounce, r.pronounce)
.setAlt('📣 ' + r.pronounce, r.pronounce)
.setCopy(r.title)
.setAlt("https://unbug.github.io/codelf/#" + r.word, r.word)
.setQuicklookurl(r.quicklookurl)
.result();
});
Expand Down

0 comments on commit 7701bc0

Please sign in to comment.