Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: translate module #15

Merged
merged 8 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ node_modules
dist
lib
temp
example/auto-command/localesDir
example/auto-command/localesFile
9 changes: 9 additions & 0 deletions example/auto-command/localesDir/zh-CN.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import menu from './zh-CN/menu';
import pages from './zh-CN/pages';
import other from './zh-CN/other';

export default {
...menu,
...pages,
...other,
};
10 changes: 10 additions & 0 deletions example/auto-command/localesDir/zh-CN/menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
'menu.login': '登录',
'menu.welcome': '欢迎页面',
'menu.setting': '设置',
'menu.analysis': '图标统计',
'menu.data-base': '基础数据',
'menu.data-date': '日期数据',
'menu.account.logout': '退出登录',
'menu.account.userName': '访客',
};
4 changes: 4 additions & 0 deletions example/auto-command/localesDir/zh-CN/other.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
'app.copyright.produced': '道源1035',
'app.connection': '有建议可以联系:微信:DaoYuan1035、邮箱:[email protected]',
};
50 changes: 50 additions & 0 deletions example/auto-command/localesDir/zh-CN/pages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export default {
'pages.layouts.userLayout.title': '时间管理系统',
'pages.layouts.userLayout.subTitle': '更好的统计你的时间去向',
'pages.login.accountLogin.tab': '关注公众号登录',
'pages.login.accountLogin.errorMessage': '错误的验证码',
'pages.login.weixin.tips': '关注公众号登录,回复”验证码“获得',
'pages.login.failure': '登录失败,请重试!',
'pages.login.success': '登录成功!',
'pages.login.captcha.placeholder': '请输入验证码!',
'pages.login.captcha.required': '验证码是必填项!',
'pages.login.submit': '登录',
'pages.table.columns.1': '路径',
'pages.table.columns.2': '开始时间',
'pages.table.columns.3': '结束时间',
'pages.table.columns.4': '大小',
'pages.table.columns.5': '名称',
'pages.table.columns.6': '操作',
'pages.table.columns.7': '标签分组',
'pages.table.columns.8': '索引',
'pages.table.columns.9': '标签',
'pages.table.columns.10': '持续时间',
'pages.table.columns.11': '备注',
'pages.table.columns.12': '日期',
'pages.table.columns.13': '第几周',
'pages.table.columns.14': '记录时间',
'pages.table.columns.15': '未记录时间',
'pages.table.columns.16': '年份',
'pages.table.columns.17': '月份',
'pages.table.operation.1': '删除',
'pages.table.operation.2': '编辑',
'pages.table.other.1': '导入数据',
'pages.table.other.2': '自动获取类型',
'pages.table.other.3': '数据表格',
'pages.table.other.4': '导出配置',
'pages.table.other.5': '导入配置',
'pages.setting.1': '导入导出',
'pages.setting.2': '设置相关',
'pages.setting.3': '类型',
'pages.analysis.1': '卡片动态配置',
'pages.analysis.2': '图标动态配置',
'pages.analysis.3': '记录天数',
'pages.analysis.4': '天',
'pages.analysis.5': '记录时间',
'pages.analysis.6': '最近一周',
'pages.analysis.7': '占比',
'pages.analysis.8': '总计',
'pages.analysis.9': '总',
'pages.analysis.10': '最近一月',
'pages.analysis.11': '最近一年',
};
10 changes: 10 additions & 0 deletions example/auto-command/localesFile/zh-CN.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
'menu.login': '登录',
'menu.welcome': '欢迎页面',
'menu.setting': '设置',
'menu.analysis': '图标统计',
'menu.data-base': '基础数据',
'menu.data-date': '日期数据',
'menu.account.logout': '退出登录',
'menu.account.userName': '访客',
};
12 changes: 12 additions & 0 deletions example/auto-command/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"private": true,
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "esno ./test.ts"
},
"keywords": [],
"author": "",
"license": "ISC"
}
43 changes: 43 additions & 0 deletions example/auto-command/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { defineConfig } from '@/packages/auto-command/src';
import translate from '@/packages/auto-command/src/translate';

