A Django application that facilitates the translation process of your Django projects
—
Core web interface providing browser-based translation editing, file management, pagination, and download functionality for .po/.mo files. The interface integrates with Django's admin system and provides a user-friendly translation experience.
Foundation classes providing common functionality for authentication, filtering, and .po file handling.
class RosettaBaseMixin:
"""
Base mixin providing authentication and po_filter property.
Requires user to pass can_translate() check and provides never_cache decorator.
"""
@property
def po_filter(self) -> dict:
"""Filter parameters for .po file selection."""
class RosettaFileLevelMixin(RosettaBaseMixin):
"""
Mixin for operations on single .po files.
Extends RosettaBaseMixin with file-specific functionality.
"""View for displaying available translation files organized by language and application, with filtering and navigation capabilities.
class TranslationFileListView(RosettaBaseMixin, TemplateView):
"""
Lists available translation files for editing.
Displays .po files grouped by language and application, with counts
of translated, untranslated, and fuzzy entries. Provides navigation
to translation editing interface.
"""
template_name: str = "rosetta/languages.html"Main interface for editing translation entries with pagination, search, filtering, and inline editing capabilities.
class TranslationFormView(RosettaFileLevelMixin, TemplateView):
"""
Main translation editing interface.
Provides paginated view of translation entries with:
- Inline editing of msgstr values
- Fuzzy flag management
- Search and filtering
- Progress tracking
- Batch operations
"""
template_name: str = "rosetta/pofile.html"Handles downloading of .po and .mo files for offline editing or distribution.
class TranslationFileDownload(RosettaFileLevelMixin, View):
"""
Handles .po/.mo file downloads.
Supports downloading:
- Original .po files
- Compiled .mo files
- Modified .po files with current translations
"""
def get(self, request, *args, **kwargs) -> HttpResponse:
"""
Download translation file.
Parameters:
- file_format: 'po' or 'mo'
Returns:
HttpResponse with file attachment
"""AJAX endpoint providing automatic translation suggestions from configured external services.
def translate_text(request) -> JsonResponse:
"""
AJAX endpoint for translation suggestions.
Accepts POST request with:
- text: Text to translate
- from_language: Source language code
- to_language: Target language code
Returns:
JsonResponse with translated text or error message
Requires:
- User authentication via can_translate()
- ROSETTA_ENABLE_TRANSLATION_SUGGESTIONS = True
- Configured translation service API keys
"""URL patterns defining the routing for all Rosetta views and endpoints.
urlpatterns: list = [
# Main interfaces
path('', TranslationFileListView.as_view(), name='rosetta-file-list'),
path('files/<path:po_filter>/', TranslationFormView.as_view(), name='rosetta-form'),
# File operations
path('download/<path:po_filter>/', TranslationFileDownload.as_view(), name='rosetta-download-file'),
# API endpoints
path('translate/', translate_text, name='rosetta.translate_text'),
# Legacy redirects
path('select/', RedirectView.as_view(), name='rosetta-old-home-redirect'),
path('files/', RedirectView.as_view(), name='rosetta-file-list-redirect'),
]Helper functions supporting the web interface functionality.
def get_app_name(path: str) -> str:
"""
Extract Django application name from .po file path.
Parameters:
- path: File system path to .po file
Returns:
Application name string
"""
def urlencode_safe(query: dict) -> str:
"""
URL-encode query parameters safely for template use.
Parameters:
- query: Dictionary of query parameters
Returns:
URL-encoded query string
"""
class LoginURL(Promise):
"""
Tests-friendly login URL that resolves at runtime.
Used by user_passes_test decorator to provide dynamic
login URL based on ROSETTA_LOGIN_URL setting.
"""
def __str__(self) -> str:
"""Returns configured login URL."""# In your Django URLs configuration
from django.urls import path, include
urlpatterns = [
path('rosetta/', include('rosetta.urls')),
]
# Access the interface at /rosetta/ in your browser
# - Lists all available translation files
# - Click on a file to start editing translations
# - Use pagination to navigate through entries
# - Download .po/.mo files as neededfrom rosetta.views import RosettaBaseMixin, TranslationFormView
class CustomTranslationView(RosettaBaseMixin, TemplateView):
"""Custom view extending Rosetta functionality."""
template_name = 'my_app/custom_translations.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# Add custom context
context['custom_data'] = self.get_custom_data()
return context
def get_custom_data(self):
"""Add your custom logic here."""
return {}// Frontend JavaScript for translation suggestions
function getTranslationSuggestion(text, fromLang, toLang) {
fetch('/rosetta/translate/', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'X-CSRFToken': getCookie('csrftoken')
},
body: new URLSearchParams({
'text': text,
'from_language': fromLang,
'to_language': toLang
})
})
.then(response => response.json())
.then(data => {
if (data.translation) {
// Use the translation suggestion
document.getElementById('msgstr-field').value = data.translation;
}
});
}Install with Tessl CLI
npx tessl i tessl/pypi-django-rosetta