Skip to content

Commit

Permalink
feat: pontx-config.json add "cacheFilePath"
Browse files Browse the repository at this point in the history
  • Loading branch information
mu-kang committed Jun 6, 2023
1 parent cc0e457 commit 0d4aa81
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# CHANGELOG

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

## 2023-06-03
1. FIX: update baidu translate lib
Expand Down
17 changes: 4 additions & 13 deletions plugins/fetch/pontx-meta-fetch-plugin/src/LocalDictManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,17 @@ import * as os from "os";
class LocalDictManager {
static singleInstance = null as LocalDictManager;

static getSingleInstance(options = {}, configDir = "") {
static getSingleInstance(localDictDir) {
if (!LocalDictManager.singleInstance) {
LocalDictManager.singleInstance = new LocalDictManager(options, configDir);
LocalDictManager.singleInstance = new LocalDictManager(localDictDir);
return LocalDictManager.singleInstance;
}

return LocalDictManager.singleInstance;
}

private localDictDir = os.homedir() + "/.pont";

constructor(options, configDir) {
if (options?.translateCacheDir) {
this.localDictDir = path.join(configDir, options?.translateCacheDir);
if (!fs.pathExistsSync(this.localDictDir)) {
fs.mkdirpSync(this.localDictDir);
}
return;
}
if (!fs.pathExistsSync(this.localDictDir)) {
constructor(private localDictDir = "") {
if (this.localDictDir && !fs.pathExistsSync(this.localDictDir)) {
fs.mkdirpSync(this.localDictDir);
}
}
Expand Down
17 changes: 10 additions & 7 deletions plugins/fetch/pontx-meta-fetch-plugin/src/translator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fetch } from "node-fetch";
import * as os from "os";
import * as path from "path";
import * as _ from "lodash";
const { youdao, baidu, google } = require("translation.js");
import * as assert from "assert";
Expand Down Expand Up @@ -62,13 +63,15 @@ export class Translate {
dict = {};

constructor(private logger, private translateOptions: any = {}, private config: any = {}) {
this.PontDictManager = PontDictManager(
this.translateOptions,
this.config?.plugins?.fetch?.instance?.innerConfig?.configDir,
);
if (translateOptions?.translateCacheDir) {
this.dictName = "pontxTranslateCache.json";
let localDictDir = os.homedir() + "/.pont";
if (translateOptions?.cacheFilePath) {
this.dictName = path.basename(translateOptions?.cacheFilePath);
localDictDir = path.resolve(
this.config?.plugins?.fetch?.instance?.innerConfig?.configDir,
path.dirname(translateOptions.cacheFilePath),
);
}
this.PontDictManager = PontDictManager(localDictDir);
const localDict = this.PontDictManager.loadFileIfExistsSync(this.dictName);

if (localDict) {
Expand Down

0 comments on commit 0d4aa81

Please sign in to comment.