From 9db8827d4a98e6dfd50f984ce3e0215192346ae4 Mon Sep 17 00:00:00 2001 From: zuiidea Date: Wed, 17 Oct 2018 23:51:39 +0800 Subject: [PATCH] Add automatic translation scripts. --- package.json | 2 + scripts/translate.js | 102 +++++++++++++++++++++++++++++++++++ src/locales/zh/messages.json | 2 +- yarn.lock | 22 +++++++- 4 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 scripts/translate.js diff --git a/package.json b/package.json index 4c41bfc..de36859 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,9 @@ "enquire-js": "^0.2.1", "highcharts-exporting": "^0.1.7", "highcharts-more": "^0.1.7", + "json-format": "^1.0.1", "lodash": "^4.17.11", + "md5": "^2.2.1", "nprogress": "^0.2.0", "path-to-regexp": "^2.4.0", "prop-types": "^15.6.2", diff --git a/scripts/translate.js b/scripts/translate.js new file mode 100644 index 0000000..5980b43 --- /dev/null +++ b/scripts/translate.js @@ -0,0 +1,102 @@ +/** + * Youdao Translate, My private account is for testing purposes only. + * Please go to the official account to apply for an account. Thank you for your cooperation. + * APP ID:055c2d71f9a05214 + * Secret key:ZcpuQxQW3NkQeKVkqrXIKQYXH57g2KuN + */ + +/* eslint-disable */ +const md5 = require('md5') +const qs = require('qs') +const fs = require('fs') +const path = require('path') +const axios = require('axios') +const jsonFormat = require('json-format') +const { i18n } = require('../src/utils/config') +const { languages, defaultLanguage } = i18n + +const locales = {} + +languages.forEach(item => { + locales[item.key] = require(`../src/locales/${item.key}/messages.json`) +}) + +const youdao = ({ q, from, to }) => + new Promise((resolve, reject) => { + { + const appid = '055c2d71f9a05214' + const appse = 'ZcpuQxQW3NkQeKVkqrXIKQYXH57g2KuN' + const salt = Date.now() + + const sign = md5(appid + q + salt + appse) + const query = qs.stringify({ + q, + from, + to, + appKey: appid, + salt, + sign, + }) + + axios.get(`http://openapi.youdao.com/api?${query}`).then(({ data }) => { + if (data.query && data.translation[0]) { + resolve(data.translation[0]) + } else { + resolve(q) + } + }) + } + }) + +const transform = async ({ from, to, locales, outputPath }) => { + for (const key in locales[from]) { + if (locales[to][key]) { + console.log(`add to skip: ${key}`) + } else { + let res = key + let way = 'youdao' + if (key.indexOf('/') !== 0) { + res = await youdao({ + q: key, + }) + } else { + res = `/${to + key}` + way = 'link' + } + if (res !== key) { + locales[to][key] = res + console.log(`${way}: ${from} -> ${to}: ${key} -> ${res}`) + } else { + console.log(`same: ${from} -> ${to}: ${key}`) + } + } + } + await fs.writeFileSync( + path.resolve(__dirname, outputPath), + jsonFormat(locales[to], { + type: 'space', + size: 2, + }) + ) +} +;(async () => { + const tasks = languages + .map(item => ({ + from: defaultLanguage, + to: item.key, + })) + .filter(item => item.from !== item.to) + + for (const item of tasks) { + console.log(`start: ${item.from} -> ${item.to}`) + await transform({ + from: item.from, + to: item.to, + locales, + outputPath: `../src/locales/${item.to}/messages.json`, + }) + console.log(`completed: ${item.from} -> ${item.to}`) + } + + console.log('All translations have been completed.') +})() diff --git a/src/locales/zh/messages.json b/src/locales/zh/messages.json index 3fb8c6a..8d8710e 100644 --- a/src/locales/zh/messages.json +++ b/src/locales/zh/messages.json @@ -12,4 +12,4 @@ "Switch Theme": "切换主题", "Username": "用户名", "You have viewed all notifications.": "您已查看所有通知" -} +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 28362f7..1e9e70a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2940,6 +2940,10 @@ chardet@^0.7.0: version "0.7.0" resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e" +charenc@~0.0.1: + version "0.0.2" + resolved "https://registry.yarnpkg.com/charenc/-/charenc-0.0.2.tgz#c0a1d2f3a7092e03774bfa83f14c0fc5790a8667" + check-error@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82" @@ -3512,6 +3516,10 @@ cross-spawn@^6.0.0, cross-spawn@^6.0.5: shebang-command "^1.2.0" which "^1.2.9" +crypt@~0.0.1: + version "0.0.2" + resolved "https://registry.yarnpkg.com/crypt/-/crypt-0.0.2.tgz#88d7ff7ec0dfb86f713dc87bbb42d044d3e6c41b" + crypto-browserify@^3.11.0: version "3.12.0" resolved "https://registry.yarnpkg.com/crypto-browserify/-/crypto-browserify-3.12.0.tgz#396cf9f3137f03e4b8e532c58f698254e00f80ec" @@ -6569,7 +6577,7 @@ is-boolean-object@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/is-boolean-object/-/is-boolean-object-1.0.0.tgz#98f8b28030684219a95f375cfbd88ce3405dff93" -is-buffer@^1.1.4, is-buffer@^1.1.5: +is-buffer@^1.1.4, is-buffer@^1.1.5, is-buffer@~1.1.1: version "1.1.6" resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be" @@ -7402,6 +7410,10 @@ jsesc@~0.5.0: version "0.5.0" resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-0.5.0.tgz#e7dee66e35d6fc16f710fe91d5cf69f70f08911d" +json-format@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/json-format/-/json-format-1.0.1.tgz#143f67e62af129d6bffed288a46265ea23d0df0c" + json-parse-better-errors@^1.0.1, json-parse-better-errors@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/json-parse-better-errors/-/json-parse-better-errors-1.0.2.tgz#bb867cfb3450e69107c131d1c514bab3dc8bcaa9" @@ -8061,6 +8073,14 @@ md5.js@^1.3.4: hash-base "^3.0.0" inherits "^2.0.1" +md5@^2.2.1: + version "2.2.1" + resolved "https://registry.yarnpkg.com/md5/-/md5-2.2.1.tgz#53ab38d5fe3c8891ba465329ea23fac0540126f9" + dependencies: + charenc "~0.0.1" + crypt "~0.0.1" + is-buffer "~1.1.1" + mdast-util-compact@^1.0.0: version "1.0.2" resolved "https://registry.yarnpkg.com/mdast-util-compact/-/mdast-util-compact-1.0.2.tgz#c12ebe16fffc84573d3e19767726de226e95f649"