A Django application that facilitates the translation process of your Django projects
npx @tessl/cli install tessl/pypi-django-rosetta@0.10.0A Django application that facilitates the translation process of your Django projects by providing a web-based interface for managing gettext catalogs. Django-Rosetta enables translators to edit .po files directly through a browser without requiring technical knowledge of gettext tools, offering translation suggestions, access control, and seamless integration with Django's internationalization framework.
pip install django-rosetta# Add to Django settings
INSTALLED_APPS = [
# ... other apps
'rosetta',
]
# URL configuration
from django.urls import include, path
urlpatterns = [
path('rosetta/', include('rosetta.urls')),
]Common programmatic usage:
from rosetta import get_version
from rosetta.access import can_translate
from rosetta.conf import settings as rosetta_settings
from rosetta.poutil import find_pos
from rosetta.signals import entry_changed, post_save
from rosetta.storage import get_storage
from rosetta.translate_utils import translate, TranslationException# In your Django project settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rosetta', # Add this
# ... your other apps
]
# URL configuration in urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('rosetta/', include('rosetta.urls')),
# ... your other URLs
]
# Optional: Configure Rosetta settings
ROSETTA_MESSAGES_PER_PAGE = 10
ROSETTA_ENABLE_TRANSLATION_SUGGESTIONS = True
ROSETTA_SHOW_AT_ADMIN_PANEL = TrueAfter setup, navigate to /rosetta/ in your Django application to access the translation interface. The application will automatically discover .po files in your project and present them for editing.
Django-Rosetta is built around Django's standard application architecture with specialized components:
The application maintains database independence by not creating any database tables, storing working data in configurable backends (session or cache by default).
Core web interface providing browser-based translation editing, file management, pagination, and download functionality for .po/.mo files.
class TranslationFileListView(RosettaBaseMixin, TemplateView):
"""Lists available translation files for editing."""
class TranslationFormView(RosettaFileLevelMixin, TemplateView):
"""Main translation editing interface with pagination."""
class TranslationFileDownload(RosettaFileLevelMixin, View):
"""Handles .po/.mo file downloads."""
def translate_text(request):
"""AJAX endpoint for translation suggestions."""Configurable permission system controlling who can access translation features, with support for user groups, language-specific permissions, and custom access control functions.
def can_translate(user) -> bool:
"""Check if user can access Rosetta translation interface."""
def can_translate_language(user, langid: str) -> bool:
"""Check if user can translate specific language."""
def get_access_control_function():
"""Get configured access control predicate function."""Comprehensive settings system for customizing translation behavior, API integrations, storage backends, and user interface options.
class RosettaSettings:
"""Central configuration management for all Rosetta settings."""
# Global settings instance
settings: RosettaSettings
def reload_settings(*args, **kwargs):
"""Signal handler for settings changes."""Integration with external translation APIs providing automatic translation suggestions to assist translators.
def translate(text: str, from_language: str, to_language: str) -> str:
"""Main translation function supporting multiple services."""
class TranslationException(Exception):
"""Exception raised by translation services."""
def translate_by_deepl(text: str, to_language: str, auth_key: str) -> str:
def translate_by_google(text: str, input_language: str, output_language: str, credentials_path: str, project_id: str) -> str:
def translate_by_azure(text: str, from_language: str, to_language: str, subscription_key: str) -> str:
def translate_by_openai(text: str, from_language: str, to_language: str, api_key: str) -> str:Pluggable storage system for persisting translation work-in-progress data, supporting session-based, cache-based, or custom storage implementations.
class BaseRosettaStorage:
"""Abstract base class for storage backends."""
class SessionRosettaStorage(BaseRosettaStorage):
"""Session-based storage backend."""
class CacheRosettaStorage(BaseRosettaStorage):
"""Cache-based storage backend (default)."""
def get_storage(request) -> BaseRosettaStorage:
"""Get configured storage instance for request."""Utilities for discovering, processing, and managing .po/.mo translation files across Django projects.
def find_pos(lang: str, project_apps: bool = True, django_apps: bool = False, third_party_apps: bool = False) -> list:
"""Find .po files for specified language across application types."""
def timestamp_with_timezone(dt=None) -> str:
"""Generate timestamp with timezone information."""
def pagination_range(first: int, last: int, current: int) -> list:
"""Generate pagination ranges for UI."""Django template tags and filters for integrating Rosetta functionality into custom templates and extending the web interface.
# Template filters
def can_translate(user) -> bool
def format_message(message: str) -> str
def lines_count(message: str) -> int
def is_fuzzy(entry) -> bool
# Template tags
class IncrNode(template.Node):
"""Counter increment tag implementation."""Event system for extending Rosetta functionality and integrating with custom workflows through Django's signal framework.
entry_changed = django.dispatch.Signal()
"""
Fired when translation entry is modified.
Provides: user, old_msgstr, old_fuzzy, pofile, language_code
"""
post_save = django.dispatch.Signal()
"""
Fired after .po file is saved.
Provides: language_code, request
"""# Version information
VERSION: tuple[int, int, int] # (0, 10, 2)
def get_version(limit: int = 3) -> str:
"""Return the version as a human-format string."""
# Django app configuration
class RosettaAppConfig(AppConfig):
"""Django application configuration with admin panel integration."""
name: str = "rosetta"
def ready(self) -> None: ...