A Django app for simple model-field translations.
Project description
django-nublado-translation
A simple, reliable model-field translation system for Django projects.
Lightweight, focused, no bloat: translate model fields via a two-model contract (source model + translation model) without the tangles of GenericForeignKeys or automatic field injection. Source models stay fully intact.
Features
- Abstract base models for translation:
TranslationSourceModel— for models that can be translated.TranslationModel— for the translations of a source model.
- Translation managers:
TranslationSourceManager— helper methods for working with translations.
- Guarantees:
- A source object can have at most one translation per language.
- Unique fields on the source model are unique per language in the translation model.
Installation
pip install django-nublado-translation
Add to INSTALLED_APPS:
INSTALLED_APPS = [
...,
"django_nublado_translation",
]
Abstract models
TranslationSourceModel
An abstract base model for objects that can be translated.
from django.db import models
from django_nublado_translation.models import TranslationSourceModel
from django_nublado_translation.managers import TranslationSourceManager
class Article(TranslationSourceModel):
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique=True)
content = models.TextField()
objects = TranslationSourceManager()
Notes
- Using
TranslationSourceManageris optional but recommended. You can subclass it for app-specific translation requirements.
TranslationModel
An abstract base model for the translations of a TranslationSourceModel subclass.
- A foreign key to the source model is generated automatically.
- The foreign key name (default
"source") and the reverse relation name (default"translations") can be customized via thesource_nameandtranslations_nameattributes.
Subclasses must define:
- source_model: a subclass of TranslationSourceModel
- translation_fields: a list of fields from the source model to be translated.
from django_nublado_translation.models import TranslationModel
class ArticleTranslation(TranslationModel):
source_model = Article
translation_fields = [
"title",
"slug",
"content",
]
# Example usage
article = Article(
title="Hello everybody",
slug="hello-everybody",
content="Hello, everybody."
)
article.save()
article_translation_es = ArticleTranslation(
source=article,
language="es",
title="Hola a todos",
slug="hola-a-todos",
content="Hola a todos.",
)
article_translation_es.save()
Notes
- If
source_nameortranslations_nameis overridden, it must occur before migration for it to take effect.
Working with translations
From the source model:
article.get_translation("es")
article.get_current_translation() # Uses currently active Django language.
Using TranslationSourceManager
# Prefetch all translations.
Article.objects.prefetch_translations()
# prefetch_translations() returns a QuerySet.
Article.objects.prefetch_translations().filter(slug="source-article-slug")
# Filter translations.
translation_qs = ArticleTranslation.objects.filter(language="es")
Article.objects.prefetch_translations(queryset=translation_qs)
Notes
- The translation queryset can be freely customized.
- This is especially useful when subclassing managers with specific translation filters.
App settings
Access app settings via:
from django_nublado_translation.conf.app_settings import app_settings
# Example usage
print(app_settings.SOURCE_LANGUAGE)
Available settings and default values:
SOURCE_LANGUAGE: defaults todjango.config.settings.LANGUAGE_CODE.
Overriding settings
In your project's settings.py, define a dictionary named DJANGO_NUBLADO_TRANSLATION.
DJANGO_NUBLADO_TRANSLATION = {
"SOURCE_LANGUAGE": "en",
}
Testing
pytest
Notes:
- Runs all tests for the app.
- Requires
pytest-django.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_nublado_translation-0.1.0.tar.gz.
File metadata
- Download URL: django_nublado_translation-0.1.0.tar.gz
- Upload date:
- Size: 12.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4692aad87f5bd008bc2fc586a4dbc490295c4295450817bbb5c2af25f06dc495
|
|
| MD5 |
211fe0abad10389362ae439eebb48e41
|
|
| BLAKE2b-256 |
a2d9327b6c07c0aba4e138f916936c052521dc7cce1c2dc0a83a2f8783386ff0
|
File details
Details for the file django_nublado_translation-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_nublado_translation-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5455d66e9ac9e8fc302366c88cb67d4d4e4bdca463f1784d79ef6aca53c11a0
|
|
| MD5 |
8574722d0767f2056f3ec67e411380bf
|
|
| BLAKE2b-256 |
75ff63ce528ecdb5280f2cf06b06fb1e77e796b4792234b98509253419475df7
|