Skip to main content

A Django app for simple model-field translations.

Project description

django-nublado-translation

A minimal, explicit model-field translation system for Django.

Translations are implemented via a strict two-model contract:

  • a source model
  • a translation model

No GenericForeignKeys.
No automatic field injection on the source model.
Source model remains unchanged.


Features

  • Abstract base models:
    • TranslationSourceModel:
    • TranslationModel:
  • Manager:
    • TranslationSourceManager: Manager for TranslationSourceManager
  • Guarantees:
    • One translation per (source, language).
    • Source-model unique fields are unique per language in the translation model.
  • Base-language fallback support (e.g., es-ares).

Installation

pip install django-nublado-translation
INSTALLED_APPS = [
    ...,
    "django_nublado_translation",
]

Abstract models

TranslationSourceModel

Base model for translatable objects.

  • Translations are stored and cached in a language-code-indexed dictionary translations_dict for convenience.
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()

    translated_fields = [
        "title",
        "slug",
        "content",
    ]

    objects = TranslationSourceManager()

Notes

  • translated_fields is required for translations, and is the single source of truth for translated fields.
  • TranslationSourceManager is optional, but recommended, to take advantage of its translation-filtering capability.

TranslationModel

Base model for translations of a TranslationSourceModel subclass.

  • A foreign key to the source model is generated automatically.
  • Default foreign key name: source.
  • Default reverse relation name: translations.
  • Fields are derived from translated_fields in related source model.
  • Constraint naming is derived from the translation model name.

Subclasses must define:

  • source_model
  • No translated field declarations are allowed on the translation model.
from django_nublado_translation.models import TranslationModel


class ArticleTranslation(TranslationModel):
    source_model = Article


# Source object
article = Article.objects.create(
    title="Hello everybody",
    slug="hello-everybody",
    content="Hello, everybody.",
)

# Translation object
ArticleTranslation.objects.create(
    source=article,
    language="es",
    title="Hola a todos",
    slug="hola-a-todos",
    content="Hola a todos.",
)

Notes

  • source_name may be overridden before migrations.
  • Translated fields are copied from TranslationSourceModel.translated_fields.

Working with translations

Using a subclass of TranslationSource

Getting translations:

# Assume the default language is "en", available translation languages are
# "es" and "de", and the object has a translation for "es".

# Fetching a translation.
article.get_translation("es")
article.get_translation("es-ar")   # Falls back to "es" if no translation found for "es-ar".
article.get_current_translation()  # Uses active Django language
  • Returns None if no translation exists.

Getting translation information:

# Find out if an object has a translation in a given language.
article.has_translation("es") # True
article.has_translation("de") # False

# Get translation-languages object doesn't have a translation for.
article.get_available_translation_languages() # ["de"]

Cache management for translation_dict.

article.clear_translations_dict_cache()
  • Invalidates the internal translation cache.
  • Required when translations are modified outside the instance lifecycle.

Using TranslationSourceManager

Prefetching translations

Article.objects.prefetch_translations()

Article.objects.prefetch_translations().filter(slug="source-article-slug")

# Prefetch filtered translations.
translation_qs = ArticleTranslation.objects.filter(language="es", published_status="published")

Article.objects.prefetch_translations(queryset=translation_qs)

App settings

from django_nublado_translation.conf.app_settings import app_settings

app_settings.SOURCE_LANGUAGE

Available settings

Setting Default
SOURCE_LANGUAGE django.conf.settings.LANGUAGE_CODE

Override

DJANGO_NUBLADO_TRANSLATION = {
    "SOURCE_LANGUAGE": "en",
}

Testing

pytest

Requires pytest-django.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_nublado_translation-0.2.2.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_nublado_translation-0.2.2-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file django_nublado_translation-0.2.2.tar.gz.

File metadata

File hashes

Hashes for django_nublado_translation-0.2.2.tar.gz
Algorithm Hash digest
SHA256 5ca5cf6ac9a97259fafd24a1d11fffd3d86d4395ab731a141bd55f3d4407547b
MD5 84305b5ce4e32ad7f5b84e247c1c0cd9
BLAKE2b-256 c21dc515c7cf03305f2913f4a52cfd3bfb4f7486a920d3f6f863bbcde76dedfc

See more details on using hashes here.

File details

Details for the file django_nublado_translation-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for django_nublado_translation-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2c80cacfd002c5ee89a9540680b2cd70d559d235f23ba24a2dab20cbb3d81f52
MD5 fe29bdde080acfa1fe11896269d8ab12
BLAKE2b-256 2b570461bac8ac4a5d93bdbed5e22bae8c6f8a59a6fd90b3275c3bd16fe4f0e5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page