Skip to content

Commit 6687880

Browse files
committed
version 1.2.0, boost speed and reduce tokens
1 parent 2a5e09e commit 6687880

File tree

13 files changed

+270
-185
lines changed

13 files changed

+270
-185
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
buy_me_a_coffee: ryanhex

.prettierrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"tabWidth": 2,
33
"useTabs": false,
4-
"printWidth": 100
4+
"quoteProps": "preserve",
5+
"printWidth": 110,
6+
"trailingComma": "none",
7+
"semi": true
58
}

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Changelog
2+
3+
## [1.2.0] - 2024-11-26
4+
- Submit multiple entries per chat to boost speed and reduce tokens
5+
- Command `systemprompt` has been removed
6+
- Bug fixes
7+
8+
## [1.1.2] - 2024-11-12
9+
- Update command options for OpenAI model
10+
- Dependency updates, target es2022, module(Nodejs 18+)
11+
12+
## [1.0.11] - 2023-10-26
13+
- Add `gpt-po remove` command
14+
- improve prompt for translation
15+
16+
## [1.0.9] - 2023-09-08
17+
- Can add dictionaries for each languages

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Translation tool for gettext (po) files that supports custom system prompts and
77

88
Read in other languages: English | [简体中文](./README_zh-CN.md)
99

10+
<a href="https://buymeacoffee.com/ryanhex" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-red.png" alt="Buy Me A Coffee" height="41" width="174"></a>
11+
1012
## Installation
1113

1214
```
@@ -25,8 +27,6 @@ Set `OPENAI_API_KEY` before using this tool.
2527
- `gpt-po --dir .` Translate all po files in current directory to a designated target language.
2628
- `gpt-po userdict` Modify or view user dictionaries
2729
- `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-zh.json` is the dictionary for Simplified Chinese.
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
3030

3131
```
3232
Usage: gpt-po [options] [command]
@@ -40,7 +40,6 @@ Options:
4040
Commands:
4141
translate [options] translate po file (default command)
4242
sync [options] update po from pot file
43-
systemprompt [options] open/edit system prompt
4443
userdict [options] open/edit user dictionary
4544
remove [options] remove po entries by options
4645
help [command] display help for command
@@ -81,3 +80,14 @@ Options:
8180
-rc, --reference-contains <text> remove entries whose reference contains text, text can be a regular expression like /text/ig
8281
-h, --help display help for command
8382
```
83+
84+
```
85+
Usage: gpt-po sync [options]
86+
87+
update po from pot file
88+
89+
Options:
90+
--po <file> po file path
91+
--pot <file> pot file path
92+
-h, --help display help for command
93+
```

README_zh-CN.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ gettext(po)文件翻译工具,支持自定义系统提示词和用户字典,
77

88
使用其他语言阅读:[English](./README.md) | 简体中文
99

10+
<a href="https://buymeacoffee.com/ryanhex" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-red.png" alt="请我喝杯咖啡" height="41" width="174"></a>
11+
1012
## 安装
1113

1214
```
@@ -27,8 +29,6 @@ npm install gpt-po
2729
- `gpt-po --dir .` 将当前目录下的所有 po 文件翻译成目标语言。
2830
- `gpt-po userdict` 修改或查看用户词典。
2931
- `gpt-po userdict --explore` 浏览用户词典,如果您想添加新词典或修改现有词典,词典可以命名为 `dictionary-<lang>.json`,例如 `dictionary-zh.json` 是简体中文词典。
30-
- `gpt-po systemprompt` 修改或查看系统提示,如果您不确定如何使用它,可以忽略。
31-
- `gpt-po systemprompt --reset` 重置系统提示。
3232

3333
```
3434
用法: gpt-po [options] [command]
@@ -42,7 +42,6 @@ npm install gpt-po
4242
命令:
4343
translate [options] 翻译 po 文件(默认命令)
4444
sync [options] 根据 pot 文件更新 po 文件
45-
systemprompt [options] 打开/编辑系统提示
4645
userdict [options] 打开/编辑用户词典
4746
remove [options] 通过选项删除 po 条目
4847
help [command] 显示命令帮助
@@ -83,3 +82,14 @@ npm install gpt-po
8382
-rc, --reference-contains <text> 删除引用包含文本的条目,文本可以是正则表达式,例如 /text/ig
8483
-h, --help 显示命令帮助
8584
```
85+
86+
```
87+
用法: gpt-po sync [options]
88+
89+
从 pot 文件中更新条目到 po 文件
90+
91+
Options:
92+
--po <file> po 文件路径
93+
--pot <file> pot 文件路径
94+
-h, --help 显示命令帮助
95+
```

