html-i18n-cli
Advanced tools
Comparing version 0.1.3 to 0.2.0
@@ -22,3 +22,5 @@ #!/usr/bin/env node | ||
.option('-t, --to', '目标语言', 'zh') | ||
.option('-n, --number', '每 n 条数据保存一次', 50) | ||
.option('-n, --number', '每 n 条数据保存一次', 20) | ||
.option('-a, --appid', '百度翻译 APPID', '20200424000428633') | ||
.option('-s, --secret', '百度翻译 SECRET', 'ptBeUtXqXC0WE4vQUWcx') | ||
.description('翻译 JSON 文件') | ||
@@ -25,0 +27,0 @@ .action((file, options) => { |
@@ -10,5 +10,5 @@ const fs = require('fs') | ||
const { texts } = html2texts(html) | ||
const localTexts = texts.map(({ text }) => { | ||
return { origin: text, local: '' } | ||
}) | ||
const localTexts = texts.reduce((result, { text }) => { | ||
return { ...result, [text]: '' } | ||
}, {}) | ||
@@ -15,0 +15,0 @@ fs.writeFile(outpath, JSON.stringify(localTexts, null, 2), (err) => { |
@@ -6,2 +6,3 @@ const fs = require('fs') | ||
const html2texts = require('./utils/html2texts') | ||
const file2texts = require('./utils/file2texts') | ||
@@ -11,10 +12,10 @@ module.exports = (htmlfile, jsonfile) => { | ||
const textspath = path.join(process.cwd(), jsonfile) | ||
const outpath = path.join(process.cwd(), `translated-${htmlfile}`) | ||
const outpath = path.join(process.cwd(), path.dirname(htmlfile), `translated-${path.basename(htmlfile)}`) | ||
const html = fs.readFileSync(htmlpath).toString() | ||
const texts = require(textspath) | ||
const texts = file2texts(textspath) | ||
const { texts: originTexts, tree } = html2texts(html) | ||
originTexts.forEach((item, index) => { | ||
lodash.set(tree, item.paths, texts[index].local) | ||
lodash.set(tree, item.paths, texts[index] && texts[index].local) | ||
}) | ||
@@ -21,0 +22,0 @@ |
const fs = require('fs') | ||
const path = require('path') | ||
const { program } = require('commander') | ||
const fanyi = require('./utils/fanyi') | ||
const file2texts = require('./utils/file2texts') | ||
module.exports = (file, options) => { | ||
const filepath = path.join(process.cwd(), file) | ||
const texts = require(filepath) | ||
const texts = file2texts(filepath) | ||
const total = texts.length | ||
const transMap = new Map | ||
const transMap = new Map() | ||
const writeFile = (data) => { | ||
fs.writeFile(filepath, JSON.stringify(data, null, 2), (err) => { | ||
const texts = data.reduce((result, { local, origin }) => { | ||
return { ...result, [origin]: local } | ||
}, {}) | ||
fs.writeFile(filepath, JSON.stringify(texts, null, 2), (err) => { | ||
if (err) console.log(err) | ||
@@ -19,3 +22,3 @@ console.log('译文已更新\n') | ||
async function translation(index) { | ||
async function translation (index) { | ||
if (index <= texts.length) { | ||
@@ -37,3 +40,3 @@ const current = texts[index - 1] | ||
} else { | ||
current.local = await fanyi(origin, options.to) | ||
current.local = await fanyi(origin, options.to, options.appid, options.secret) | ||
transMap.set(origin, current.local) | ||
@@ -48,3 +51,3 @@ setTimeout(() => translation(index + 1), 1000) | ||
const { number } = options | ||
const { number = 20 } = options | ||
if (!(index % number)) writeFile(texts) | ||
@@ -51,0 +54,0 @@ } |
const axios = require('axios') | ||
const SparkMD5 = require('spark-md5') | ||
const appid = '20200424000428633' | ||
const secret = 'ptBeUtXqXC0WE4vQUWcx' | ||
module.exports = (word, to = 'zh') => { | ||
module.exports = (word, to = 'zh', appid = '20200424000428633', secret = 'ptBeUtXqXC0WE4vQUWcx') => { | ||
const encodeWord = encodeURIComponent(word) | ||
@@ -16,7 +13,4 @@ const salt = Date.now() | ||
const { trans_result = [] } = res.data | ||
const first_result = trans_result[0] | ||
if (!first_result) return '' | ||
return first_result.dst | ||
const firstResult = trans_result[0] | ||
return firstResult ? firstResult.dst : '' | ||
}) | ||
@@ -23,0 +17,0 @@ .catch(() => { |
@@ -49,3 +49,3 @@ const dayjs = require('dayjs') | ||
return { tree, texts} | ||
return { tree, texts } | ||
} |
{ | ||
"name": "html-i18n-cli", | ||
"version": "0.1.3", | ||
"version": "0.2.0", | ||
"description": "HTML 国际化工具", | ||
@@ -10,2 +10,3 @@ "main": "lib/index.js", | ||
"scripts": { | ||
"lint": "./node_modules/.bin/eslint **/*.js", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
@@ -15,3 +16,3 @@ }, | ||
"dependencies": { | ||
"axios": "^0.19.2", | ||
"axios": "^0.27.2", | ||
"commander": "^5.1.0", | ||
@@ -25,3 +26,11 @@ "convert-excel-to-json": "^1.6.1", | ||
"spark-md5": "^3.0.1" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^6.8.0", | ||
"eslint-config-standard": "^14.1.1", | ||
"eslint-plugin-import": "^2.20.2", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^4.2.1", | ||
"eslint-plugin-standard": "^4.0.1" | ||
} | ||
} |
@@ -29,2 +29,4 @@ # html-i18n-cli | ||
* -n 50 指定更新频率 | ||
* --appid 百度翻译 APPID | ||
* --serret 百度翻译 SECRET | ||
@@ -50,1 +52,6 @@ 3. 还原 HTML 文件 | ||
``` | ||
## 相关项目 | ||
* [VSCode 插件](https://github.com/F-loat/vscode-plugin-html-i18n) | ||
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
20566
13
224
56
7
6
+ Addedasynckit@0.4.0(transitive)
+ Addedaxios@0.27.2(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedfollow-redirects@1.15.9(transitive)
+ Addedform-data@4.0.1(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
- Removedaxios@0.19.2(transitive)
- Removedfollow-redirects@1.5.10(transitive)
Updatedaxios@^0.27.2