Skip to content

Commit

Permalink
version 1.2.0, boost speed and reduce tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanhex53 committed Nov 26, 2024
1 parent 2a5e09e commit 6687880
Show file tree
Hide file tree
Showing 13 changed files with 270 additions and 185 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
buy_me_a_coffee: ryanhex
5 changes: 4 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"tabWidth": 2,
"useTabs": false,
"printWidth": 100
"quoteProps": "preserve",
"printWidth": 110,
"trailingComma": "none",
"semi": true
}
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Changelog

## [1.2.0] - 2024-11-26
- Submit multiple entries per chat to boost speed and reduce tokens
- Command `systemprompt` has been removed
- Bug fixes

## [1.1.2] - 2024-11-12
- Update command options for OpenAI model
- Dependency updates, target es2022, module(Nodejs 18+)

## [1.0.11] - 2023-10-26
- Add `gpt-po remove` command
- improve prompt for translation

## [1.0.9] - 2023-09-08
- Can add dictionaries for each languages
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Translation tool for gettext (po) files that supports custom system prompts and

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

<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>

## Installation

```
Expand All @@ -25,8 +27,6 @@ Set `OPENAI_API_KEY` before using this tool.
- `gpt-po --dir .` Translate all po files in current directory to a designated target language.
- `gpt-po userdict` Modify or view user dictionaries
- `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.
- `gpt-po systemprompt` Modify or view system prompts, if you are not sure how to use it, you can leave it alone
- `gpt-po systemprompt --reset` Reset system prompts

```
Usage: gpt-po [options] [command]
Expand All @@ -40,7 +40,6 @@ Options:
Commands:
translate [options] translate po file (default command)
sync [options] update po from pot file
systemprompt [options] open/edit system prompt
userdict [options] open/edit user dictionary
remove [options] remove po entries by options
help [command] display help for command
Expand Down Expand Up @@ -81,3 +80,14 @@ Options:
-rc, --reference-contains <text> remove entries whose reference contains text, text can be a regular expression like /text/ig
-h, --help display help for command
```

```
Usage: gpt-po sync [options]
update po from pot file
Options:
--po <file> po file path
--pot <file> pot file path
-h, --help display help for command
```
16 changes: 13 additions & 3 deletions README_zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ gettext(po)文件翻译工具,支持自定义系统提示词和用户字典,

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

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

## 安装

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

```
用法: gpt-po [options] [command]
Expand All @@ -42,7 +42,6 @@ npm install gpt-po
命令:
translate [options] 翻译 po 文件(默认命令)
sync [options] 根据 pot 文件更新 po 文件
systemprompt [options] 打开/编辑系统提示
userdict [options] 打开/编辑用户词典
remove [options] 通过选项删除 po 条目
help [command] 显示命令帮助
Expand Down Expand Up @@ -83,3 +82,14 @@ npm install gpt-po
-rc, --reference-contains <text> 删除引用包含文本的条目,文本可以是正则表达式,例如 /text/ig
-h, --help 显示命令帮助
```

```
用法: gpt-po sync [options]
从 pot 文件中更新条目到 po 文件
Options:
--po <file> po 文件路径
--pot <file> pot 文件路径
-h, --help 显示命令帮助
```
15 changes: 8 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gpt-po",
"version": "1.1.2",
"version": "1.2.0",
"description": "command tool for translate po files by gpt",
"main": "lib/src/index.js",
"type": "module",
Expand Down
110 changes: 55 additions & 55 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
#!/usr/bin/env node
#!/usr/bin/env node --no-warnings=ExperimentalWarning

import { Command, Option } from "commander";
import path from "path";
import { fileURLToPath } from "url";
import pkg from "../package.json" with { type: "json" };
import { sync } from "./sync.js";
import { init, translatePo, translatePoDir } from "./translate.js";
import { copyFileIfNotExists, compilePo, findConfig, openFileByDefault, openFileExplorer, parsePo } from "./utils.js";
import {
copyFileIfNotExists,
compilePo,
findConfig,
openFileByDefault,
openFileExplorer,
parsePo
} from "./utils.js";
import { removeByOptions } from "./manipulate.js";

const __filename = fileURLToPath(import.meta.url);
Expand All @@ -29,9 +36,7 @@ program
.option("--verbose", "print verbose log")
.option("--context <file>", "text file that provides the bot additional context")
.addOption(
new Option("-o, --output <file>", "output file path, overwirte po file by default").conflicts(
"dir",
),
new Option("-o, --output <file>", "output file path, overwirte po file by default").conflicts("dir")
)
.action(async (args) => {
const { key, host, model, po, dir, source, lang, verbose, output, context } = args;
Expand Down Expand Up @@ -66,70 +71,65 @@ program
await sync(po, pot);
});

// program command `systemprompt` with help text `open/edit system prompt`
program
.command("systemprompt")
.description("open/edit system prompt")
.option("--reset", "reset system prompt to default")
.action((args) => {
const { reset } = args;
// open `systemprompt.txt` file by system text default editor
const copyFile = __dirname + "/systemprompt.txt";
// user home path
const promptFile = findConfig("systemprompt.txt");
copyFileIfNotExists(promptFile, copyFile, reset);
if (reset) {
console.log("systemprompt.txt reset to default");
}
openFileByDefault(promptFile);
});

