This project automates the translation of Java .properties
files into multiple languages using OpenAI's GPT-based
APIs. It detects changed .properties
files via Git, queues them for translation, applies a glossary for consistent
terminology, and archives completed work.
-
Python 3.9+
This script should work in Python 3.7+, but 3.9 or higher is recommended. -
OpenAI API Key
You need an OpenAI API key to use GPT-based translations. Sign up at OpenAI to obtain a key. -
Git
If you want to leverage Git-based detection of changed.properties
files, ensure Git is installed and the target project is a Git repository.
-
src/translate_localization_files.py
Main script containing the logic for translating.properties
files. -
config.yaml
Stores configurable parameters such as paths, OpenAI model name, etc. -
glossary.json
Contains a language-specific glossary to ensure consistent translations. -
tests/
Holds test files (e.g., integration tests). -
venv/
(Optional) A virtual environment for isolating Python dependencies.
- Create a
.env
File
Place a.env
file in the root directory with your OpenAI API key:OPENAI_API_KEY=your_secret_api_key_here
- Set up
config.yaml
target_project_root
: Absolute path to the Git repository root.input_folder
: Path to the folder containing.properties
files to be translated.glossary_file_path
: Path toglossary.json
.model_name
: Name of the OpenAI model (e.g.,gpt-4
,gpt-3.5-turbo
).translation_queue_folder
&translated_queue_folder
: Folders to manage the translation workflow.dry_run
: Iftrue
, the script will simulate file operations without actually copying or moving files.
Example config.yaml
:
target_project_root: "/path/to/your/git/repo"
input_folder: "/path/to/properties/files"
glossary_file_path: "glossary.json"
model_name: "gpt-4"
translation_queue_folder: "translation_queue"
translated_queue_folder: "translated_queue"
dry_run: false
-
Clone This Repository (if you haven't already):
git clone https://github.com/YourUsername/YourRepo.git cd YourRepo
-
Create & Activate a Virtual Environment (recommended):
# For Linux/Mac python3 -m venv venv source venv/bin/activate # For Windows python -m venv venv venv\Scripts\activate
-
Install Dependencies:
pip install -r requirements.txt
-
Set Up
.env
File
Make sure the.env
file at the repository root contains:OPENAI_API_KEY=your_secret_api_key_here
From within the src
folder, run:
python translate_localization_files.py
-
Validates Paths
Ensures paths inconfig.yaml
exist and are accessible. -
Checks Git for Changes
Usesgit status
to detect recently modified.properties
files in theinput_folder
. -
Copies Changed Files to a Queue
Copies changed files totranslation_queue_folder
. -
Translates Files
The script scans each file for lines needing translation (based on a comparison to the source). It then calls the OpenAI API, respecting placeholders, glossaries, and existing translations. -
Copies Translated Files Back
Finished translations are moved totranslated_queue_folder
and then synced back toinput_folder
. -
Archiving
Archives the original files from the queue folder to anarchive
subfolder for record-keeping. -
Cleanup
Ifdry_run
is set tofalse
, the queue folders are cleared after successful processing.
If dry_run
is true
in config.yaml
, the script will not move or copy any files. Instead, it will log all
intended operations, allowing you to safely test.
glossary.json
ensures certain terms are translated consistently or left untranslated.- Key: Language code (e.g.,
"de"
,"cs"
). - Value: Another dictionary of
{ "Term": "Translation" }
.
- Key: Language code (e.g.,
Example snippet:
{
"de": {
"Bitcoin": "Bitcoin",
"Bisq": "Bisq"
},
"cs": {
"Bitcoin": "Bitcoin"
}
}
- Install
pytest
(already inrequirements.txt
):pip install pytest
- Run Tests:
Tests reside in the
pytest -v
tests
folder. Logs may appear intests/translation_log.log
.
-
Token Limits
The script attempts to keep prompts within token limits defined intranslate_localization_files.py
(MAX_MODEL_TOKENS
). Adjust as necessary for your model. -
Error Handling & Retries
The script includes retry logic with exponential backoff if the OpenAI API returns errors or rate-limit responses. -
Logging
Progress, errors, and debug messages go totranslation_log.log
(in both thesrc
andtests
folders, depending on the run context).
If you encounter any issues or need additional help, feel free to open an issue or contact the maintainers!