gpt-po 1.0.7 → 1.0.9

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -21,9 +21,12 @@ 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 --po <file> --lang <lang>` Translate specified po files to a designated target language.
24
25
  - `gpt-po --dir .` Translate all po files in current directory to a designated target language.
25
26
  - `gpt-po userdict` Modify or view user dictionaries
27
+ - `gpt-po userdict --explore` Explore user dictionaries, if you want add new dictionaries or modify existing dictionaries. dictionaries can be named as `dictionary-<lang>.json`, for example, `dictionary-simplified-chinese.json` is the dictionary for Simplified Chinese.
26
28
  - `gpt-po systemprompt` Modify or view system prompts, if you are not sure how to use it, you can leave it alone
29
+ - `gpt-po systemprompt --reset` Reset system prompts
27
30
 
28
31
  ```
29
32
  Usage: gpt-po [command] [options]
package/README_zh-CN.md CHANGED
@@ -21,9 +21,12 @@ 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 --po <file> --lang <lang>` Translate specified po files to a designated target language.
24
25
  - `gpt-po --dir .` 翻译当前目录下的所有po文件到指定的目标语言,默认目标语言是简体中文
25
26
  - `gpt-po userdict` 修改或查看用户字典
27
+ - `gpt-po userdict --explore` 用于浏览字典目录, 如果你想要增加或修改现有的字典。字典可以按`dictionary-<lang>.json`格式命令, 例如, `dictionary-simplified-chinese.json` 是针对简体中文的字典.
26
28
  - `gpt-po systemprompt` 修改或查看系统提示词,如果你不确定如何使用,可以不用修改
29
+ - `gpt-po systemprompt --reset` 重置系统提示词
27
30
 
28
31
  ```
29
32
  Usage: gpt-po [command] [options]
package/lib/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gpt-po",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "command tool for translate po files by gpt",
5
5
  "main": "lib/src/index.js",