// program command `userdict` with help text `open/edit user dictionary`
program
.command("userdict")
.description("open/edit user dictionary")
.option("--explore", "open user dictionary directory")
.option("-l, --lang <lang>", "target language (ISO 639-1)")
.action((args) => {
const { explore } = args;
const { explore, lang } = args;
// open `dictionary.json` file by system text default editor
const copyFile = __dirname + "/dictionary.json";
// user home path
const dictFile = findConfig("dictionary.json");
// find from user home path
const dictFile = findConfig(`dictionary${lang ? "-" + lang : ""}.json`);
if (explore) {
// open user dictionary directory
return openFileExplorer(dictFile);
}
copyFileIfNotExists(dictFile, copyFile);
if (!lang) copyFileIfNotExists(dictFile, copyFile);
openFileByDefault(dictFile);
});

// program command `remove` with help text `remove po entries by options`
program
.command("remove")
.description("remove po entries by options")
.requiredOption("--po <file>", "po file path")
.option("--fuzzy", "remove fuzzy entries")
.option("-obs, --obsolete", "remove obsolete entries")
.option("-ut, --untranslated", "remove untranslated entries")
.option("-t, --translated", "remove translated entries")
.option("-tnf, --translated-not-fuzzy", "remove translated not fuzzy entries")
.option("-ft, --fuzzy-translated", "remove fuzzy translated entries")
.option("-rc, --reference-contains <text>", "remove entries whose reference contains text, text can be a regular expression like /text/ig")
.action(async (args) => {
const { po, fuzzy, obsolete, untranslated, translated, translatedNotFuzzy, fuzzyTranslated, referenceContains } = args;
const options = {
fuzzy,
obsolete,
untranslated,
translated,
translatedNotFuzzy,
fuzzyTranslated,
referenceContains,
};
const output = args.output || po;
const translations = await parsePo(po);
await compilePo(removeByOptions(translations, options), output);
console.log("done")
});
// program command `remove` with help text `remove po entries by options`
program
.command("remove")
.description("remove po entries by options")
.requiredOption("--po <file>", "po file path")
.option("--fuzzy", "remove fuzzy entries")
.option("-obs, --obsolete", "remove obsolete entries")
.option("-ut, --untranslated", "remove untranslated entries")
.option("-t, --translated", "remove translated entries")
.option("-tnf, --translated-not-fuzzy", "remove translated not fuzzy entries")
.option("-ft, --fuzzy-translated", "remove fuzzy translated entries")
.option(
"-rc, --reference-contains <text>",
"remove entries whose reference contains text, text can be a regular expression like /text/ig"
)
.action(async (args) => {
const {
po,
fuzzy,
obsolete,
untranslated,
translated,
translatedNotFuzzy,
fuzzyTranslated,
referenceContains
} = args;
const options = {
fuzzy,
obsolete,
untranslated,
translated,
translatedNotFuzzy,
fuzzyTranslated,
referenceContains
};
const output = args.output || po;
const translations = await parsePo(po);
await compilePo(removeByOptions(translations, options), output);
console.log("done");
});

program.parse(process.argv);
3 changes: 1 addition & 2 deletions src/manipulate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface RemoveByOptions {
*/
export function removeByOptions(
potrans: GetTextTranslations,
options: RemoveByOptions | undefined,
options: RemoveByOptions | undefined
): GetTextTranslations {
const fuzzyRegx = /\bfuzzy\b/;
const obsoleteRegx = /\bobsolete\b/;
Expand Down Expand Up @@ -60,7 +60,6 @@ export function removeByOptions(
delete potrans.translations[ctx][msgid];
}
}

}
}
return potrans;
Expand Down
32 changes: 1 addition & 31 deletions src/systemprompt.txt
Original file line number Diff line number Diff line change
@@ -1,31 +1 @@
You are a language translation expert. You will translate text from one language to another, following these guidelines:

1. **Language Codes**:
- 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`).
- 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.

2. **Placeholder Handling**:
- Maintain the positions of placeholders (e.g., %s, %d, {example}) in the translated text. Do not translate placeholders.

3. **Formatting**:
- Preserve the formatting of untranslatable portions.
- Retain any whitespace at the beginning or end of the message.
- Add or omit a period (.) at the end of your translation to match the incoming message.

4. **XML Tags**:
- Input messages will be wrapped in `<translate>` XML tags.
- Respond with the translated message wrapped in `<translated>` XML tags.

5. **Quality**:
- Translate in a colloquial, professional, and elegant manner without sounding like a machine translation.

6. **Error Handling**:
- If you cannot reliably translate a message, respond without `<translated>` XML tags and provide a short reason why.

**Examples**:
- Input: `<translate>This is a message. </translate>`
- Output: `<translated>Este es un mensaje. </translated>`
- Input: `<translate> Hello %s</translate>`
- Output: `<translated> Hola %s</translated>`

Do not answer questions or explain concepts. Only provide translations within `<translated>` XML tags unless you need to respond with a short error reason.
You are a language translation expert. You will carefully follow the translation guidelines to translate the incoming XML messages from one language to another.
Loading

0 comments on commit 6687880

Please sign in to comment.