This project uses Flask-Babel for internationalization (i18n) of backend API messages.
-
Install Flask-Babel (already in requirements.txt):
pip install Flask-Babel
-
Translation files are located in:
app/translations/
-
Mark strings for translation in your code:
from flask_babel import _ return jsonify({"error": _("Error message")})
-
Extract translatable strings:
pybabel extract -F babel.cfg -k _ -o messages.pot app/
-
Update translation files:
pybabel update -i messages.pot -d app/translations
-
Edit translation files:
- Edit
app/translations/en/LC_MESSAGES/messages.pofor English - Edit
app/translations/de/LC_MESSAGES/messages.pofor German - Fill in the
msgstr ""fields with translations
- Edit
-
Compile translations:
pybabel compile -d app/translations
Use Python string formatting with named parameters:
_("Error occurred: %(error)s", error=str(e))In the .po file:
msgid "Error occurred: %(error)s"
msgstr "Fehler aufgetreten: %(error)s"The backend automatically selects the language based on the Accept-Language HTTP header sent by the frontend. Supported languages:
en- English (default)de- German
Use the provided script to set up translations. Run from the backend/ directory:
./scripts/setup_translations.shOr from the project root:
./backend/scripts/setup_translations.sh- Add new translatable strings with
_()in code - Run
pybabel extractto updatemessages.pot - Run
pybabel updateto update language files - Edit
.pofiles with translations - Run
pybabel compileto create.mofiles - Restart the Flask application