Skip to content

Commit 0d4aa81

Browse files
committed
feat: pontx-config.json add "cacheFilePath"
1 parent cc0e457 commit 0d4aa81

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# CHANGELOG
22

33
## 2023-06-06
4-
1. FEAT: `pontx-config.json` new parameters `plugins.fetch.options.translate.translateCacheDir`,Caching translation results
4+
1. FEAT: `pontx-config.json` new parameters `plugins.fetch.options.translate.cacheFilePath`,Caching translation results
55

66
## 2023-06-03
77
1. FIX: update baidu translate lib

plugins/fetch/pontx-meta-fetch-plugin/src/LocalDictManager.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,17 @@ import * as os from "os";
55
class LocalDictManager {
66
static singleInstance = null as LocalDictManager;
77

8-
static getSingleInstance(options = {}, configDir = "") {
8+
static getSingleInstance(localDictDir) {
99
if (!LocalDictManager.singleInstance) {
10-
LocalDictManager.singleInstance = new LocalDictManager(options, configDir);
10+
LocalDictManager.singleInstance = new LocalDictManager(localDictDir);
1111
return LocalDictManager.singleInstance;
1212
}
1313

1414
return LocalDictManager.singleInstance;
1515
}
1616

17-
private localDictDir = os.homedir() + "/.pont";
18-
19-
constructor(options, configDir) {
20-
if (options?.translateCacheDir) {
21-
this.localDictDir = path.join(configDir, options?.translateCacheDir);
22-
if (!fs.pathExistsSync(this.localDictDir)) {
23-
fs.mkdirpSync(this.localDictDir);
24-
}
25-
return;
26-
}
27-
if (!fs.pathExistsSync(this.localDictDir)) {
17+
constructor(private localDictDir = "") {
18+
if (this.localDictDir && !fs.pathExistsSync(this.localDictDir)) {
2819
fs.mkdirpSync(this.localDictDir);
2920
}
3021
}

plugins/fetch/pontx-meta-fetch-plugin/src/translator.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { fetch } from "node-fetch";
1+
import * as os from "os";
2+
import * as path from "path";
23
import * as _ from "lodash";
34
const { youdao, baidu, google } = require("translation.js");
45
import * as assert from "assert";
@@ -62,13 +63,15 @@ export class Translate {
6263
dict = {};
6364

6465
constructor(private logger, private translateOptions: any = {}, private config: any = {}) {
65-
this.PontDictManager = PontDictManager(
66-
this.translateOptions,
67-
this.config?.plugins?.fetch?.instance?.innerConfig?.configDir,
68-
);
69-
if (translateOptions?.translateCacheDir) {
70-
this.dictName = "pontxTranslateCache.json";
66+
let localDictDir = os.homedir() + "/.pont";
67+
if (translateOptions?.cacheFilePath) {
68+
this.dictName = path.basename(translateOptions?.cacheFilePath);
69+
localDictDir = path.resolve(
70+
this.config?.plugins?.fetch?.instance?.innerConfig?.configDir,
71+
path.dirname(translateOptions.cacheFilePath),
72+
);
7173
}
74+
this.PontDictManager = PontDictManager(localDictDir);
7275
const localDict = this.PontDictManager.loadFileIfExistsSync(this.dictName);
7376

7477
if (localDict) {

0 commit comments

Comments
 (0)