Skip to content

Commit e593d5c

Browse files
committed
add: get-trl
1 parent 68d296b commit e593d5c

File tree

6 files changed

+54
-16
lines changed

6 files changed

+54
-16
lines changed

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Commands:
3333
set-trl [options] <name> 设置翻译平台访问渠道的应用ID和密钥
3434
-a, --appid <appid> 设置翻译平台应用ID
3535
-s, --secret-key <secretKey> 设置翻译平台密钥
36+
get-trl [options] [name] 显示指定平台的应用ID和秘钥,不指定则显示当前选中平台
37+
-s, --show 显示真正的秘钥
3638
set-langs [options] 设置源语言和目标语言
3739
-s, --source <source> 设置源语言
3840
-t, --target <target> 设置目标语言
@@ -47,25 +49,29 @@ $ tlm set-trl baidu -a 123456 -s abcdefghijklmnopqrstuvwxyz
4749
4850
设置百度翻译平台的应用ID和密钥
4951
50-
$ tlm ls
52+
$ tlm get-trl baidu -s
5153
52-
查看翻译平台列表和当前选中的平台
54+
查看百度翻译平台的应用ID和秘钥
5355
5456
$ tlm use youdao
5557
56-
使用有道翻译平台进行翻译
58+
使用有道翻译平台进行翻译
59+
60+
$ tlm ls
61+
62+
查看翻译平台列表和当前选中的平台
5763
5864
$ tlm ls langs
5965
60-
查看当前可以选择的源语言和目标语言代码
66+
查看当前可以选择的源语言和目标语言代码
6167
6268
$ tlm set-langs -s en -t zh
6369
64-
设置翻译源语言为英语,目标语言为中文
70+
设置翻译源语言为英语,目标语言为中文
6571
6672
$ tlm p hello world
6773
68-
tlm p 跟上需要翻译的文本内容
74+
tlm p 跟上需要翻译的文本内容
6975
```
7076
## 支持平台
7177

README_EN.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ Commands:
3333
set-trl [options] <name> Set the appid and key for the translation platform to access the channel translation api
3434
-a, --appid <appid> Set translation platform appid
3535
-s, --secret-key <secretKey> Set translation platform secret key
36+
get-trl [options] [name] Displays the appid and key of the specified platform, or the currently selected platform if not specified
37+
-s, --show Displays the real secret key
3638
set-langs [options] Set source and target languages
3739
-s, --source <source> Set source language
3840
-t, --target <target> Set target language
@@ -46,6 +48,14 @@ $ tlm set-trl baidu -a 123456 -s abcdefghijklmnopqrstuvwxyz
4648
4749
Set the application ID and key of Baidu Translation Platform
4850
51+
$ tlm get-trl baidu -s
52+
53+
View the app ID and key of Baidu Translate platform
54+
55+
$ tlm use youdao
56+
57+
Use Youdao translation platform for translation
58+
4959
$ tlm ls
5060
5161
View the list of translation platforms and the currently selected platform
@@ -54,10 +64,6 @@ $ tlm ls langs
5464
5565
View the currently available source and target language codes
5666
57-
$ tlm use youdao
58-
59-
Use Youdao translation platform for translation
60-
6167
$ tlm set-langs -s en -t zh
6268
6369
Set the translation source language to English and the target language to Chinese

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tlm",
3-
"version": "0.0.2",
3+
"version": "0.1.0",
44
"type": "module",
55
"description": "Terminal packages that support multiple translation platforms",
66
"main": "./dist/cli.js",

src/cli.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
onList,
88
onUse,
99
onSetTranslation,
10+
onGetTranslation,
1011
onTranslate,
1112
onSetTranslateLanguage
1213
} from "@util/actions";
@@ -36,17 +37,23 @@ program
3637
.description("Set the appid and key for the translation platform to access the channel translation api.")
3738
.action(onSetTranslation);
3839

40+
program
41+
.command("get-trl [name]")
42+
.option("-s, --show", "Displays the real secret key")
43+
.description("Displays the appid and key of the specified platform, or the currently selected platform if not specified.")
44+
.action(onGetTranslation);
45+
3946
program
4047
.command("set-langs")
4148
.option("-s, --source <source>", "Set source language")
4249
.option("-t, --target <target>", "Set target language")
4350
.description("Set source and target languages")
44-
.action(onSetTranslateLanguage)
51+
.action(onSetTranslateLanguage);
4552

4653
program
4754
.command("p")
4855
.argument("<query...>")
4956
.description("Translate the text using the 'tlm p <query...>' directive")
50-
.action(onTranslate)
57+
.action(onTranslate);
5158

5259
program.parse(process.argv);

src/util/actions.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import {
55
showPlatformList,
66
changePlatform,
77
changeLanguageCode,
8-
setTranslation
8+
setTranslation,
9+
getTranslation
910
} from "@/util/controller";
1011

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

28+
async function onGetTranslation(name: string, info: { show: boolean }) {
29+
await getTranslation(name, info);
30+
}
31+
2732
async function onTranslate(query: string[]) {
2833
const txt = await Translator.translate(query);
2934
if (txt) console.log(chalk.blue(txt));
@@ -37,6 +42,7 @@ export {
3742
onList,
3843
onUse,
3944
onSetTranslation,
45+
onGetTranslation,
4046
onTranslate,
4147
onSetTranslateLanguage
4248
}

src/util/controller.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ async function showLanguageList(len = 14) {
7272
const { source, target } = <Tl.Config>await readFile(TLMRC);
7373
const map: { [key: string]: string } = { source, target };
7474

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

148+
async function getTranslation(name: string, { show }: { show: boolean }) {
149+
const { pl, platform } = await getPlatformInfo();
150+
name = name ?? pl;
151+
if (await isTranslationPlatformNotFound(name)) return;
152+
const [_, value] = platform.find(([key]) => key == name)!;
153+
const prefix = value.name.split("-")[0] + "翻译";
154+
const appid = value.appid || "暂未设置";
155+
const key = value.key ? (show ? value.key : "*".repeat(value.key.length)) : "暂未设置";
156+
const message = `${prefix}\n- 应用ID: ${appid}\n- 秘钥: ${key}`;
157+
console.log(message);
158+
}
159+
148160
export {
149161
isTranslationPlatformNotFound,
150162
languageListHandle,
151163
showLanguageList,
152164
showPlatformList,
153165
changePlatform,
154166
changeLanguageCode,
155-
setTranslation
167+
setTranslation,
168+
getTranslation
156169
}

0 commit comments

Comments
 (0)