package-lock.json

Lines changed: 8 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gpt-po",
3-
"version": "1.1.2",
3+
"version": "1.2.0",
44
"description": "command tool for translate po files by gpt",
55
"main": "lib/src/index.js",
66
"type": "module",

src/index.ts

Lines changed: 55 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
#!/usr/bin/env node
1+
#!/usr/bin/env node --no-warnings=ExperimentalWarning
22

33
import { Command, Option } from "commander";
44
import path from "path";
55
import { fileURLToPath } from "url";
66
import pkg from "../package.json" with { type: "json" };
77
import { sync } from "./sync.js";
88
import { init, translatePo, translatePoDir } from "./translate.js";
9-
import { copyFileIfNotExists, compilePo, findConfig, openFileByDefault, openFileExplorer, parsePo } from "./utils.js";
9+
import {
10+
copyFileIfNotExists,
11+
compilePo,
12+
findConfig,
13+
openFileByDefault,
14+
openFileExplorer,
15+
parsePo
16+
} from "./utils.js";
1017
import { removeByOptions } from "./manipulate.js";
1118

1219
const __filename = fileURLToPath(import.meta.url);
@@ -29,9 +36,7 @@ program
2936
.option("--verbose", "print verbose log")
3037
.option("--context <file>", "text file that provides the bot additional context")
3138
.addOption(
32-
new Option("-o, --output <file>", "output file path, overwirte po file by default").conflicts(
33-
"dir",
34-
),
39+
new Option("-o, --output <file>", "output file path, overwirte po file by default").conflicts("dir")
3540
)
3641
.action(async (args) => {
3742
const { key, host, model, po, dir, source, lang, verbose, output, context } = args;
@@ -66,70 +71,65 @@ program
6671
await sync(po, pot);
6772
});
6873

69-
// program command `systemprompt` with help text `open/edit system prompt`
70-
program
71-
.command("systemprompt")
72-
.description("open/edit system prompt")
73-
.option("--reset", "reset system prompt to default")
74-
.action((args) => {
75-
const { reset } = args;
76-
// open `systemprompt.txt` file by system text default editor
77-
const copyFile = __dirname + "/systemprompt.txt";
78-
// user home path
79-
const promptFile = findConfig("systemprompt.txt");
80-
copyFileIfNotExists(promptFile, copyFile, reset);
81-
if (reset) {
82-
console.log("systemprompt.txt reset to default");
83-
}
84-
openFileByDefault(promptFile);
85-
});
86-
8774
// program command `userdict` with help text `open/edit user dictionary`
8875
program
8976
.command("userdict")
9077
.description("open/edit user dictionary")
9178
.option("--explore", "open user dictionary directory")
79+
.option("-l, --lang <lang>", "target language (ISO 639-1)")
9280
.action((args) => {
93-
const { explore } = args;
81+
const { explore, lang } = args;
9482
// open `dictionary.json` file by system text default editor
9583
const copyFile = __dirname + "/dictionary.json";
96-
// user home path
97-
const dictFile = findConfig("dictionary.json");
84+
// find from user home path
85+
const dictFile = findConfig(`dictionary${lang ? "-" + lang : ""}.json`);
9886
if (explore) {
9987
// open user dictionary directory
10088
return openFileExplorer(dictFile);
10189
}
102-
copyFileIfNotExists(dictFile, copyFile);
90+
if (!lang) copyFileIfNotExists(dictFile, copyFile);
10391
openFileByDefault(dictFile);
10492
});
10593

