Skip to main content

Django model translation for perfectionists with deadlines.

Project description

Django Translations

build python pypi django flake8

Django model translation for perfectionists with deadlines.

Goal

There are two types of content, each of which has its own challenges for translation:

  • Static content: This is the content which is defined in the code. e.g. "Please enter a valid email address."

    Django already provides a solution for translating static content.

  • Dynamic content: This is the content which is stored in the database. (We can't know it beforehand!)

    Django Translations provides a solution for translating dynamic content.

Requirements

  • Python (>=3.5)
  • Django (>=2.0)

Installation

  1. Install Django Translations using pip:

    $ pip install django-translations
    
  2. Add translations to the INSTALLED_APPS in the settings of your project:

    INSTALLED_APPS += [
        'translations',
    ]
    
  3. Run migrate:

    $ python manage.py migrate
    
  4. Configure Django internationalization and localization settings:

    USE_I18N = True          # use internationalization
    USE_L10N = True          # use localization
    
    MIDDLEWARE += [          # locale middleware
        'django.middleware.locale.LocaleMiddleware',
    ]
    
    LANGUAGE_CODE = 'en-us'  # default (fallback) language
    LANGUAGES = (            # supported languages
        ('en', 'English'),
        ('en-gb', 'English (Great Britain)'),
        ('de', 'German'),
        ('tr', 'Turkish'),
    )
    

    Please note that these settings are for Django itself.

Basic Usage

Model

Inherit Translatable in any model you want translated:

from translations.models import Translatable

class Continent(Translatable):
    code = models.Charfield(...)
    name = models.Charfield(...)
    denonym = models.Charfield(...)

    class TranslatableMeta:
        fields = ['name', 'denonym']

No migrations needed afterwards.

Admin

Use the admin extensions:

from translations.admin import TranslatableAdmin, TranslationInline

class ContinentAdmin(TranslatableAdmin):
    inlines = [TranslationInline,]

This provides specialized translation inlines for the model.

image

QuerySet

Use the queryset extensions:

>>> from sample.models import Continent
>>> continents = Continent.objects.all(
... ).distinct(           # familiar distinct
... ).probe(['en', 'de']  # probe (filter, exclude, etc.) in English and German
... ).filter(             # familiar filtering
...     countries__cities__name__startswith='Köln'
... ).translate('de'      # translate the results in German
... ).translate_related(  # translate these relations as well
...     'countries', 'countries__cities',
... )
>>> print(continents)
<TranslatableQuerySet [
    <Continent: Europa>,
]>
>>> print(continents[0].countries.all())
<TranslatableQuerySet [
    <Country: Deutschland>,
]>
>>> print(continents[0].countries.all()[0].cities.all())
<TranslatableQuerySet [
    <City: Köln>,
]>

This provides a powerful yet familiar interface to work with the querysets.

Context

Use the translation context:

>>> from translations.context import Context
>>> from sample.models import Continent
>>> continents = Continent.objects.all()
>>> relations = ('countries', 'countries__cities',)
>>> with Context(continents, *relations) as context:
...     context.read('de')    # read the translations onto the context
...     print(':')            # use the objects like before
...     print(continents)
...     print(continents[0].countries.all())
...     print(continents[0].countries.all()[0].cities.all())
... 
...     continents[0].countries.all()[0].name = 'Change the name'
...     context.update('de')  # update the translations from the context
... 
...     context.delete('de')  # delete the translations of the context
... 
...     context.reset()       # reset the translations of the context
...     print(':')            # use the objects like before
...     print(continents)
...     print(continents[0].countries.all())
...     print(continents[0].countries.all()[0].cities.all())
:
<TranslatableQuerySet [
    <Continent: Europa>,
    <Continent: Asien>,
]>
<TranslatableQuerySet [
    <Country: Deutschland>,
]>
<TranslatableQuerySet [
    <City: Köln>,
]>
:
<TranslatableQuerySet [
    <Continent: Europe>,
    <Continent: Asia>,
]>
<TranslatableQuerySet [
    <Country: Germany>,
]>
<TranslatableQuerySet [
    <City: Cologne>,
]>

This can CRUD the translations of any objects (instance, queryset, list) and their relations.

Documentation

For more interesting capabilities browse through the documentation.

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-translations-1.0.1.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

django_translations-1.0.1-py3-none-any.whl (16.3 kB view details)

Uploaded Python 3

File details

Details for the file django-translations-1.0.1.tar.gz.

File metadata

  • Download URL: django-translations-1.0.1.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.3

File hashes

Hashes for django-translations-1.0.1.tar.gz
Algorithm Hash digest
SHA256 1ad9b319bd24ae64c5e4fa8f164e9fde67badd1ec712385a58b53a1bd4d1d60c
MD5 daacbb27a2781a9fc698d86637669fc3
BLAKE2b-256 2dc28e5cfa37e41c736beeb9ebb16020da5df9550c29691f640991e8fce11b9a

See more details on using hashes here.

Provenance

File details

Details for the file django_translations-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: django_translations-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 16.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.3

File hashes

Hashes for django_translations-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3273cd751b41dea4e348e9f8788b345e2e571f9a870e4996debb3d025f994e29
MD5 ab9dca2c11af1d1c790c86ffe845005d
BLAKE2b-256 406d1499c234d0b8ec0789df162de362c855de61834903060665313ff232dec8

See more details on using hashes here.

Provenance

Supported by

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