gpt-po 1.0.3 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # PO File Translation Tool for ChatGPT
2
2
 
3
+ [![NPM version](https://img.shields.io/npm/v/gpt-po.svg)](https://npmjs.org/package/gpt-po)
4
+ [![Downloads](https://img.shields.io/npm/dm/gpt-po.svg)](https://npmjs.org/package/gpt-po)
5
+
3
6
  Translation tool for gettext (po) files that supports custom system prompts and user dictionaries. It also supports translating specified po files to a designated target language and updating po files based on pot files.
4
7
 
5
8
  Read in other languages: English | [简体中文](./README_zh-CN.md)
@@ -18,6 +21,7 @@ Set `OPENAI_API_KEY` before using this tool.
18
21
 
19
22
  - `gpt-po sync --po <file> --pot <file>` Update the po file based on the pot file, while preserving the original translations.
20
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.
21
25
  - `gpt-po userdict` Modify or view user dictionaries
22
26
  - `gpt-po systemprompt` Modify or view system prompts, if you are not sure how to use it, you can leave it alone
23
27
 
@@ -49,6 +53,9 @@ Options:
49
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",
50
54
  default: "gpt-3.5-turbo")
51
55
  --po <file> po file path
56
+ --dir <dir> po file directory
57
+ -src, --source <lang> source language (default: "english")
58
+ --verbose show verbose log
52
59
  -l, --lang <lang> target language (default: "simplified chinese")
53
60
  -o, --output <file> output file path, overwirte po file by default
54
61
  -h, --help display help for command
package/README_zh-CN.md CHANGED
@@ -1,8 +1,11 @@
1
1
  # PO文件CHATGPT翻译工具
2
2
 
3
+ [![NPM version](https://img.shields.io/npm/v/gpt-po.svg)](https://npmjs.org/package/gpt-po)
4
+ [![Downloads](https://img.shields.io/npm/dm/gpt-po.svg)](https://npmjs.org/package/gpt-po)
5
+
3
6
  gettext(po)文件翻译工具,支持自定义系统提示词和用户字典,支持翻译指定的po文件到指定的目标语言,支持根据pot文件更新po文件。
4
7
 
5
- 使用其他语言阅读:[English](./Readme.md) | 简体中文
8
+ 使用其他语言阅读:[English](./README.md) | 简体中文
6
9
 
7
10
  ## 安装
8
11
 
@@ -18,6 +21,7 @@ npm install gpt-po
18
21
 
19
22
  - `gpt-po sync --po <file> --pot <file>` 根据pot文件更新po文件,保留原有翻译
20
23
  - `gpt-po --po <file>` 翻译指定的po文件到指定的目标语言,默认目标语言是简体中文
24
+ - `gpt-po --dir .` 翻译当前目录下的所有po文件到指定的目标语言,默认目标语言是简体中文
21
25
  - `gpt-po userdict` 修改或查看用户字典
22
26
  - `gpt-po systemprompt` 修改或查看系统提示词,如果你不确定如何使用,可以不用修改
23
27
 
@@ -49,6 +53,9 @@ Options:
49
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",
50
54
  default: "gpt-3.5-turbo")
51
55
  --po <file> po file path
56
+ --dir <dir> po files directory
57
+ -src, --source <lang> source language (default: "english")
58
+ --verbose show verbose log
52
59
  -l, --lang <lang> target language (default: "simplified chinese")
53
60
  -o, --output <file> output file path, overwirte po file by default
54
61
  -h, --help display help for command
package/lib/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gpt-po",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "command tool for translate po files by gpt",
5
5
  "main": "lib/src/index.js",
6
6
  "bin": {
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
- .requiredOption("--po <file>", "po file path")
38
- .option("-src, --source <lang>", "source language", "english")
39
- .requiredOption("-l, --lang <lang>", "target language", "simplified chinese")
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
- yield (0, translate_1.translatePo)(model, po, source, lang, output);
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")
@@ -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>;
@@ -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.5,
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 incoming ${src} content into ${lang}, only the translated content can be returned, don't ask question.`,
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("nothing needs translate");
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
- if (modified) {
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
package/lib/src/utils.js CHANGED
@@ -36,7 +36,7 @@ function parsePo(poFile, defaultCharset) {
36
36
  }
37
37
  exports.parsePo = parsePo;
38
38
  function compilePo(data, poFile) {
39
- const buffer = gettext_parser_1.po.compile(data);
39
+ const buffer = gettext_parser_1.po.compile(data, { foldLength: 120 });
40
40
  return new Promise((resolve, reject) => {
41
41
  fs.writeFile(poFile, buffer, (err) => {
42
42
  if (err)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gpt-po",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "command tool for translate po files by gpt",
5
5
  "main": "lib/src/index.js",
6
6
  "bin": {