Skip to main content

A Django app for adding field-level translations to your models.

Project description

bc-model-translator

This is a lightweight and extensible Django app for model field translations based on the current active language.

Supports dynamic field registration, localized querying, and transparent field access using Python magic.

✨ Features

  • Automatic creation of translated fields (name_en, name_ru, etc.)
  • Transparent access to localized values via model attributes
  • Language-aware filter(), get(), and exclude() on QuerySets
  • Simple registration system via decorators
  • Integration-ready: autodiscovery of translation.py files
  • Per-language field customization — configure attributes like verbose_name, max_length, default, etc., per field and language via BcTranslationFieldParams

🚀 Installation

pip install bc-django-modeltranslator

Add the app to INSTALLED_APPS:

⚙️ Configuration

1. Add the app to INSTALLED_APPS

INSTALLED_APPS = [
    ...
    "bc_modeltranslator",
]

2. Available settings:

# Temporarily disable translation registration if needed
MODELTRANSLATION_ENABLE_REGISTRATIONS = True

# Filename to look for in each app (default: "translation")
TRANS_FILENAME = "translation"

The TRANS_FILENAME setting defines the file name (without .py) that the app will look for in each Django app to discover translation registration.

🛠️ Usage

Create a translation.py file inside your Django app.

This file is automatically discovered on startup.

# your_app/translation.py

from bc_modeltranslater import register, BcTranslationOptions, BcTranslationField, BcTranslationFieldParams
from .models import TestModel

@register(TestModel)
class SymptomTranslationOptions(BcTranslationOptions):
    fields = [
        BcTranslationField(field_name="name"),
        BcTranslationField(
            field_name="desc",
            params={
                "fr": BcTranslationFieldParams(verbose_name="Description in French"),
                "kk": BcTranslationFieldParams(verbose_name="Kazakh description"),
            }
        )
    ]

🛠️ Available field parameters

BcTranslationFieldParams(
    verbose_name: str | None = None,
    default: str | None = None,
    blank: bool | None = None,
    help_text: str | None = None,
    max_length: int | None = None,
    null: bool | None = None,
    unique: bool | None = None
)

You can define different attributes per language using the params dictionary, where each key is a language code ("en", "ru", "fr", etc.).

🔍 Querying

All query methods automatically use the active language:

from django.utils.translation import activate

activate("ru")
TestModel.objects.filter(name="Имя")  # Filters by name_ru

🧠 Field Access

You can access localized values through normal attribute access:

obj = TestModel.objects.first()
print(obj.name)  # Returns name_ru or name_en depending on current language

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

bc_django_modeltranslator-0.0.3a0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

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

bc_django_modeltranslator-0.0.3a0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file bc_django_modeltranslator-0.0.3a0.tar.gz.

File metadata

File hashes

Hashes for bc_django_modeltranslator-0.0.3a0.tar.gz
Algorithm Hash digest
SHA256 efd80cf31d9a468ffd244a09d508d0618b2c077f520df5ebb55720c2db076e56
MD5 50a6f495eea0b2ad6dfd7b24db71cada
BLAKE2b-256 20e93d868d37470a3df4a90820c4a7164089f45662263b3df1bfa9da0f6ae906

See more details on using hashes here.

File details

Details for the file bc_django_modeltranslator-0.0.3a0-py3-none-any.whl.

File metadata

File hashes

Hashes for bc_django_modeltranslator-0.0.3a0-py3-none-any.whl
Algorithm Hash digest
SHA256 d80d25a5c57fe7d2fca99bb314cd5ce71e3657267538d9f79187a409d92e4716
MD5 448d7f45ff2d13b186d098bdd39171c0
BLAKE2b-256 94c835c9153903f9861edfce1338c54d5d7b4b992dc73d29d4d14e7ca2fd3f1c

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