A simple Python script for translating .po files with googletrans.
The script reads a PO file, translates messages whose msgstr is empty, and saves the translated text to the corresponding msgstr. Messages that already have a translation are skipped.
- Docker
- Python 3.12
- Dependencies listed in
requirements.txt - Docker (optional, if you prefer to use the included
Dockerfile)
This project uses googletrans==4.0.0-rc1, which is not compatible with Python 3.13 and later. The pinned release also relies on older HTTP dependencies.
Build the Docker Image:
docker build -t translation-tool .docker run --rm -it `
-v "${PWD}/translation_input:/app/translation_input" `
-v "${PWD}/translation_output:/app/translation_output" `
translation-toolPlace the .po file you want to translate in translation_input/. For example:
translation_input/messages.po
When prompted, enter the PO filename (not the full path). The script looks for it in translation_input/:
Enter the PO filename, for example example_messages.po: example_messages.po
Then enter the destination language code:
Enter the destination language code, for example fr, de, hi, or es: fr
Some example language codes:
| Code | Language |
|---|---|
fr |
French |
de |
German |
hi |
Hindi |
es |
Spanish |
it |
Italian |
pt |
Portuguese |
nl |
Dutch |
The script validates the language code before translating.
Translated files are saved in translation_output/. The destination language code is appended to the output filename.
For example, translating translation_input/example_messages.po to French creates:
translation_output/example_messages_fr.po
Translation errors are logged to translation.log.
translation_tool/
├── Dockerfile
├── examples/
│ └── example_messages.po
├── translation_input/
│ └── .gitkeep
├── translation_output/
│ └── .gitkeep
├── .gitignore
├── README.md
├── requirements.txt
└── script.py
examples/contains a reference PO file.translation_input/contains files to be translated.translation_output/contains generated translations.- Files placed in
translation_input/and generated intranslation_output/are ignored by Git.
This project deliberately uses googletrans==4.0.0-rc1. googletrans is an unofficial library that communicates with Google Translate and does not require a Google Cloud Translation API key. It depends on undocumented Google Translate behavior, so it may stop working if Google changes the service.
If you see this error:
ModuleNotFoundError: No module named 'cgi'
Check your Python version:
python --versionYou are likely running Python 3.13 or later. Use Python 3.12 for this project.