const config1 = defineConfig({
translate: {
translatorType: 'youdao',
// 分隔符号(可选):默认为-,如果你的文件名不是以-分割的话需要配置
separator: '-',
// 是否保持以前的翻译不变(可选),默认关闭
keep: false,
// 类型(可选):默认为目录
type: 'dir',
// 路径(必填):locales文件的绝对路径
outDir: '/Users/shawdanon/GitHub/minehttp/txp/example/auto-command/localesDir',
// 语言转换(可选):默认从中文转英文
language: {
from: 'zh-CN',
to: ['en-US'],
},
},
});
const config2 = defineConfig({
translate: {
google: { proxy: { host: '1', port: 123 } },
youdao: { key: '12', secret: '123' },
translatorType: 'youdao',
language: {
from: 'zh-CN',
to: ['en-US'],
},
type: 'file',
outDir: '/Users/shawdanon/GitHub/minehttp/txp/example/auto-command/localesFile',
},
});
const config3 = defineConfig({
translate: {
outDir: '/Users/shawdanon/GitHub/minehttp/txp/example/auto-command/localesDir',
},
});
// 测试目录情况
// translate(config1.translate);
// 测试文件情况
translate(config3.translate);
File renamed without changes.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
"@types/babel__core": "^7.1.19",
"@types/babel__generator": "^7.6.4",
"@types/fs-extra": "^9.0.13",
"@types/got": "^9.6.12",
"@types/jest": "^24.0.15",
"@types/lodash": "^4.14.182",
"@types/md5": "^2.3.2",
"@types/signale": "^1.4.4",
"@types/tunnel": "^0.0.3",
"@types/yargs-parser": "^21.0.0",
"@umijs/fabric": "^1.1.9",
"esno": "^0.16.3",
Expand Down
29 changes: 22 additions & 7 deletions packages/auto-command/README zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,33 @@ import { defineConfig } from 'auto-command';

export default defineConfig({
translate: {
// 分隔符号(可选):默认为-,如果你的文件名不是以-分割的话需要配置
separator: '-',
// 是否保持以前的翻译不变(可选),默认关闭
keep: false,
// 类型(可选):默认为目录
type: 'dir',
// 路径(必填):locales文件的绝对路径
outDir: '/xxx/xxx/xxx/src/locales',
// 语言转换(可选):默认从中文转英文
// 是否保持以前的翻译不变(可选),默认开启
keep: true,
// 类型(可选):默认为目录(antd-pro模式)
type: 'dir',
// 语言转换(可选):默认从中文转英文,输出的文件名和这个配置有关
language: {
from: 'zh-CN',
to: ['en-US'],
},
// 分隔符号(可选):默认为-,如果你的文件名不是以-分割的话需要配置
separator: '-',
// 翻译器类型(可选):默认youdao
translatorType: 'youdao',
// google翻译器配置(可选):默认空
google: {
proxy: {
host: '127.0.0.1',
port: 7890,
},
},
// youdao翻译器配置(可选):默认有值,如果翻译失败可能余额不足,请配置
youdao: {
key: 'xxx',
secret: 'xxx',
},
},
});
```
Expand All @@ -56,6 +70,7 @@ $ npx ac -t=translate
- 支持配置文件提示
- 支持已经在已经翻译过的项目上进行补充翻译
- 支持自定义分隔符
- 支持多类型翻译器:谷歌翻译(国外建议)、有道翻译(国内建议)

## 分支差异比较并导出差异文件

Expand Down
29 changes: 22 additions & 7 deletions packages/auto-command/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,33 @@ import { defineConfig } from 'auto-command';

export default defineConfig({
translate: {
// Separator (optional): The default is -, if your file name is not separated by -, you need to configure
separator: '-',
// Whether to keep the previous translation unchanged (optional), off by default
keep: false,
// type (optional): defaults to directory
type: 'dir',
// path (required): absolute path to the locales file
outDir: '/xxx/xxx/xxx/src/locales',
// Language conversion (optional): default from Chinese to English
// Whether to keep the previous translation unchanged (optional), enabled by default
keep: true,
// type (optional): defaults to directory (antd-pro mode)
type: 'dir',
// Language conversion (optional): Convert from Chinese to English by default, the output file name is related to this configuration
language: {
from: 'zh-CN',
to: ['en-US'],
},
// Separator (optional): The default is -, if your file name is not separated by -, you need to configure
separator: '-',
// Translator type (optional): default youdao
translatorType: 'youdao',
// google translator configuration (optional): empty by default
google: {
proxy: {
host: '127.0.0.1',
port: 7890,
},
},
// youdao translator configuration (optional): there is a value by default, if the translation fails, the balance may be insufficient, please configure
youdao: {
key: 'xxx',
secret: 'xxx',
},
},
});
```
Expand All @@ -56,6 +70,7 @@ $ npx ac -t=translate
- Support configuration file prompts
- Support for supplementary translations on already translated projects
- Support custom delimiter
- Support multiple translators: Google Translate (recommended abroad), Youdao Translator (recommended in China)

