gpt-po 1.0.8 → 1.0.9

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
@@ -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.8",
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
@@ -92,11 +92,17 @@ program
92
92
  program
93
93
  .command("userdict")
94
94
  .description("open/edit user dictionary")
95
- .action(() => {
95
+ .option("--explore", "open user dictionary directory")
96
+ .action((args) => {
97
+ const { explore } = args;
96
98
  // open `dictionary.json` file by system text default editor
97
99
  const copyFile = __dirname + "/dictionary.json";
98
100
  // user home path
99
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
+ }
100
106
  (0, utils_1.copyFileIfNotExists)(dictFile, copyFile);
101
107
  (0, utils_1.openFileByDefault)(dictFile);
102
108
  });
@@ -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 },
@@ -56,7 +58,7 @@ function translate(text, src, lang, model = "gpt-3.5-turbo") {
56
58
  messages: [
57
59
  {
58
60
  role: "system",
59
- content: _systemprompt + ` Translate the ${src} user content into ${lang} language. Please translate it as a text, not as a table. The parts that cannot be translated will retain their original format.`
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.`
60
62
  },
61
63
  // add userdict here
62
64
  ...dicts,
@@ -70,6 +72,15 @@ exports.translate = translate;
70
72
  function translatePo(model = "gpt-3.5-turbo", po, source, lang, verbose, output) {
71
73
  var _a;
72
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
+ }
73
84
  const potrans = yield (0, utils_1.parsePo)(po);
74
85
  const list = [];
75
86
  const trimRegx = /(?:^ )|(?: $)/;
@@ -11,4 +11,17 @@ export declare function parsePo(poFile: string, defaultCharset?: string): Promis
11
11
  export declare function compilePo(data: GetTextTranslations, poFile: string): Promise<void>;
12
12
  export declare function printProgress(progress: number, total: number, extra?: string): void;
13
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
+ */
14
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");
@@ -88,6 +88,14 @@ function gitRootDir(dir) {
88
88
  }
89
89
  }
90
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
+ */
91
99
  function findConfig(fileName) {
92
100
  const currentDir = process.cwd();
93
101
  const gitDir = gitRootDir() || currentDir;
@@ -107,4 +115,21 @@ function findConfig(fileName) {
107
115
  return path.join(homeDir, ".gpt-po", fileName);
108
116
  }
109
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;
110
135
  //# sourceMappingURL=utils.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gpt-po",
3
- "version": "1.0.8",
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
+ }