Skip to content

Commit

Permalink
add: get-trl
Browse files Browse the repository at this point in the history
  • Loading branch information
chen-ziwen committed Aug 17, 2024
1 parent 68d296b commit e593d5c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 16 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Commands:
set-trl [options] <name> 设置翻译平台访问渠道的应用ID和密钥
-a, --appid <appid> 设置翻译平台应用ID
-s, --secret-key <secretKey> 设置翻译平台密钥
get-trl [options] [name] 显示指定平台的应用ID和秘钥,不指定则显示当前选中平台
-s, --show 显示真正的秘钥
set-langs [options] 设置源语言和目标语言
-s, --source <source> 设置源语言
-t, --target <target> 设置目标语言
Expand All @@ -47,25 +49,29 @@ $ tlm set-trl baidu -a 123456 -s abcdefghijklmnopqrstuvwxyz
设置百度翻译平台的应用ID和密钥
$ tlm ls
$ tlm get-trl baidu -s
查看翻译平台列表和当前选中的平台
查看百度翻译平台的应用ID和秘钥
$ tlm use youdao
使用有道翻译平台进行翻译
使用有道翻译平台进行翻译
$ tlm ls
查看翻译平台列表和当前选中的平台
$ tlm ls langs
查看当前可以选择的源语言和目标语言代码
查看当前可以选择的源语言和目标语言代码
$ tlm set-langs -s en -t zh
设置翻译源语言为英语,目标语言为中文
设置翻译源语言为英语,目标语言为中文
$ tlm p hello world
tlm p 跟上需要翻译的文本内容
tlm p 跟上需要翻译的文本内容
```
## 支持平台

Expand Down
14 changes: 10 additions & 4 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Commands:
set-trl [options] <name> Set the appid and key for the translation platform to access the channel translation api
-a, --appid <appid> Set translation platform appid
-s, --secret-key <secretKey> Set translation platform secret key
get-trl [options] [name] Displays the appid and key of the specified platform, or the currently selected platform if not specified
-s, --show Displays the real secret key
set-langs [options] Set source and target languages
-s, --source <source> Set source language
-t, --target <target> Set target language
Expand All @@ -46,6 +48,14 @@ $ tlm set-trl baidu -a 123456 -s abcdefghijklmnopqrstuvwxyz
Set the application ID and key of Baidu Translation Platform
$ tlm get-trl baidu -s
View the app ID and key of Baidu Translate platform
$ tlm use youdao
Use Youdao translation platform for translation
$ tlm ls
View the list of translation platforms and the currently selected platform
Expand All @@ -54,10 +64,6 @@ $ tlm ls langs
View the currently available source and target language codes
$ tlm use youdao
Use Youdao translation platform for translation
$ tlm set-langs -s en -t zh
Set the translation source language to English and the target language to Chinese
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tlm",
"version": "0.0.2",
"version": "0.1.0",
"type": "module",
"description": "Terminal packages that support multiple translation platforms",
"main": "./dist/cli.js",
Expand Down
11 changes: 9 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
onList,
onUse,
onSetTranslation,
onGetTranslation,
onTranslate,
onSetTranslateLanguage
} from "@util/actions";
Expand Down Expand Up @@ -36,17 +37,23 @@ program
.description("Set the appid and key for the translation platform to access the channel translation api.")
.action(onSetTranslation);

program
.command("get-trl [name]")
.option("-s, --show", "Displays the real secret key")
.description("Displays the appid and key of the specified platform, or the currently selected platform if not specified.")
.action(onGetTranslation);

program
.command("set-langs")
.option("-s, --source <source>", "Set source language")
.option("-t, --target <target>", "Set target language")
.description("Set source and target languages")
.action(onSetTranslateLanguage)
.action(onSetTranslateLanguage);

program
.command("p")
.argument("<query...>")
.description("Translate the text using the 'tlm p <query...>' directive")
.action(onTranslate)
.action(onTranslate);

program.parse(process.argv);
8 changes: 7 additions & 1 deletion src/util/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
showPlatformList,
changePlatform,
changeLanguageCode,
setTranslation
setTranslation,
getTranslation
} from "@/util/controller";

async function onList(query: string) {
Expand All @@ -24,6 +25,10 @@ async function onSetTranslation(name: string, info: { appid: string, secretKey:
await setTranslation(name, info);
}

async function onGetTranslation(name: string, info: { show: boolean }) {
await getTranslation(name, info);
}

async function onTranslate(query: string[]) {
const txt = await Translator.translate(query);
if (txt) console.log(chalk.blue(txt));
Expand All @@ -37,6 +42,7 @@ export {
onList,
onUse,
onSetTranslation,
onGetTranslation,
onTranslate,
onSetTranslateLanguage
}
17 changes: 15 additions & 2 deletions src/util/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ async function showLanguageList(len = 14) {
const { source, target } = <Tl.Config>await readFile(TLMRC);
const map: { [key: string]: string } = { source, target };

console.log(`\n- ${chalk.blue('蓝色')}高亮文本为当前选中语种\n- ${chalk.red('红色')}高亮文本为当前不支持语种\n- 不同翻译平台的不同语种支持略有差异\n`);
console.log(`- ${chalk.blue('蓝色')}高亮文本为当前选中语种\n- ${chalk.red('红色')}高亮文本为当前不支持语种\n- 不同翻译平台的不同语种支持略有差异\n`);
console.log(`| ${stringFill(len, '源语言')} | ${stringFill(len, "目标语言")} |`);
console.log(`|${'-'.repeat(len + 2)}|${'-'.repeat(len + 2)}|`);
LANGUAGE_MAP.forEach(item => {
Expand Down Expand Up @@ -145,12 +145,25 @@ async function setTranslation(name: string, { appid, secretKey }: { appid: strin
successLog(`${plName}翻译平台成功设置应用ID和秘钥`);
}

async function getTranslation(name: string, { show }: { show: boolean }) {
const { pl, platform } = await getPlatformInfo();
name = name ?? pl;
if (await isTranslationPlatformNotFound(name)) return;
const [_, value] = platform.find(([key]) => key == name)!;
const prefix = value.name.split("-")[0] + "翻译";
const appid = value.appid || "暂未设置";
const key = value.key ? (show ? value.key : "*".repeat(value.key.length)) : "暂未设置";
const message = `${prefix}\n- 应用ID: ${appid}\n- 秘钥: ${key}`;
console.log(message);
}

export {
isTranslationPlatformNotFound,
languageListHandle,
showLanguageList,
showPlatformList,
changePlatform,
changeLanguageCode,
setTranslation
setTranslation,
getTranslation
}

0 comments on commit e593d5c

Please sign in to comment.