106-
// program command `remove` with help text `remove po entries by options`
107-
program
108-
.command("remove")
109-
.description("remove po entries by options")
110-
.requiredOption("--po <file>", "po file path")
111-
.option("--fuzzy", "remove fuzzy entries")
112-
.option("-obs, --obsolete", "remove obsolete entries")
113-
.option("-ut, --untranslated", "remove untranslated entries")
114-
.option("-t, --translated", "remove translated entries")
115-
.option("-tnf, --translated-not-fuzzy", "remove translated not fuzzy entries")
116-
.option("-ft, --fuzzy-translated", "remove fuzzy translated entries")
117-
.option("-rc, --reference-contains <text>", "remove entries whose reference contains text, text can be a regular expression like /text/ig")
118-
.action(async (args) => {
119-
const { po, fuzzy, obsolete, untranslated, translated, translatedNotFuzzy, fuzzyTranslated, referenceContains } = args;
120-
const options = {
121-
fuzzy,
122-
obsolete,
123-
untranslated,
124-
translated,
125-
translatedNotFuzzy,
126-
fuzzyTranslated,
127-
referenceContains,
128-
};
129-
const output = args.output || po;
130-
const translations = await parsePo(po);
131-
await compilePo(removeByOptions(translations, options), output);
132-
console.log("done")
133-
});
94+
// program command `remove` with help text `remove po entries by options`
95+
program
96+
.command("remove")
97+
.description("remove po entries by options")
98+
.requiredOption("--po <file>", "po file path")
99+
.option("--fuzzy", "remove fuzzy entries")
100+
.option("-obs, --obsolete", "remove obsolete entries")
101+
.option("-ut, --untranslated", "remove untranslated entries")
102+
.option("-t, --translated", "remove translated entries")
103+
.option("-tnf, --translated-not-fuzzy", "remove translated not fuzzy entries")
104+
.option("-ft, --fuzzy-translated", "remove fuzzy translated entries")
105+
.option(
106+
"-rc, --reference-contains <text>",
107+
"remove entries whose reference contains text, text can be a regular expression like /text/ig"
108+
)
109+
.action(async (args) => {
110+
const {
111+
po,
112+
fuzzy,
113+
obsolete,
114+
untranslated,
115+
translated,
116+
translatedNotFuzzy,
117+
fuzzyTranslated,
118+
referenceContains
119+
} = args;
120+
const options = {
121+
fuzzy,
122+
obsolete,
123+
untranslated,
124+
translated,
125+
translatedNotFuzzy,
126+
fuzzyTranslated,
127+
referenceContains
128+
};
129+
const output = args.output || po;
130+
const translations = await parsePo(po);
131+
await compilePo(removeByOptions(translations, options), output);
132+
console.log("done");
133+
});
134134

135135
program.parse(process.argv);

src/manipulate.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export interface RemoveByOptions {
1515
*/
1616
export function removeByOptions(
1717
potrans: GetTextTranslations,
18-
options: RemoveByOptions | undefined,
18+
options: RemoveByOptions | undefined
1919
): GetTextTranslations {
2020
const fuzzyRegx = /\bfuzzy\b/;
2121
const obsoleteRegx = /\bobsolete\b/;
@@ -60,7 +60,6 @@ export function removeByOptions(
6060
delete potrans.translations[ctx][msgid];
6161
}
6262
}
63-
6463
}
6564
}
6665
return potrans;

src/systemprompt.txt

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1 @@
1-
You are a language translation expert. You will translate text from one language to another, following these guidelines:
2-
3-
1. **Language Codes**:
4-
- If provided with an ISO 639 two-letter language code (e.g., `de`), use the language’s main dialect (e.g., `de` is equivalent to `de_DE`).
5-
- If provided with both an ISO 639 language code and an ISO 3166 country code (e.g., `es_MX`), translate into that specific regional dialect.
6-
7-
2. **Placeholder Handling**:
8-
- Maintain the positions of placeholders (e.g., %s, %d, {example}) in the translated text. Do not translate placeholders.
9-
10-
3. **Formatting**:
11-
- Preserve the formatting of untranslatable portions.
12-
- Retain any whitespace at the beginning or end of the message.
13-
- Add or omit a period (.) at the end of your translation to match the incoming message.
14-
15-
4. **XML Tags**:
16-
- Input messages will be wrapped in `<translate>` XML tags.
17-
- Respond with the translated message wrapped in `<translated>` XML tags.
18-
19-
5. **Quality**:
20-
- Translate in a colloquial, professional, and elegant manner without sounding like a machine translation.
21-
22-
6. **Error Handling**:
23-
- If you cannot reliably translate a message, respond without `<translated>` XML tags and provide a short reason why.
24-
25-
**Examples**:
26-
- Input: `<translate>This is a message. </translate>`
27-
- Output: `<translated>Este es un mensaje. </translated>`
28-
- Input: `<translate> Hello %s</translate>`
29-
- Output: `<translated> Hola %s</translated>`
30-
31-
Do not answer questions or explain concepts. Only provide translations within `<translated>` XML tags unless you need to respond with a short error reason.
1+
You are a language translation expert. You will carefully follow the translation guidelines to translate the incoming XML messages from one language to another.

0 commit comments

Comments
 (0)