Potr (Po Translator) is a command line tool for translating Gettext PO files.
Currently, it supports translation using OpenAI, Azure OpenAI Service, and DeepL.
bash
cargo install potr
To start translating the PO files and update the original file inplace, we can use the potr
command:
bash
potr -p <po-file> -e <engine> -t <target-languange> -k <api-key> ...
The target languange is defined using the ISO 639-1 code, e.g. en = English, zh = Chinese, de = German, fr = French, etc.
For examples:
```bash
potr -p en.po -e deepl -t en -k
potr -p en.po -e openai -t en -k
Translation might take long time, but no worries, we can use Ctrl + C
to stop processing further messages and the translated messages will be saved to the PO file.
By default, potr will skip certain messages, such as translated messages and code blocks (message starts with ```
), we can use certain flags to control this behavior:
--pt
or --process-translated
: Process translated messages--pc
or --process-code-blocks
: Process code blocks--skip-text
: Skip normal text messages (non-code-blocks)--st
or --skip-translation
: Skip translation for all messages. This is useful when we only want to update the PO file with the current message to see format changes, etc.-l
or `--limit``: Limit the number of messages to translate. This is useful for testing purpose.```bash
potr -p en.po -e openai -t en -k
We can also specify the API key for each translation service using environment variables:
```bash
export POTRAPIKEY_DEEPL="..."
export POTRAPIKEY_OPENAI="..."
export POTRAPIKEYAZUREOPENAI="..." export POTRAPIBASEAZUREOPENAI="..." export POTRAPIDEPLOYMENTIDAZURE_OPENAI="..." ```
Or, in Powershell on Windows:
```powershell
$env:POTRAPIKEY_DEEPL="..."
$env:POTRAPIKEY_OPENAI="..."
$env:POTRAPIKEYAZUREOPENAI="..." $env:POTRAPIBASEAZUREOPENAI="..." $env:POTRAPIDEPLOYMENTIDAZURE_OPENAI="..." ```
Beside translating messages in PO files, Potr also includes 2 tools for manipulating messages in PO files: Clear
and Clone
. Using the message skipping flags mentioned above, we can use these tools to clean up the PO files or clone certain messages in the PO files.
```bash
potr -p en.po -e clear --pt
potr -p en.po -e clone --pt --pc --skip-text ```
mdbook-i18n-helpers
is an awesome tool for translating mdbook projects. We can use potr
to translate the PO files generated by mdbook-i18n-helpers
:
```bash
MDBOOK_OUTPUT='{"xgettext": {"pot-file": "messages.pot"}}' mdbook build -d po
msgmerge --update po/en.po po/messages.pot
potr -p po/en.po -e openai -t en -k
Potr is written in Rust. Building Potr is just like all the other rust projects:
bash
cargo build
For running unit tests in Potr, we also need to have a valid API key for each translation service. The API keys are fetched from environment variables in the same way as we setup for the potr
command. Please see "Usage" section above for more details.
Then, we can run the tests:
bash
cargo test
Apache-2.0: https://www.apache.org/licenses/LICENSE-2.0