6
6
  "bin": {
@@ -37,7 +37,7 @@
37
37
  "@types/jest": "^29.5.1",
38
38
  "jest": "^29.5.0",
39
39
  "ncp": "^2.0.0",
40
- "prettier": "^3.0.0-alpha.7-for-vscode",
40
+ "prettier": "^3.0.3",
41
41
  "rimraf": "^5.0.0",
42
42
  "ts-jest": "^29.1.0",
43
43
  "tslint": "^6.1.3",
package/lib/src/index.js CHANGED
@@ -46,6 +46,11 @@ program
46
46
  if (key) {
47
47
  process.env.OPENAI_API_KEY = key;
48
48
  }
49
+ // process.env.OPENAI_API_KEY is not set, exit
50
+ if (!process.env.OPENAI_API_KEY) {
51
+ console.error("OPENAI_API_KEY is required");
52
+ process.exit(1);
53
+ }
49
54
  (0, translate_1.init)();
50
55
  if (po) {
51
56
  yield (0, translate_1.translatePo)(model, po, source, lang, verbose, output);
@@ -70,23 +75,34 @@ program
70
75
  program
71
76
  .command("systemprompt")
72
77
  .description("open/edit system prompt")
73
- .action(() => {
78
+ .option("--reset", "reset system prompt to default")
79
+ .action((args) => {
80
+ const { reset } = args;
74
81
  // open `systemprompt.txt` file by system text default editor
75
82
  const copyFile = __dirname + "/systemprompt.txt";
76
83
  // user home path
77
84
  const promptFile = (0, utils_1.findConfig)("systemprompt.txt");
78
- (0, utils_1.copyFileIfNotExists)(promptFile, copyFile);
85
+ (0, utils_1.copyFileIfNotExists)(promptFile, copyFile, reset);
86
+ if (reset) {
87
+ console.log("systemprompt.txt reset to default");
88
+ }
79
89
  (0, utils_1.openFileByDefault)(promptFile);
80
90
  });
81
91
  // program command `userdict` with help text `open/edit user dictionary`
82
92
  program
83
93
  .command("userdict")
84
94
  .description("open/edit user dictionary")
85
- .action(() => {
95
+ .option("--explore", "open user dictionary directory")
96
+ .action((args) => {
97
+ const { explore } = args;
86
98
  // open `dictionary.json` file by system text default editor
87
99
  const copyFile = __dirname + "/dictionary.json";
88
100
  // user home path
89
101
  const dictFile = (0, utils_1.findConfig)("dictionary.json");
102
+ if (explore) {
103
+ // open user dictionary directory
104
+ return (0, utils_1.openFileExplorer)(dictFile);
105
+ }
90
106
  (0, utils_1.copyFileIfNotExists)(dictFile, copyFile);
91
107
  (0, utils_1.openFileByDefault)(dictFile);
92
108
  });
@@ -1 +1 @@
1
- You are a translation expert, follow the user's instructions carefully, translate the text in a colloquial, professional and elegant manner without sounding like a machine translation. Remember to only translate the text and not further interpret it.
1
+ You are a translation expert, translate the text in a colloquial, professional and elegant manner without sounding like a machine translation. Remember to only translate the text and not interpret it further.
@@ -38,13 +38,15 @@ function init(force) {
38
38
  if (!_userdict || force) {
39
39
  const userdict = (0, utils_1.findConfig)("dictionary.json");
40
40
  (0, utils_1.copyFileIfNotExists)(userdict, (0, path_1.join)(__dirname, "dictionary.json"));
41
- _userdict = JSON.parse(fs.readFileSync(userdict, "utf-8"));
41
+ _userdict = { "default": JSON.parse(fs.readFileSync(userdict, "utf-8")) };
42
42
  }
43
43
  return _openai;
44
44
  }
45
45
  exports.init = init;
46
46
  function translate(text, src, lang, model = "gpt-3.5-turbo") {
47
- const dicts = Object.entries(_userdict)
47
+ const lang_code = lang.toLowerCase().trim().replace(/[\W_]+/g, "-");
48
+ const dicts = Object.entries(_userdict[lang_code] || _userdict["default"])
49
+ .filter(([k, _]) => text.toLowerCase().includes(k.toLowerCase()))
48
50
  .map(([k, v]) => [
49
51
  { role: "user", content: k },
50
52
  { role: "assistant", content: v },
@@ -54,14 +56,9 @@ function translate(text, src, lang, model = "gpt-3.5-turbo") {
54
56
  model,
55
57
  temperature: 0.1,
56
58
  messages: [
57
- { role: "system", content: _systemprompt },
58
59
  {
59
- role: "user",
60
- content: `Translate the ${src} content I will post later into ${lang}, and keep the untranslated parts such as symbols in the result.`,
61
- },
62
- {
63
- role: "assistant",
64
- content: "Sure, Please send me the content that needs to be translated.",
60
+ role: "system",
61
+ content: _systemprompt + ` Translate the ${src} content provided by the user into ${lang}. Please translate it as a text, not as a table. The parts that cannot be translated will retain their original format.`
65
62
  },
66
63
  // add userdict here
67
64
  ...dicts,
@@ -75,6 +72,15 @@ exports.translate = translate;
75
72
  function translatePo(model = "gpt-3.5-turbo", po, source, lang, verbose, output) {
76
73
  var _a;
77
74
  return __awaiter(this, void 0, void 0, function* () {
75
+ // try to load dictionary by lang-code if it not loaded
76
+ const lang_code = lang.toLowerCase().trim().replace(/[\W_]+/g, "-");
77
+ if (!_userdict[lang_code]) {
78
+ const lang_dic_file = (0, utils_1.findConfig)(`dictionary-${lang_code}.json`);
79
+ if (fs.existsSync(lang_dic_file)) {
80
+ _userdict[lang_code] = JSON.parse(fs.readFileSync(lang_dic_file, "utf-8"));
81
+ console.log(`dictionary-${lang_code}.json is loaded.`);
82
+ }
83
+ }
78
84
  const potrans = yield (0, utils_1.parsePo)(po);
79
85
  const list = [];
80
86
  const trimRegx = /(?:^ )|(?: $)/;
@@ -136,6 +142,9 @@ function translatePo(model = "gpt-3.5-turbo", po, source, lang, verbose, output)
136
142
  }
137
143
  else {
138
144
  console.error(error.message);
145
+ if (error.code == "ECONNABORTED") {
146
+ console.log('you may need to set "HTTPS_PROXY" to reach openai api.');
147
+ }
139
148
  }
140
149
  }
141
150
  }
@@ -3,11 +3,25 @@ import { GetTextTranslations } from "gettext-parser";
3
3
  * copy source file to destination file if destination file does not exist
4
4
  * @param file destination file path
5
5
  * @param copyFile source file path
6
+ * @param force force copy file
6
7
  */
7
- export declare function copyFileIfNotExists(file: string, copyFile: string): void;
8
+ export declare function copyFileIfNotExists(file: string, copyFile: string, force?: boolean): void;
8
9
  export declare function openFileByDefault(filePath: string): void;
9
10
  export declare function parsePo(poFile: string, defaultCharset?: string): Promise<GetTextTranslations>;
10
11
  export declare function compilePo(data: GetTextTranslations, poFile: string): Promise<void>;
11
12
  export declare function printProgress(progress: number, total: number, extra?: string): void;
12
13
  export declare function gitRootDir(dir?: string): string | null;
14
+ /**
15
+ * find config file in the following order:
16
+ * 1. current directory
17
+ * 2. git root directory
18
+ * 3. home directory
19
+ * @param fileName
20
+ * @returns full path of the config file
21
+ */
13
22
  export declare function findConfig(fileName: string): string;
23
+ /**
24
+ * open file explorer by platform
25
+ * @param location folder or file path
26
+ */
27
+ export declare function openFileExplorer(location: string): void;
package/lib/src/utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.findConfig = exports.gitRootDir = exports.printProgress = exports.compilePo = exports.parsePo = exports.openFileByDefault = exports.copyFileIfNotExists = void 0;
3
+ exports.openFileExplorer = exports.findConfig = exports.gitRootDir = exports.printProgress = exports.compilePo = exports.parsePo = exports.openFileByDefault = exports.copyFileIfNotExists = void 0;
4
4
  const child_process_1 = require("child_process");
5
5
  const fs = require("fs");
6
6
  const gettext_parser_1 = require("gettext-parser");
@@ -10,15 +10,18 @@ const path = require("path");
10
10
  * copy source file to destination file if destination file does not exist
11
11
  * @param file destination file path
12
12
  * @param copyFile source file path
13
+ * @param force force copy file
13
14
  */
14
- function copyFileIfNotExists(file, copyFile) {
15
+ function copyFileIfNotExists(file, copyFile, force = false) {
15
16
  // make sure the directory exists
16
17
  fs.mkdirSync(path.dirname(file), { recursive: true });
17
18
  // check if file exists else create it
18
19
  try {
19
20
  fs.accessSync(file, fs.constants.F_OK);
20
- // check if the file is empty else copy the file
21
- fs.statSync(file).size === 0 && fs.copyFileSync(copyFile, file);
21
+ // check if the file is empty or force, copy the file
22
+ if (force || fs.statSync(file).size === 0) {
23
+ fs.copyFileSync(copyFile, file);
24
+ }
22
25
  }
23
26
  catch (err) {
24
27
  fs.copyFileSync(copyFile, file);
@@ -85,6 +88,14 @@ function gitRootDir(dir) {
85
88
  }
86
89
  }
87
90
  exports.gitRootDir = gitRootDir;
91
+ /**
92
+ * find config file in the following order:
93
+ * 1. current directory
94
+ * 2. git root directory
95
+ * 3. home directory
96
+ * @param fileName
97
+ * @returns full path of the config file
98
+ */
88
99
  function findConfig(fileName) {
89
100
  const currentDir = process.cwd();
90
101
  const gitDir = gitRootDir() || currentDir;
@@ -104,4 +115,21 @@ function findConfig(fileName) {
104
115
  return path.join(homeDir, ".gpt-po", fileName);
105
116
  }
106
117
  exports.findConfig = findConfig;
118
+ /**
119
+ * open file explorer by platform
120
+ * @param location folder or file path
121
+ */
122
+ function openFileExplorer(location) {
123
+ if ((0, os_1.platform)() === 'win32') {
124
+ (0, child_process_1.exec)(`explorer.exe "${path.dirname(location)}"`);
125
+ }
126
+ else if ((0, os_1.platform)() === 'darwin') {
127
+ (0, child_process_1.exec)(`open "${path.dirname(location)}"`);
128
+ }
129
+ else {
130
+ // Assuming a Linux-based system
131
+ (0, child_process_1.exec)(`xdg-open "${path.dirname(location)}"`);
132
+ }
133
+ }
134
+ exports.openFileExplorer = openFileExplorer;
107
135
  //# sourceMappingURL=utils.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gpt-po",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "description": "command tool for translate po files by gpt",
5
5
  "main": "lib/src/index.js",
6
6
  "bin": {
@@ -37,7 +37,7 @@
37
37
  "@types/jest": "^29.5.1",
38
38
  "jest": "^29.5.0",
39
39
  "ncp": "^2.0.0",
40
- "prettier": "^3.0.0-alpha.7-for-vscode",
40
+ "prettier": "^3.0.3",
41
41
  "rimraf": "^5.0.0",
42
42
  "ts-jest": "^29.1.0",
43
43
  "tslint": "^6.1.3",
@@ -52,4 +52,4 @@
52
52
  "engines": {
53
53
  "node": ">=12.0.0"
54
54
  }
55
- }
55
+ }