gpt-po 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +3 -0
- package/README_zh-CN.md +3 -0
- package/lib/package.json +1 -1
- package/lib/src/index.js +16 -5
- package/lib/src/translate.d.ts +2 -1
- package/lib/src/translate.js +25 -10
- package/package.json +1 -1
package/README.md
CHANGED
@@ -21,6 +21,7 @@ Set `OPENAI_API_KEY` before using this tool.
|
|
21
21
|
|
22
22
|
- `gpt-po sync --po <file> --pot <file>` Update the po file based on the pot file, while preserving the original translations.
|
23
23
|
- `gpt-po --po <file>` Translate specified po files to a designated target language. By default, the target language is Simplified Chinese.
|
24
|
+
- `gpt-po --dir .` Translate all po files in current directory to a designated target language.
|
24
25
|
- `gpt-po userdict` Modify or view user dictionaries
|
25
26
|
- `gpt-po systemprompt` Modify or view system prompts, if you are not sure how to use it, you can leave it alone
|
26
27
|
|
@@ -52,7 +53,9 @@ Options:
|
|
52
53
|
--model <model> openai model (choices: "gpt-4", "gpt-4-0314", "gpt-4-32k", "gpt-4-32k-0314", "gpt-3.5-turbo", "gpt-3.5-turbo-0301",
|
53
54
|
default: "gpt-3.5-turbo")
|
54
55
|
--po <file> po file path
|
56
|
+
--dir <dir> po file directory
|
55
57
|
-src, --source <lang> source language (default: "english")
|
58
|
+
--verbose show verbose log
|
56
59
|
-l, --lang <lang> target language (default: "simplified chinese")
|
57
60
|
-o, --output <file> output file path, overwirte po file by default
|
58
61
|
-h, --help display help for command
|
package/README_zh-CN.md
CHANGED
@@ -21,6 +21,7 @@ npm install gpt-po
|
|
21
21
|
|
22
22
|
- `gpt-po sync --po <file> --pot <file>` 根据pot文件更新po文件,保留原有翻译
|
23
23
|
- `gpt-po --po <file>` 翻译指定的po文件到指定的目标语言,默认目标语言是简体中文
|
24
|
+
- `gpt-po --dir .` 翻译当前目录下的所有po文件到指定的目标语言,默认目标语言是简体中文
|
24
25
|
- `gpt-po userdict` 修改或查看用户字典
|
25
26
|
- `gpt-po systemprompt` 修改或查看系统提示词,如果你不确定如何使用,可以不用修改
|
26
27
|
|
@@ -52,7 +53,9 @@ Options:
|
|
52
53
|
--model <model> openai model (choices: "gpt-4", "gpt-4-0314", "gpt-4-32k", "gpt-4-32k-0314", "gpt-3.5-turbo", "gpt-3.5-turbo-0301",
|
53
54
|
default: "gpt-3.5-turbo")
|
54
55
|
--po <file> po file path
|
56
|
+
--dir <dir> po files directory
|
55
57
|
-src, --source <lang> source language (default: "english")
|
58
|
+
--verbose show verbose log
|
56
59
|
-l, --lang <lang> target language (default: "simplified chinese")
|
57
60
|
-o, --output <file> output file path, overwirte po file by default
|
58
61
|
-h, --help display help for command
|
package/lib/package.json
CHANGED
package/lib/src/index.js
CHANGED
@@ -34,18 +34,29 @@ program
|
|
34
34
|
"gpt-3.5-turbo",
|
35
35
|
"gpt-3.5-turbo-0301",
|
36
36
|
]))
|
37
|
-
.
|
38
|
-
.
|
39
|
-
.
|
37
|
+
.addOption(new commander_1.Option("--po <file>", "po file path").conflicts("dir"))
|
38
|
+
.addOption(new commander_1.Option("--dir <dir>", "po file directory").conflicts("po"))
|
39
|
+
.option("-src, --source <lang>", "source language", "English")
|
40
|
+
.option("-l, --lang <lang>", "target language", "Simplified Chinese")
|
41
|
+
.option("--verbose", "print verbose log")
|
40
42
|
.option("-o, --output <file>", "output file path, overwirte po file by default")
|
41
|
-
.action(({ key, host, model, po, source, lang, output }) => __awaiter(void 0, void 0, void 0, function* () {
|
43
|
+
.action(({ key, host, model, po, dir, source, lang, verbose, output }) => __awaiter(void 0, void 0, void 0, function* () {
|
42
44
|
if (host) {
|
43
45
|
process.env.OPENAI_API_HOST = host;
|
44
46
|
}
|
45
47
|
if (key) {
|
46
48
|
process.env.OPENAI_API_KEY = key;
|
47
49
|
}
|
48
|
-
|
50
|
+
if (po) {
|
51
|
+
yield (0, translate_1.translatePo)(model, po, source, lang, verbose, output);
|
52
|
+
}
|
53
|
+
else if (dir) {
|
54
|
+
yield (0, translate_1.translatePoDir)(model, dir, source, lang, verbose, output);
|
55
|
+
}
|
56
|
+
else {
|
57
|
+
console.error("po file or directory is required");
|
58
|
+
process.exit(1);
|
59
|
+
}
|
49
60
|
}));
|
50
61
|
program
|
51
62
|
.command("sync")
|
package/lib/src/translate.d.ts
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
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
|
-
export declare function translatePo(model: string | undefined, po: string, source: string, lang: string, output: string): Promise<void>;
|
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, output: string): Promise<void>;
|
package/lib/src/translate.js
CHANGED
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
9
9
|
});
|
10
10
|
};
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
-
exports.translatePo = exports.translate = exports.init = void 0;
|
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
15
|
const os_1 = require("os");
|
@@ -54,13 +54,12 @@ function translate(text, src, lang, model = "gpt-3.5-turbo") {
|
|
54
54
|
.flat();
|
55
55
|
return openai.createChatCompletion({
|
56
56
|
model,
|
57
|
-
temperature: 0.
|
58
|
-
n: 1,
|
57
|
+
temperature: 0.1,
|
59
58
|
messages: [
|
60
59
|
{ role: "system", content: _systemprompt },
|
61
60
|
{
|
62
61
|
role: "user",
|
63
|
-
content: `Translate
|
62
|
+
content: `Translate the ${src} content I will post later into ${lang}, and keep the untranslated parts such as symbols in the result.`,
|
64
63
|
},
|
65
64
|
{
|
66
65
|
role: "assistant",
|
@@ -75,7 +74,7 @@ function translate(text, src, lang, model = "gpt-3.5-turbo") {
|
|
75
74
|
});
|
76
75
|
}
|
77
76
|
exports.translate = translate;
|
78
|
-
function translatePo(model = "gpt-3.5-turbo", po, source, lang, output) {
|
77
|
+
function translatePo(model = "gpt-3.5-turbo", po, source, lang, verbose, output) {
|
79
78
|
var _a;
|
80
79
|
return __awaiter(this, void 0, void 0, function* () {
|
81
80
|
const potrans = yield (0, utils_1.parsePo)(po);
|
@@ -88,9 +87,10 @@ function translatePo(model = "gpt-3.5-turbo", po, source, lang, output) {
|
|
88
87
|
}
|
89
88
|
}
|
90
89
|
if (list.length == 0) {
|
91
|
-
console.log("
|
90
|
+
console.log("done.");
|
92
91
|
return;
|
93
92
|
}
|
93
|
+
potrans.headers["Last-Translator"] = `gpt-po v${pkg.version}`;
|
94
94
|
let err429 = false;
|
95
95
|
let modified = false;
|
96
96
|
for (let i = 0; i < list.length; i++) {
|
@@ -105,7 +105,12 @@ function translatePo(model = "gpt-3.5-turbo", po, source, lang, output) {
|
|
105
105
|
const res = yield translate(trans.msgid, source, lang, model);
|
106
106
|
trans.msgstr[0] = ((_a = res.data.choices[0].message) === null || _a === void 0 ? void 0 : _a.content) || trans.msgstr[0];
|
107
107
|
modified = true;
|
108
|
+
if (verbose) {
|
109
|
+
console.log(trans.msgid);
|
110
|
+
console.log(trans.msgstr[0]);
|
111
|
+
}
|
108
112
|
(0, utils_1.printProgress)(i + 1, list.length);
|
113
|
+
yield (0, utils_1.compilePo)(potrans, output || po);
|
109
114
|
}
|
110
115
|
catch (error) {
|
111
116
|
if (error.response) {
|
@@ -124,11 +129,21 @@ function translatePo(model = "gpt-3.5-turbo", po, source, lang, output) {
|
|
124
129
|
}
|
125
130
|
}
|
126
131
|
}
|
127
|
-
|
128
|
-
potrans.headers["Last-Translator"] = `gpt-po v${pkg.version}`;
|
129
|
-
yield (0, utils_1.compilePo)(potrans, output || po);
|
130
|
-
}
|
132
|
+
console.log("done.");
|
131
133
|
});
|
132
134
|
}
|
133
135
|
exports.translatePo = translatePo;
|
136
|
+
function translatePoDir(model = "gpt-3.5-turbo", dir, source, lang, verbose, output) {
|
137
|
+
return __awaiter(this, void 0, void 0, function* () {
|
138
|
+
const files = fs.readdirSync(dir);
|
139
|
+
for (const file of files) {
|
140
|
+
if (file.endsWith(".po")) {
|
141
|
+
const po = (0, path_1.join)(dir, file);
|
142
|
+
console.log(`translating ${po}`);
|
143
|
+
yield translatePo(model, po, source, lang, verbose, output);
|
144
|
+
}
|
145
|
+
}
|
146
|
+
});
|
147
|
+
}
|
148
|
+
exports.translatePoDir = translatePoDir;
|
134
149
|
//# sourceMappingURL=translate.js.map
|