├── lib ├── utils │ ├── md5.js │ ├── format.js │ ├── request.js │ └── tran-data.js └── core │ ├── command.js │ ├── config.js │ └── action.js ├── index.js ├── package.json ├── LICENSE ├── README.md └── .gitignore /lib/utils/md5.js: -------------------------------------------------------------------------------- 1 | const crypto = require('crypto') 2 | 3 | const md5String = (message) => { 4 | const md5 = crypto.createHash('md5') 5 | const result = md5.update(message).digest('hex') 6 | return result 7 | } 8 | 9 | module.exports = { 10 | md5String 11 | } 12 | -------------------------------------------------------------------------------- /lib/core/command.js: -------------------------------------------------------------------------------- 1 | const { program } = require('commander'); 2 | const { translationAcion } = require('./action'); 3 | const registerCommands = () => { 4 | program.argument('', '需要翻译的信息').action(translationAcion); 5 | }; 6 | 7 | module.exports = { 8 | registerCommands 9 | }; 10 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | const { program } = require("commander") 3 | const { registerCommands } = require('./lib/core/command') 4 | program.version(require('./package.json').version, '-v, --version', '打印版本号') 5 | program.helpOption('-h, --help','帮助信息') 6 | registerCommands() 7 | program.parse(process.argv) -------------------------------------------------------------------------------- /lib/core/config.js: -------------------------------------------------------------------------------- 1 | const translationBase = { 2 | youdao: { 3 | dict: 'https://dict.youdao.com' 4 | }, 5 | baidu: { 6 | sentence: 'https://fanyi-api.baidu.com/', 7 | appid: '20220626001257099', 8 | salt: '121', 9 | key: 'UzAECNbrxoNxLZSF9bnk' 10 | } 11 | }; 12 | const TIMEOUT = 10000; 13 | module.exports = { 14 | translationBase, 15 | TIMEOUT 16 | }; 17 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "telper", 3 | "version": "0.0.1", 4 | "description": "", 5 | "main": "index.js", 6 | "bin": { 7 | "telp": "index.js", 8 | "tp":"index.js" 9 | }, 10 | "directories": { 11 | "lib": "lib" 12 | }, 13 | "scripts": { 14 | "test": "echo \"Error: no test specified\" && exit 1" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "git+https://github.com/loclink/telper.git" 19 | }, 20 | "keywords": [], 21 | "author": "", 22 | "license": "ISC", 23 | "bugs": { 24 | "url": "https://github.com/loclink/telper/issues" 25 | }, 26 | "homepage": "https://github.com/loclink/telper#readme", 27 | "dependencies": { 28 | "axios": "^0.27.2", 29 | "chalk": "^4.1.2", 30 | "commander": "^9.3.0", 31 | "ora": "^5.4.1", 32 | "qs": "^6.10.5" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /lib/utils/format.js: -------------------------------------------------------------------------------- 1 | const chalk = require('chalk'); 2 | const formatDictReslut = data => { 3 | let resultMessge = ''; 4 | data.map((item, index) => { 5 | const result = item.explain.split('; '); 6 | let explainString = ''; 7 | result.map((item, indey) => { 8 | explainString += chalk.yellow(item) + (indey + 1 < result.length ? chalk.green(' | ') : ''); 9 | }); 10 | resultMessge += `${chalk.blue(item.entry)}: ${explainString}` + (index + 1 < data.length ? '\n' : ''); 11 | }); 12 | return resultMessge; 13 | }; 14 | 15 | const formatSentenceResult = data => { 16 | let resultMessage = ''; 17 | let index = 1; 18 | 19 | data.forEach(item => { 20 | resultMessage += chalk.blue(item.src) + ': ' + chalk.yellow(item.dst) + (index < data.length ? '\n' : ''); 21 | index++; 22 | }); 23 | return resultMessage; 24 | }; 25 | module.exports = { 26 | formatDictReslut, 27 | formatSentenceResult 28 | }; 29 | -------------------------------------------------------------------------------- /lib/utils/request.js: -------------------------------------------------------------------------------- 1 | const axios = require('axios').default; 2 | const { translationBase, TIMEOUT } = require('../core/config'); 3 | 4 | const ydDictInstance = axios.create({ 5 | baseURL: translationBase.youdao.dict, 6 | timeout: TIMEOUT 7 | }); 8 | 9 | ydDictInstance.interceptors.response.use( 10 | res => { 11 | return res.data; 12 | }, 13 | err => { 14 | console.log(err); 15 | return err; 16 | } 17 | ); 18 | 19 | const bdSentenceInstance = axios.create({ 20 | baseURL: translationBase.baidu.sentence, 21 | timeout: TIMEOUT, 22 | headers: { 23 | 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' 24 | } 25 | }); 26 | 27 | bdSentenceInstance.interceptors.request.use(config => { 28 | return config; 29 | }); 30 | 31 | bdSentenceInstance.interceptors.response.use(res => { 32 | return res.data; 33 | }); 34 | 35 | module.exports = { 36 | ydDictInstance, 37 | bdSentenceInstance 38 | }; 39 | -------------------------------------------------------------------------------- /lib/utils/tran-data.js: -------------------------------------------------------------------------------- 1 | const qs = require('qs'); 2 | const { ydDictInstance, bdSentenceInstance } = require('./request'); 3 | const { translationBase } = require('../core/config'); 4 | const { md5String } = require('../utils/md5'); 5 | 6 | const getDictData = params => { 7 | return ydDictInstance.get('suggest', { 8 | params: { 9 | ver: 3.0, 10 | doctype: 'json', 11 | cache: false, 12 | le: 'en', 13 | num: 3, 14 | ...params 15 | } 16 | }); 17 | }; 18 | // sentence: 'https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule' 19 | 20 | const getSentenceData = data => { 21 | const { baidu } = translationBase; 22 | const md5 = md5String(baidu.appid + data.q + baidu.salt + baidu.key); 23 | 24 | return bdSentenceInstance.post( 25 | 'api/trans/vip/translate', 26 | qs.stringify({ 27 | from: 'auto', 28 | to: 'auto', 29 | appid: translationBase.baidu.appid, 30 | salt: translationBase.baidu.salt, 31 | sign: md5, 32 | ...data 33 | }) 34 | ); 35 | }; 36 | module.exports = { 37 | getDictData, 38 | getSentenceData 39 | }; 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Loclink 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /lib/core/action.js: -------------------------------------------------------------------------------- 1 | const ora = require('ora'); 2 | const chalk = require('chalk'); 3 | const { getDictData, getSentenceData } = require('../utils/tran-data'); 4 | const { formatDictReslut, formatSentenceResult } = require('../utils/format'); 5 | 6 | const translationAcion = async message => { 7 | const loading = ora('开始查询').start(); 8 | 9 | getDictData({ q: message }) 10 | .then(res => { 11 | if (res.result.code === 200) { 12 | const result = formatDictReslut(res.data.entries); 13 | loading.succeed(chalk.green('查询成功:')); 14 | console.log(); 15 | console.log(result); 16 | console.log(); 17 | console.log(); 18 | } else if (res.result.code === 404) { 19 | getSentenceData({ q: message }) 20 | .then(res => { 21 | loading.succeed(chalk.green('查询成功:')); 22 | const result = formatSentenceResult(res.trans_result); 23 | console.log(); 24 | console.log(result); 25 | console.log(); 26 | console.log(); 27 | }) 28 | .catch(err => { 29 | loading.fail(chalk.red('查询失败:')); 30 | console.log(err); 31 | }); 32 | } 33 | }) 34 | .catch(err => { 35 | loading.fail(chalk.red('查询失败:')); 36 | console.log(err); 37 | }); 38 | }; 39 | 40 | module.exports = { 41 | translationAcion 42 | }; 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Telper 2 | 3 | > 你是否还在为起变量名而烦恼?不想窗口切来切去,拒绝臃肿的窗口程序,使用`telper`翻译工具,将撸码变成一种享受。 4 | > 5 | > `telper`是一款运行在终端的翻译软件,它将在你coding时祝你一臂之力。 6 | 7 | ![Peek 2022-06-26 14-03](https://tvax3.sinaimg.cn/large/0087ufIQgy1h3lm1b4a2dg30wp0mpk43.gif) 8 | 9 | ### 一、安装: 10 | 11 | - 软件的开发环境依赖于`node v16.15.1` 12 | - 请使用`16.15.1`版本运行该程序 13 | 14 | 使用`npm`包管理工具进行全局安装: 15 | 16 | ``` shell 17 | npm install telper -g 18 | ``` 19 | 20 | ### 二、使用: 21 | 22 | 1. 翻译文字,`tp `,支持多语言翻译,多国语言自动识别,开箱即用。 23 | 24 | ``` shell 25 | ➜ telper git:(main) ✗ tp promise 26 | ✔ 查询成功: 27 | 28 | promise: v. 承诺,保证;使很可能,预示;指望,期待(promoise oneself);<古>把(某人,尤... 29 | promised: 预示 | 答应 30 | promises: v. 允诺 | n. 承诺,诺言 31 | ``` 32 | 33 | 2. 如果你希望使用英转中,并且语句中存在空格,那么则需要将该语句用`""`或者`''`包裹起来: 34 | 35 | ![Peek 2022-06-26 14-54](https://tva1.sinaimg.cn/large/0087ufIQgy1h3lnhmihneg30wp0fljx5.gif) 36 | 37 | 3. 查看帮助: 38 | 39 | ``` shell 40 | ➜ telper git:(main) ✗ tp -h 41 | Usage: tp [options] 42 | 43 | Arguments: 44 | message 需要翻译的信息 45 | 46 | Options: 47 | -v, --version 打印版本号 48 | -h, --help 帮助信息 49 | ``` 50 | 51 | 4. 你还可以使用完整的指令:`telp` 来完成以上所有操作,`tp`指令为完整指令的语法糖,如果你的终端有指令与之冲突,那么建议你使用完整指令。 52 | 53 | ``` shell 54 | ➜ telper git:(main) ✗ telp 代码 55 | ✔ 查询成功: 56 | 57 | 代码: code | word 58 | 代码重构: code refactoring 59 | 代码片段: code snippet 60 | ``` 61 | 62 | ### 三、关于: 63 | 64 | 1. 词典数据来源-有道词典 65 | 2. 翻译数据来源-百度翻译 66 | 67 | ### 四、归档: 68 | 69 | - v0.0.1(2022/6/26): 70 | 1. 接入有道词典,实现字典查询 71 | 2. 接入百度翻译,实现语句翻译 72 | 3. 查询结果着色 73 | 4. 查询过程展示加载动画效果 74 | 75 | *如果你觉得本工具有帮助到你,就留下一个star吧~~本项目开源,欢迎一起学习交流* -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.toptal.com/developers/gitignore/api/node 2 | # Edit at https://www.toptal.com/developers/gitignore?templates=node 3 | package-lock.json 4 | ### Node ### 5 | # Logs 6 | logs 7 | *.log 8 | npm-debug.log* 9 | yarn-debug.log* 10 | yarn-error.log* 11 | lerna-debug.log* 12 | .pnpm-debug.log* 13 | 14 | # Diagnostic reports (https://nodejs.org/api/report.html) 15 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 16 | 17 | # Runtime data 18 | pids 19 | *.pid 20 | *.seed 21 | *.pid.lock 22 | 23 | # Directory for instrumented libs generated by jscoverage/JSCover 24 | lib-cov 25 | 26 | # Coverage directory used by tools like istanbul 27 | coverage 28 | *.lcov 29 | 30 | # nyc test coverage 31 | .nyc_output 32 | 33 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 34 | .grunt 35 | 36 | # Bower dependency directory (https://bower.io/) 37 | bower_components 38 | 39 | # node-waf configuration 40 | .lock-wscript 41 | 42 | # Compiled binary addons (https://nodejs.org/api/addons.html) 43 | build/Release 44 | 45 | # Dependency directories 46 | node_modules/ 47 | jspm_packages/ 48 | 49 | # Snowpack dependency directory (https://snowpack.dev/) 50 | web_modules/ 51 | 52 | # TypeScript cache 53 | *.tsbuildinfo 54 | 55 | # Optional npm cache directory 56 | .npm 57 | 58 | # Optional eslint cache 59 | .eslintcache 60 | 61 | # Optional stylelint cache 62 | .stylelintcache 63 | 64 | # Microbundle cache 65 | .rpt2_cache/ 66 | .rts2_cache_cjs/ 67 | .rts2_cache_es/ 68 | .rts2_cache_umd/ 69 | 70 | # Optional REPL history 71 | .node_repl_history 72 | 73 | # Output of 'npm pack' 74 | *.tgz 75 | 76 | # Yarn Integrity file 77 | .yarn-integrity 78 | 79 | # dotenv environment variable files 80 | .env 81 | .env.development.local 82 | .env.test.local 83 | .env.production.local 84 | .env.local 85 | 86 | # parcel-bundler cache (https://parceljs.org/) 87 | .cache 88 | .parcel-cache 89 | 90 | # Next.js build output 91 | .next 92 | out 93 | 94 | # Nuxt.js build / generate output 95 | .nuxt 96 | dist 97 | 98 | # Gatsby files 99 | .cache/ 100 | # Comment in the public line in if your project uses Gatsby and not Next.js 101 | # https://nextjs.org/blog/next-9-1#public-directory-support 102 | # public 103 | 104 | # vuepress build output 105 | .vuepress/dist 106 | 107 | # vuepress v2.x temp and cache directory 108 | .temp 109 | 110 | # Docusaurus cache and generated files 111 | .docusaurus 112 | 113 | # Serverless directories 114 | .serverless/ 115 | 116 | # FuseBox cache 117 | .fusebox/ 118 | 119 | # DynamoDB Local files 120 | .dynamodb/ 121 | 122 | # TernJS port file 123 | .tern-port 124 | 125 | # Stores VSCode versions used for testing VSCode extensions 126 | .vscode-test 127 | 128 | # yarn v2 129 | .yarn/cache 130 | .yarn/unplugged 131 | .yarn/build-state.yml 132 | .yarn/install-state.gz 133 | .pnp.* 134 | 135 | ### Node Patch ### 136 | # Serverless Webpack directories 137 | .webpack/ 138 | 139 | # Optional stylelint cache 140 | 141 | # SvelteKit build / generate output 142 | .svelte-kit 143 | 144 | # End of https://www.toptal.com/developers/gitignore/api/node 145 | --------------------------------------------------------------------------------