Skip to content

Commit 66a482d

Browse files
committed
feat: #394 支持自定义翻译文件的生成路径
1 parent cc88851 commit 66a482d

9 files changed

Lines changed: 41 additions & 12 deletions

File tree

docs/pontConfig.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
| usingOperationId | 使用operationId作为方法名 | `boolean` | `true` |
2323
| usingMultipleOrigins | pont 支持一个项目中配置多个 Swagger 来源。此处配置是否启用多数据源 | `boolean` | `false` |
2424
| baiduTranslateConfigs | pont内置多种翻译引擎,用于转换非法类名(如包含特殊字符或者中文)。填写了此配置将优先使用百度开放翻译引擎。需填写APP_ID和APP_SECRET,详见https://fanyi-api.baidu.com/product/113 | `Array<{appId:string,appSecret:string}>` | null |
25+
| translatePath | 自定义翻译文件的生成路径,包含文件名(使用相对路径指定)| `string` | `"node_modules/.pont/translate-dict.txt"` |
2526

2627

2728
## origins 配置项

packages/pont-engine/src/compatible/Config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { IBaiduTranslateConfig, OriginType, Surrounding } from '../types/pontCon
55
import { getTemplate } from '../utils/templateHelp';
66
import { defaultFetchMethodCode, defaultTransformCode } from '../constants/defaultCode';
77
import { Mocks } from './Mocks';
8+
import { LOCAL_DICT_DIR, TRANSLATE_DICT_NAME } from '../constants';
89

