gpt-po 1.0.5 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/package.json +1 -1
- package/lib/src/index.js +5 -3
- package/lib/src/translate.d.ts +1 -1
- package/lib/src/translate.js +18 -8
- package/lib/src/utils.d.ts +7 -0
- package/lib/src/utils.js +49 -2
- package/package.json +1 -1
package/lib/package.json
CHANGED
package/lib/src/index.js
CHANGED
@@ -39,19 +39,21 @@ program
|
|
39
39
|
.option("-src, --source <lang>", "source language", "English")
|
40
40
|
.option("-l, --lang <lang>", "target language", "Simplified Chinese")
|
41
41
|
.option("--verbose", "print verbose log")
|
42
|
-
.
|
43
|
-
.action((
|
42
|
+
.addOption(new commander_1.Option("-o, --output <file>", "output file path, overwirte po file by default").conflicts("dir"))
|
43
|
+
.action((args) => __awaiter(void 0, void 0, void 0, function* () {
|
44
|
+
const { key, host, model, po, dir, source, lang, verbose, output, checkRegx } = args;
|
44
45
|
if (host) {
|
45
46
|
process.env.OPENAI_API_HOST = host;
|
46
47
|
}
|
47
48
|
if (key) {
|
48
49
|
process.env.OPENAI_API_KEY = key;
|
49
50
|
}
|
51
|
+
(0, translate_1.init)();
|
50
52
|
if (po) {
|
51
53
|
yield (0, translate_1.translatePo)(model, po, source, lang, verbose, output);
|
52
54
|
}
|
53
55
|
else if (dir) {
|
54
|
-
yield (0, translate_1.translatePoDir)(model, dir, source, lang, verbose
|
56
|
+
yield (0, translate_1.translatePoDir)(model, dir, source, lang, verbose);
|
55
57
|
}
|
56
58
|
else {
|
57
59
|
console.error("po file or directory is required");
|
package/lib/src/translate.d.ts
CHANGED
@@ -2,4 +2,4 @@ import { OpenAIApi } from "openai";
|
|
2
2
|
export declare function init(force?: boolean): OpenAIApi;
|
3
3
|
export declare function translate(text: string, src: string, lang: string, model?: string): Promise<import("axios").AxiosResponse<import("openai").CreateChatCompletionResponse, any>>;
|
4
4
|
export declare function translatePo(model: string | undefined, po: string, source: string, lang: string, verbose: boolean, output: string): Promise<void>;
|
5
|
-
export declare function translatePoDir(model: string | undefined, dir: string, source: string, lang: string, verbose: boolean
|
5
|
+
export declare function translatePoDir(model: string | undefined, dir: string, source: string, lang: string, verbose: boolean): Promise<void>;
|
package/lib/src/translate.js
CHANGED
@@ -12,7 +12,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.translatePoDir = exports.translatePo = exports.translate = exports.init = void 0;
|
13
13
|
const fs = require("fs");
|
14
14
|
const openai_1 = require("openai");
|
15
|
-
const os_1 = require("os");
|
16
15
|
const path_1 = require("path");
|
17
16
|
const pkg = require("../package.json");
|
18
17
|
const utils_1 = require("./utils");
|
@@ -31,13 +30,13 @@ function init(force) {
|
|
31
30
|
}
|
32
31
|
// load systemprompt.txt from homedir
|
33
32
|
if (!_systemprompt || force) {
|
34
|
-
const systemprompt = (0,
|
33
|
+
const systemprompt = (0, utils_1.findConfig)("systemprompt.txt");
|
35
34
|
(0, utils_1.copyFileIfNotExists)(systemprompt, (0, path_1.join)(__dirname, "systemprompt.txt"));
|
36
35
|
_systemprompt = fs.readFileSync(systemprompt, "utf-8");
|
37
36
|
}
|
38
37
|
// load dictionary.json from homedir
|
39
38
|
if (!_userdict || force) {
|
40
|
-
const userdict = (0,
|
39
|
+
const userdict = (0, utils_1.findConfig)("dictionary.json");
|
41
40
|
(0, utils_1.copyFileIfNotExists)(userdict, (0, path_1.join)(__dirname, "dictionary.json"));
|
42
41
|
_userdict = JSON.parse(fs.readFileSync(userdict, "utf-8"));
|
43
42
|
}
|
@@ -45,14 +44,13 @@ function init(force) {
|
|
45
44
|
}
|
46
45
|
exports.init = init;
|
47
46
|
function translate(text, src, lang, model = "gpt-3.5-turbo") {
|
48
|
-
const openai = init();
|
49
47
|
const dicts = Object.entries(_userdict)
|
50
48
|
.map(([k, v]) => [
|
51
49
|
{ role: "user", content: k },
|
52
50
|
{ role: "assistant", content: v },
|
53
51
|
])
|
54
52
|
.flat();
|
55
|
-
return
|
53
|
+
return _openai.createChatCompletion({
|
56
54
|
model,
|
57
55
|
temperature: 0.1,
|
58
56
|
messages: [
|
@@ -79,13 +77,25 @@ function translatePo(model = "gpt-3.5-turbo", po, source, lang, verbose, output)
|
|
79
77
|
return __awaiter(this, void 0, void 0, function* () {
|
80
78
|
const potrans = yield (0, utils_1.parsePo)(po);
|
81
79
|
const list = [];
|
80
|
+
const trimRegx = /(?:^ )|(?: $)/;
|
81
|
+
let trimed = false;
|
82
82
|
for (const [ctx, entries] of Object.entries(potrans.translations)) {
|
83
83
|
for (const [msgid, trans] of Object.entries(entries)) {
|
84
|
+
if (msgid == "")
|
85
|
+
continue;
|
84
86
|
if (!trans.msgstr[0]) {
|
85
87
|
list.push(trans);
|
88
|
+
continue;
|
89
|
+
}
|
90
|
+
else if (trimRegx.test(trans.msgstr[0])) {
|
91
|
+
trimed = true;
|
92
|
+
trans.msgstr[0] = trans.msgstr[0].trim();
|
86
93
|
}
|
87
94
|
}
|
88
95
|
}
|
96
|
+
if (trimed) {
|
97
|
+
yield (0, utils_1.compilePo)(potrans, po);
|
98
|
+
}
|
89
99
|
if (list.length == 0) {
|
90
100
|
console.log("done.");
|
91
101
|
return;
|
@@ -121,7 +131,7 @@ function translatePo(model = "gpt-3.5-turbo", po, source, lang, verbose, output)
|
|
121
131
|
}
|
122
132
|
else {
|
123
133
|
console.error(error.response.status);
|
124
|
-
|
134
|
+
console.log(error.response.data);
|
125
135
|
}
|
126
136
|
}
|
127
137
|
else {
|
@@ -133,14 +143,14 @@ function translatePo(model = "gpt-3.5-turbo", po, source, lang, verbose, output)
|
|
133
143
|
});
|
134
144
|
}
|
135
145
|
exports.translatePo = translatePo;
|
136
|
-
function translatePoDir(model = "gpt-3.5-turbo", dir, source, lang, verbose
|
146
|
+
function translatePoDir(model = "gpt-3.5-turbo", dir, source, lang, verbose) {
|
137
147
|
return __awaiter(this, void 0, void 0, function* () {
|
138
148
|
const files = fs.readdirSync(dir);
|
139
149
|
for (const file of files) {
|
140
150
|
if (file.endsWith(".po")) {
|
141
151
|
const po = (0, path_1.join)(dir, file);
|
142
152
|
console.log(`translating ${po}`);
|
143
|
-
yield translatePo(model, po, source, lang, verbose,
|
153
|
+
yield translatePo(model, po, source, lang, verbose, po);
|
144
154
|
}
|
145
155
|
}
|
146
156
|
});
|
package/lib/src/utils.d.ts
CHANGED
@@ -1,6 +1,13 @@
|
|
1
1
|
import { GetTextTranslations } from "gettext-parser";
|
2
|
+
/**
|
3
|
+
* copy source file to destination file if destination file does not exist
|
4
|
+
* @param file destination file path
|
5
|
+
* @param copyFile source file path
|
6
|
+
*/
|
2
7
|
export declare function copyFileIfNotExists(file: string, copyFile: string): void;
|
3
8
|
export declare function openFileByDefault(filePath: string): void;
|
4
9
|
export declare function parsePo(poFile: string, defaultCharset?: string): Promise<GetTextTranslations>;
|
5
10
|
export declare function compilePo(data: GetTextTranslations, poFile: string): Promise<void>;
|
6
11
|
export declare function printProgress(progress: number, total: number, extra?: string): void;
|
12
|
+
export declare function gitRootDir(dir?: string): string | null;
|
13
|
+
export declare function findConfig(fileName: string): string;
|
package/lib/src/utils.js
CHANGED
@@ -1,10 +1,19 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.printProgress = exports.compilePo = exports.parsePo = exports.openFileByDefault = exports.copyFileIfNotExists = void 0;
|
4
|
-
const fs = require("fs");
|
3
|
+
exports.findConfig = exports.gitRootDir = exports.printProgress = exports.compilePo = exports.parsePo = exports.openFileByDefault = exports.copyFileIfNotExists = void 0;
|
5
4
|
const child_process_1 = require("child_process");
|
5
|
+
const fs = require("fs");
|
6
6
|
const gettext_parser_1 = require("gettext-parser");
|
7
|
+
const os_1 = require("os");
|
8
|
+
const path = require("path");
|
9
|
+
/**
|
10
|
+
* copy source file to destination file if destination file does not exist
|
11
|
+
* @param file destination file path
|
12
|
+
* @param copyFile source file path
|
13
|
+
*/
|
7
14
|
function copyFileIfNotExists(file, copyFile) {
|
15
|
+
// make sure the directory exists
|
16
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
8
17
|
// check if file exists else create it
|
9
18
|
try {
|
10
19
|
fs.accessSync(file, fs.constants.F_OK);
|
@@ -57,4 +66,42 @@ function printProgress(progress, total, extra) {
|
|
57
66
|
process.stdout.write(`\r${bar}${dots} ${percent}% ${progress}/${total} ${extra || ""}`);
|
58
67
|
}
|
59
68
|
exports.printProgress = printProgress;
|
69
|
+
function gitRootDir(dir) {
|
70
|
+
// if dir is not provided, use current working directory
|
71
|
+
dir = dir || process.cwd();
|
72
|
+
// check if dir is a git repository
|
73
|
+
if (fs.existsSync(path.join(dir, ".git"))) {
|
74
|
+
return dir;
|
75
|
+
}
|
76
|
+
else {
|
77
|
+
// if dir is root directory, return null
|
78
|
+
if (path.dirname(dir) === dir) {
|
79
|
+
return null;
|
80
|
+
}
|
81
|
+
else {
|
82
|
+
// else, check parent directory
|
83
|
+
return gitRootDir(path.dirname(dir));
|
84
|
+
}
|
85
|
+
}
|
86
|
+
}
|
87
|
+
exports.gitRootDir = gitRootDir;
|
88
|
+
function findConfig(fileName) {
|
89
|
+
const currentDir = process.cwd();
|
90
|
+
const gitDir = gitRootDir() || currentDir;
|
91
|
+
const homeDir = (0, os_1.homedir)();
|
92
|
+
const filePaths = [
|
93
|
+
path.join(currentDir, ".gpt-po", fileName),
|
94
|
+
path.join(gitDir, ".gpt-po", fileName),
|
95
|
+
path.join(homeDir, ".config", ".gpt-po", fileName)
|
96
|
+
];
|
97
|
+
// check if file exists and return the first one
|
98
|
+
for (const filePath of filePaths) {
|
99
|
+
if (fs.existsSync(filePath)) {
|
100
|
+
return filePath;
|
101
|
+
}
|
102
|
+
}
|
103
|
+
// if no file exists, return the default one
|
104
|
+
return path.join(homeDir, ".gpt-po", fileName);
|
105
|
+
}
|
106
|
+
exports.findConfig = findConfig;
|
60
107
|
//# sourceMappingURL=utils.js.map
|