## Branch diff comparison and export diff file

Expand Down
4 changes: 4 additions & 0 deletions packages/auto-command/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
],
"scripts": {
"build": "pnpm tsc",
"genLangs": "esno ./src/translate/translate-api/languages/genTargetCode.ts",
"sort": "npx sort-package-json"
},
"dependencies": {
Expand All @@ -29,11 +30,14 @@
"axios": "^0.27.2",
"esbuild": "^0.14.49",
"fs-extra": "^9.0.1",
"got": "9.6.0",
"inquirer": "^7.3.0",
"md5": "^2.3.0",
"pirates": "^4.0.5",
"prettier": "^2.7.1",
"querystring": "^0.2.1",
"signale": "^1.4.0",
"tunnel": "^0.0.6",
"txp-utils": "^1.2.0",
"yargs-parser": "^21.0.1"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/auto-command/src/defineConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ITranslateConfig } from './translate/index';
import { TranslateConfig } from './translate/index';

export type ConfigType = { translate: ITranslateConfig };
export type ConfigType = { translate: TranslateConfig };

export function defineConfig(config: ConfigType): ConfigType {
return config;
Expand Down
46 changes: 21 additions & 25 deletions packages/auto-command/src/translate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import traverse from '@babel/traverse';
import generate from '@babel/generator';
import utils from 'txp-utils';
import prettier from 'prettier';
import type { TLanguage } from './translate';
import translate from './translate';
import type { Code, I18nPartOptions, I18nOptions } from './translate-i18n/types';
import type { ApiPartOptions } from './translate-api/types';
import translate from './translate-i18n';

interface generalObj {
[key: string]: generalObj;
Expand Down Expand Up @@ -153,11 +154,7 @@ function getObj(str: string) {
}

// 传入一个对象,翻译fileContent里面的value值
async function replaceValue(
params: { [key: string]: any },
{ from, to }: { from: string; to: string },
separator: string,
) {
async function replaceValue(params: { [key: string]: any }, options: I18nOptions) {
// 遍历对象value值成扁平数组
const newParams = JSON.parse(JSON.stringify(params));
let isLog = false;
Expand All @@ -183,7 +180,8 @@ async function replaceValue(
);
// 数组转换成字符串,翻译,再转换成数组
const str = arr.join('\n');
const newStr = await translate(str, { from, to }, separator);
// @ts-ignore 这里设置默认值,options有值就覆盖
const newStr = await translate(str, { separator: '-', translatorType: 'youdao', ...options });
const newArr = newStr.split('\n');
// 新数组重新赋值给对象
let index = 0;
Expand Down Expand Up @@ -211,20 +209,20 @@ async function replaceValue(
return newParams;
}
// 核心翻译流程
export interface ITranslateConfig {
export interface TranslateConfig extends ApiPartOptions, I18nPartOptions {
outDir: string;
keep?: boolean;
type?: 'dir' | 'file';
outDir: string;
language?: { from: TLanguage; to: TLanguage[] };
separator?: string;
language?: { from: Code; to: Code[] };
}

async function core({
outDir,
keep = true,
type = 'dir',
outDir,
language = { from: 'zh-CN', to: ['en-US'] },
separator = '-',
}: ITranslateConfig) {
...rest
}: TranslateConfig) {
signale.time('translate');
// 判断input是路径还是文件
if (type === 'dir') {
Expand Down Expand Up @@ -262,14 +260,13 @@ async function core({
const inputFileData = outFileArr.find((item) => item.dirName === language.from);
// 翻译
const allRequst = language.to.map((item) =>
replaceValue(
inputFileData?.dirContent || {},
{
replaceValue(inputFileData?.dirContent || {}, {
language: {
from: language.from,
to: item,
},
separator,
),
...rest,
}),
);
const resData = await Promise.all(allRequst);
const transData = language.to.map((item, index) => ({
Expand Down Expand Up @@ -310,14 +307,13 @@ async function core({
const suffix = path.extname(inputFileData.fileName);
// 翻译
const allRequst = language.to.map((item) =>
replaceValue(
inputFileData.fileContent,
{
replaceValue(inputFileData.fileContent, {
language: {
from: language.from,
to: item,
},
separator,
),
...rest,
}),
);
const resData = await Promise.all(allRequst);
const transData = language.to.map((item, index) => ({
Expand Down
Loading