910
/** @deprecated */
1011
export class DataSourceConfig {
@@ -29,6 +30,8 @@ export class DataSourceConfig {
2930
mocks = new Mocks();
3031
customTemplatePath: string;
3132
baiduTranslateConfigs: Array<IBaiduTranslateConfig> = null;
33+
/** 指定翻译文件的生成路径(相对路径) */
34+
translatePath = `${LOCAL_DICT_DIR}/${TRANSLATE_DICT_NAME}`;
3235

3336
constructor(config: DataSourceConfig) {
3437
Object.keys(config).forEach((key) => {

packages/pont-engine/src/compatible/scripts/base.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ export class OriginBaseReader {
3030
chineseKeyCollect.sort((pre, next) => next.length - pre.length);
3131

3232
let result = await Promise.all(
33-
chineseKeyCollect.map((text) => translate(this.config.rootDir, this.config.baiduTranslateConfigs, text))
33+
chineseKeyCollect.map((text) =>
34+
translate(this.config.rootDir, this.config.translatePath, this.config.baiduTranslateConfigs, text)
35+
)
3436
);
3537
// const normalizeRegStr = (str: string) => str.replace(/(\W)/g, '$1');
3638
const toRegStr = (str) => str.replace(/(\W)/g, '\\$1');

packages/pont-engine/src/main/Config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export class Config extends OldConfig {
7474
customTemplatePath: origin.customTemplatePath
7575
},
7676
baiduTranslateConfigs: standardBaseConfig.baiduTranslateConfigs,
77+
translatePath: standardBaseConfig.translatePath,
7778
...origin
7879
};
7980
});
@@ -88,7 +89,8 @@ export class Config extends OldConfig {
8889
templateOriginalPath: {
8990
customTemplatePath: null
9091
},
91-
baiduTranslateConfigs: standardBaseConfig.baiduTranslateConfigs
92+
baiduTranslateConfigs: standardBaseConfig.baiduTranslateConfigs,
93+
translatePath: standardBaseConfig.translatePath
9294
}
9395
];
9496
}

packages/pont-engine/src/main/originManage/OriginReader.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ export class OriginReader implements IOriginReader {
2020
}
2121

2222
translate(jsonString: string): Promise<string> {
23-
return translateChinese(jsonString, this.config.rootDir, this.config.baiduTranslateConfigs);
23+
return translateChinese(
24+
jsonString,
25+
this.config.rootDir,
26+
this.config.translatePath,
27+
this.config.baiduTranslateConfigs
28+
);
2429
}
2530

2631
async transform2StandardDataSource(json: any, config: IStandardOirginConfig): Promise<StandardDataSource> {

packages/pont-engine/src/types/pontConfig.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ export interface IBaseConfig {
5959

6060
/** 百度翻译秘钥信息 */
6161
baiduTranslateConfigs?: Array<IBaiduTranslateConfig>;
62+
63+
/** 指定翻译文件的生成路径(相对路径) */
64+
translatePath?: string;
6265
}
6366

6467
export interface IPontConfig extends IBaseConfig {
@@ -91,4 +94,7 @@ export interface IStandardOirginConfig extends IOriginConfig {
9194

9295
/** 百度翻译秘钥信息 */
9396
baiduTranslateConfigs?: Array<IBaiduTranslateConfig>;
97+
98+
/** 指定翻译文件的生成路径(相对路径) */
99+
translatePath?: string;
94100
}

packages/pont-engine/src/utils/PontFileManager.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import * as path from 'path';
88
import { LOCAL_DICT_DIR } from '../constants';
99

1010
export class PontFileManager {
11-
static getLocalFilePath(rootDir: string, filename: string): string {
11+
static getLocalFilePath(rootDir: string, filename: string, customPath?: string): string {
12+
if (customPath) {
13+
return path.join(rootDir, customPath);
14+
}
1215
return path.join(rootDir, LOCAL_DICT_DIR, filename);
1316
}
1417

@@ -56,8 +59,8 @@ export class PontFileManager {
5659
static appendFile(filePath: string, content: string) {
5760
if (fs.existsSync(filePath)) {
5861
return fs.appendFile(filePath, content);
59-
}else{
60-
return PontFileManager.saveFile(filePath,content)
62+
} else {
63+
return PontFileManager.saveFile(filePath, content);
6164
}
6265
}
6366

packages/pont-engine/src/utils/translate.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import * as assert from 'assert';
66
import * as _ from 'lodash';
77
import { PontFileManager } from './PontFileManager';
8-
import { TRANSLATE_DICT_NAME } from '../constants';
98
import { IBaiduTranslateConfig } from '../types/pontConfig';
109
import { youdao, baidu, google } from 'translation.js';
1110
import pinyin from './pinyin';
@@ -48,8 +47,8 @@ const engines = [
4847
export const dict: { [rootDir: string]: { [cn: string]: string } } = {};
4948
const dicPath: { [rootDir: string]: string } = {};
5049

51-
function init(rootDir: string) {
52-
dicPath[rootDir] = PontFileManager.getLocalFilePath(rootDir, TRANSLATE_DICT_NAME);
50+
function init(rootDir: string, translatePath: string) {
51+
dicPath[rootDir] = PontFileManager.getLocalFilePath(rootDir, null, translatePath);
5352
const localDict = PontFileManager.loadFile(dicPath[rootDir]);
5453

5554
if (localDict) {
@@ -85,12 +84,13 @@ function startCaseClassName(result) {
8584

8685
export async function translate(
8786
rootDir,
87+
translatePath,
8888
baiduTranslateConfigs: IBaiduTranslateConfig[],
8989
text: string,
9090
engineIndex = 0
9191
) {
9292
if (!dicPath[rootDir]) {
93-
init(rootDir);
93+
init(rootDir, translatePath);
9494
}
9595

9696
if (dict[rootDir]?.[text]) {
@@ -140,14 +140,15 @@ export async function translate(
140140
return enKey;
141141
} catch (err) {
142142
console.error(`translateEngine:${translateEngine.name} text:${text} err:${err}`);
143-
return translate(rootDir, baiduTranslateConfigs, text, index + 1);
143+
return translate(rootDir, translatePath, baiduTranslateConfigs, text, index + 1);
144144
}
145145
}
146146

147147
/** 翻译中文类名等 */
148148
export async function translateChinese(
149149
jsonString: string,
150150
rootDir: string,
151+
translatePath: string,
151152
baiduTranslateConfigs?: IBaiduTranslateConfig[]
152153
) {
153154
let retString = jsonString;
@@ -168,7 +169,9 @@ export async function translateChinese(
168169
// 例如: 请求参数vo, 请求参数, 替换时先替换 请求参数vo, 后替换请求参数
169170
chineseKeyCollect.sort((pre, next) => next.length - pre.length);
170171

171-
let result = await Promise.all(chineseKeyCollect.map((text) => translate(rootDir, baiduTranslateConfigs, text)));
172+
let result = await Promise.all(
173+
chineseKeyCollect.map((text) => translate(rootDir, translatePath, baiduTranslateConfigs, text))
174+
);
172175
// const normalizeRegStr = (str: string) => str.replace(/(\W)/g, '$1');
173176
const toRegStr = (str) => str.replace(/(\W)/g, '\\$1');
174177
result.forEach((enKey: string, index) => {

packages/vscode-pont/configSchema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@
134134
}
135135
}
136136
}
137+
},
138+
"translatePath": {
139+
"description": "自定义翻译文件的生成路径,包含文件名(使用相对路径指定)",
140+
"type": "string"
137141
}
138142
}
139143
}

0 commit comments

Comments
 (0)