A Python tool that automatically translates GNU gettext .po (Portable Object) files using the Google Translate API. This tool is designed to help developers quickly translate their internationalization files while maintaining the proper gettext format.
- 🌍 Automatic translation of
.pofiles using Google Translate API - 🔄 Handles fuzzy translations and updates them with fresh translations
- 📁 Batch processing of entire locale directories
- 🧹 Automatically cleans up fuzzy flags and previous msgid comments
- ⚡ Preserves original
.pofile structure and metadata - 🛡️ Error handling for failed translations
- Python 3.6+
- Google Cloud Translation API key
- GNU gettext
.pofiles
- Clone this repository:
git clone <repository-url>
cd translate- Install the required dependencies:
pip install -r requirements.txt- Set up your Google Translate API key:
- Get an API key from Google Cloud Console
- Enable the Cloud Translation API
- Create a
.envfile in the project root:
GOOGLE_TRANSLATE_API_KEY=your_api_key_herepython translate.py /path/to/your/locale_directoryYour locale directory should follow the standard gettext structure:
locale/
├── en_US/
│ └── LC_MESSAGES/
│ ├── messages.po
│ └── other.po
├── fr_FR/
│ └── LC_MESSAGES/
│ └── messages.po
└── es_ES/
└── LC_MESSAGES/
└── messages.po
# Translate all .po files in the locale directory
python translate.py ./locale
# The tool will process each language directory and translate:
# - Empty msgstr entries
# - Entries marked as fuzzy
# - Entries with previous msgid commentsThe tool processes .po files by:
- Scanning the provided locale directory for language subdirectories
- Finding all
.pofiles inLC_MESSAGESfolders - Identifying entries that need translation:
- Empty
msgstr(untranslated strings) - Entries with
fuzzyflag (uncertain translations) - Entries with previous
msgidcomments (changed source strings)
- Empty
- Translating using Google Translate API
- Cleaning up by removing fuzzy flags and previous msgid comments
- Saving the updated
.pofiles
GOOGLE_TRANSLATE_API_KEY: Your Google Cloud Translation API key (required)
The tool automatically converts locale codes (e.g., en_US) to Google Translate language codes (e.g., en-US) by replacing underscores with hyphens.
- Missing API Key: The script will exit with an error message
- Invalid Directory: Validates that the provided path exists
- Translation Errors: Individual translation failures are logged but don't stop the process
- API Errors: Network and API errors are caught and reported
The tool provides detailed output showing:
- Which files are being processed
- Each translation performed
- Any errors encountered
- Confirmation when files are saved
Example output:
Processing ./locale/fr_FR/LC_MESSAGES/messages.po ...
Translated: 'Hello, world!' -> 'Bonjour le monde!'
Translated: 'Welcome' -> 'Bienvenue'
Saved translations to ./locale/fr_FR/LC_MESSAGES/messages.po
requests: For making HTTP requests to Google Translate APIpolib: For parsing and manipulating.pofilespython-dotenv: For loading environment variables from.envfile
- Requires a valid Google Cloud Translation API key
- API usage may incur costs based on Google's pricing
- Translation quality depends on Google Translate's capabilities
- Does not handle plural forms specially (translates each form independently)
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Setup Guide - Detailed step-by-step setup instructions
- Examples - Practical examples and use cases
- Changelog - Version history and changes
- Environment Example - Example configuration file
- Copy
env.exampleto.envand add your Google Translate API key - Install dependencies:
pip install -r requirements.txt - Run:
python translate.py /path/to/your/locale
For detailed setup instructions, see the Setup Guide.
If you encounter any issues or have questions, please open an issue on GitHub.