Django Rosetta
django-rosetta is a Django application designed to simplify the translation process for Django projects. It works by providing a web-based interface to edit gettext `.po` files, without requiring any database models or creating tables. The library is actively maintained, with recent updates in version 0.10.3 adding support for Django 6.0 and modernizing its JavaScript codebase by dropping jQuery. It also features integration with various AI translation services like DeepL and OpenAI for translation suggestions.
Common errors
-
django.core.exceptions.ImproperlyConfigured: 'rosetta' is not in your INSTALLED_APPS setting.
cause The 'rosetta' app has not been added to the INSTALLED_APPS list in your Django project's settings.py file.fixAdd `'rosetta'` to the `INSTALLED_APPS` list in `settings.py`: ```python INSTALLED_APPS = [ # ... other apps 'rosetta', ] ``` -
NoReverseMatch at /rosetta/ 'rosetta' is not a registered namespace
cause The `rosetta.urls` have not been included in your project's main `urls.py` file or are incorrectly included.fixAdd `path('rosetta/', include('rosetta.urls'))` to your project's `urlpatterns` in `urls.py`: ```python from django.urls import include, path urlpatterns = [ # ... other url patterns path('rosetta/', include('rosetta.urls')), ] ``` -
Translated strings do not appear on the website after editing in Rosetta.
cause After editing `.po` files in Rosetta, the corresponding `.mo` (machine object) files, which Django uses for translations, have not been recompiled or the web server hasn't been restarted. This can also happen if `LOCALE_PATHS` is incorrect or `makemessages` wasn't run for the relevant strings.fixRun `python manage.py compilemessages` from your project root. If `ROSETTA_WSGI_AUTO_RELOAD` is enabled and your web server supports it, changes might apply immediately, but a server restart is often needed. Ensure `LOCALE_PATHS` is correctly configured in `settings.py`. -
Uncaught ReferenceError: jQuery is not defined in rosetta interface.
cause Your custom JavaScript or an older version of your frontend assets in the Rosetta interface is still trying to use jQuery, but django-rosetta versions 0.10.2 and later have removed jQuery from its core.fixUpdate any custom JavaScript that runs within the Rosetta interface to remove its dependency on jQuery or ensure a compatible jQuery version is explicitly loaded if absolutely necessary.
Warnings
- breaking As of version 0.10.2, django-rosetta has rewritten its `rosetta.js` to drop jQuery, modernizing its frontend. If your custom frontend JavaScript heavily relied on jQuery in the Rosetta interface, it might break.
- breaking Django and Python version compatibility has been frequently updated. Version 0.10.0 and later require Django 4.2+ and Python 3.9+. Older versions (0.9.9) required Django 3.2+ and Python 3.8+. Ensure your environment meets the minimum requirements for your installed rosetta version.
- gotcha When running django-rosetta in a multi-process environment (e.g., Gunicorn, mod_wsgi) with Django's default `LocMemCache` as the cache backend, you may encounter issues with translation data not persisting between requests.
- gotcha Version 0.10.1 improved Content Security Policy (CSP) support by avoiding inline styles and server-side rendered stylesheets. If your project has a very strict CSP, you might need to adjust your CSP directives to allow Rosetta's assets, especially if you had custom overrides.
- gotcha Using translation suggestion APIs (DeepL, OpenAI, etc.) requires configuring API keys in your Django settings. These keys should be securely managed, ideally via environment variables, to avoid exposure.
Install
-
pip install django-rosetta
Imports
- 'rosetta'
# settings.py INSTALLED_APPS = [ # ... 'rosetta', ] - include('rosetta.urls')
from django.conf.urls import patterns, url, include urlpatterns = patterns('', url(r'^rosetta/', include('rosetta.urls')), )# urls.py from django.urls import include, path urlpatterns = [ # ... path('rosetta/', include('rosetta.urls')), ]
Quickstart
import os
# settings.py
INSTALLED_APPS = [
# ... default Django apps
'rosetta',
'django.contrib.admin', # Rosetta often accessed via admin
]
# Example for translation suggestions
ROSETTA_ENABLE_TRANSLATION_SUGGESTIONS = True
# Configure DeepL or OpenAI if desired
# DEEPL_AUTH_KEY = os.environ.get('DEEPL_AUTH_KEY', '')
# OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY', '')
# OPENAI_BASE_URL = os.environ.get('OPENAI_BASE_URL', '') # for self-hosted LLM
# urls.py (in your project's main urls.py)
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('rosetta/', include('rosetta.urls')),
]
# After configuration and running makemessages:
# python manage.py makemessages -l es
# python manage.py compilemessages
# Then, navigate to /rosetta/ in your browser.