A Django application that facilitates the translation process of your Django projects
—
Comprehensive settings system for customizing translation behavior, API integrations, storage backends, and user interface options. The configuration system provides centralized management of all Rosetta features with sensible defaults and extensive customization options.
Central configuration class managing all Rosetta settings with validation and default values.
class RosettaSettings:
"""
Central configuration management for all Rosetta settings.
Provides access to all configuration options with proper defaults,
validation, and runtime reloading capability.
"""
def __getattr__(self, name: str):
"""Get setting value with validation and defaults."""
def reload(self) -> None:
"""Reload settings from Django configuration."""
# Global settings instance
settings: RosettaSettings
"""
Global settings instance providing access to all Rosetta configuration.
Import as: from rosetta.conf import settings as rosetta_settings
"""
def reload_settings(*args, **kwargs) -> None:
"""
Signal handler for settings changes.
Connected to Django's setting_changed signal to automatically
reload Rosetta settings when Django settings are modified.
"""ROSETTA_MESSAGES_PER_PAGE: int = 10
"""Number of translation entries to display per page."""
ROSETTA_SHOW_AT_ADMIN_PANEL: bool = True
"""Whether to show Rosetta link in Django admin panel."""
ROSETTA_SHOW_OCCURRENCES: bool = True
"""Whether to display message occurrences in source code."""
ROSETTA_ENABLE_REFLANG: bool = True
"""Whether to enable reference language display."""ROSETTA_ENABLE_TRANSLATION_SUGGESTIONS: bool = False
"""Enable automatic translation suggestions from external APIs."""
# DeepL API Configuration
ROSETTA_DEEPL_AUTH_KEY: str = ''
"""DeepL API authentication key for translation suggestions."""
ROSETTA_DEEPL_LANGUAGES: list = ['EN', 'DE', 'FR', 'IT', 'JA', 'ES', 'NL', 'PL', 'PT', 'RU', 'ZH']
"""List of language codes supported by DeepL API."""
# Google Translate Configuration
ROSETTA_GOOGLE_APPLICATION_CREDENTIALS_PATH: str = ''
"""Path to Google Cloud service account credentials JSON file."""
ROSETTA_GOOGLE_PROJECT_ID: str = ''
"""Google Cloud project ID for translation API."""
# Azure Translator Configuration
ROSETTA_AZURE_CLIENT_SECRET: str = ''
"""Azure Translator API subscription key."""
# OpenAI Configuration
ROSETTA_OPENAI_API_KEY: str = ''
"""OpenAI API key for translation suggestions."""
ROSETTA_OPENAI_PROMPT_TEMPLATE: str = 'Translate the following text from {from_language} to {to_language}: {text}'
"""Template for OpenAI translation prompts."""
# Legacy Yandex (deprecated)
ROSETTA_YANDEX_TRANSLATE_KEY: str = ''
"""Yandex Translate API key (deprecated)."""ROSETTA_MAIN_LANGUAGE: str = settings.LANGUAGE_CODE
"""Main language code for the project."""
ROSETTA_MESSAGES_SOURCE_LANGUAGE_CODE: str = ROSETTA_MAIN_LANGUAGE
"""Source language code for translation messages."""
ROSETTA_MESSAGES_SOURCE_LANGUAGE_NAME: str = 'English'
"""Display name for the source language."""
ROSETTA_LANGUAGES: list = []
"""
List of languages available for translation.
If empty, uses all languages from Django's LANGUAGES setting.
"""
ROSETTA_LANGUAGE_GROUPS: dict = {}
"""
Language-specific user group permissions.
Format: {'language_code': ['group1', 'group2']}
"""ROSETTA_EXCLUDED_APPLICATIONS: list = []
"""List of Django applications to exclude from translation scanning."""
ROSETTA_EXCLUDED_PATHS: list = []
"""List of file paths to exclude from .po file discovery."""
ROSETTA_POFILENAMES: tuple = ('django.po', 'djangojs.po')
"""Tuple of allowed .po filename patterns."""
ROSETTA_POFILE_WRAP_WIDTH: int = 78
"""Line wrap width for .po file formatting."""
ROSETTA_AUTO_COMPILE: bool = True
"""Whether to automatically compile .mo files after .po file changes."""ROSETTA_STORAGE_CLASS: str = 'rosetta.storage.CacheRosettaStorage'
"""Full Python path to storage backend class."""
ROSETTA_CACHE_NAME: str = 'default'
"""Name of Django cache backend to use for storage."""ROSETTA_REQUIRES_AUTH: bool = True
"""Whether authentication is required for Rosetta access."""
ROSETTA_ACCESS_CONTROL_FUNCTION: str = 'rosetta.access.is_superuser_staff_or_in_translators_group'
"""Full Python path to access control function."""
ROSETTA_LOGIN_URL: str = '/admin/login/'
"""URL to redirect users for authentication."""ROSETTA_WSGI_AUTO_RELOAD: bool = False
"""Whether to auto-reload WSGI server when .po files change."""
ROSETTA_UWSGI_AUTO_RELOAD: bool = False
"""Whether to auto-reload uWSGI server when .po files change."""# settings.py - Basic Rosetta setup
INSTALLED_APPS = [
'rosetta',
]
# Increase entries per page
ROSETTA_MESSAGES_PER_PAGE = 25
# Show Rosetta in admin panel
ROSETTA_SHOW_AT_ADMIN_PANEL = True
# Enable automatic .mo compilation
ROSETTA_AUTO_COMPILE = True# settings.py - Enable translation suggestions with DeepL
ROSETTA_ENABLE_TRANSLATION_SUGGESTIONS = True
ROSETTA_DEEPL_AUTH_KEY = 'your-deepl-api-key'
# Or use Google Translate
ROSETTA_GOOGLE_APPLICATION_CREDENTIALS_PATH = '/path/to/credentials.json'
ROSETTA_GOOGLE_PROJECT_ID = 'your-google-project-id'
# Or use OpenAI
ROSETTA_OPENAI_API_KEY = 'your-openai-api-key'
ROSETTA_OPENAI_PROMPT_TEMPLATE = 'Please translate from {from_language} to {to_language}: {text}'
# Or use Azure Translator
ROSETTA_AZURE_CLIENT_SECRET = 'your-azure-subscription-key'# settings.py - Custom language setup
ROSETTA_LANGUAGES = [
('en', 'English'),
('fr', 'French'),
('de', 'German'),
('es', 'Spanish'),
]
# Language-specific permissions
ROSETTA_LANGUAGE_GROUPS = {
'fr': ['french_translators'],
'de': ['german_translators'],
'es': ['spanish_translators'],
}
# Exclude certain applications
ROSETTA_EXCLUDED_APPLICATIONS = ['debug_toolbar', 'django_extensions']
# Custom .po file names
ROSETTA_POFILENAMES = ('django.po', 'djangojs.po', 'custom.po')# settings.py - Use session storage instead of cache
ROSETTA_STORAGE_CLASS = 'rosetta.storage.SessionRosettaStorage'
# Or create custom storage backend
class CustomRosettaStorage(BaseRosettaStorage):
"""Custom storage implementation."""
def set(self, key, value):
# Custom storage logic
pass
def get(self, key, default=None):
# Custom retrieval logic
pass
ROSETTA_STORAGE_CLASS = 'myapp.storage.CustomRosettaStorage'from rosetta.conf import settings as rosetta_settings
def my_view(request):
"""Access Rosetta settings programmatically."""
# Check if translation suggestions are enabled
if rosetta_settings.ENABLE_TRANSLATION_SUGGESTIONS:
# Use translation service
pass
# Get configured messages per page
page_size = rosetta_settings.MESSAGES_PER_PAGE
# Access any setting
storage_class = rosetta_settings.STORAGE_CLASS
return render(request, 'template.html', {
'page_size': page_size,
'suggestions_enabled': rosetta_settings.ENABLE_TRANSLATION_SUGGESTIONS,
})from rosetta.conf import settings as rosetta_settings, reload_settings
from django.test.utils import override_settings
# Settings are automatically reloaded when Django settings change
with override_settings(ROSETTA_MESSAGES_PER_PAGE=50):
# Rosetta settings automatically reflect the change
assert rosetta_settings.MESSAGES_PER_PAGE == 50
# Manual reload (rarely needed)
reload_settings()# myapp/access.py
def custom_access_control(user):
"""Custom access control logic."""
if not user.is_authenticated:
return False
# Custom business logic
return user.profile.can_translate if hasattr(user, 'profile') else False
# settings.py
ROSETTA_ACCESS_CONTROL_FUNCTION = 'myapp.access.custom_access_control'
ROSETTA_LOGIN_URL = '/accounts/login/'Install with Tessl CLI
npx tessl i tessl/pypi-django-rosetta