Skip to main content

An easy, efficient and modular way of translating django models.

Project description

build python pypi django flake8

Translations app provides an easy, efficient and modular way of translating Django models.

Requirements

  • Python (>=3.5)

  • Django (>=2.0)

Installation

  1. Install Translations using PIP (use --pre, still in development):

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

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

    $ python manage.py migrate
  4. Make sure django internationalization settings are set correctly:

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

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']

That’s it! 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.

https://raw.githubusercontent.com/perplexionist/django-translations/master/docs/_static/admin.png

QuerySet

Use the extended ORM capabilities:

>>> 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 does only ONE QUERY to translate the queryset and its relations.

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 does only ONE QUERY to read the translations of any object (instance, queryset, list) and its relations, or to create their translations.

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.0rc6.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file django-translations-1.0.0rc6.tar.gz.

File metadata

  • Download URL: django-translations-1.0.0rc6.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 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.0rc6.tar.gz
Algorithm Hash digest
SHA256 fc06bb21435c4321f0174b053e8543be293602863cc20addfa43b0e8cb568862
MD5 2b93cbf7d7d9deb099c3d4079e00c92f
BLAKE2b-256 01bec88dd7532ad8180d82854fc64530e60d63bd38188f64ecec254fb127104e

See more details on using hashes here.

Provenance

File details

Details for the file django_translations-1.0.0rc6-py3-none-any.whl.

File metadata

  • Download URL: django_translations-1.0.0rc6-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.0 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.0rc6-py3-none-any.whl
Algorithm Hash digest
SHA256 7006886c26119830a5cb9a5022d8403caaa6977b7b4e06590175c1113de179d6
MD5 d9edf090fa53ab5b5a64294c6d199a2d
BLAKE2b-256 d740dc3621504443cdc565ae4f478458a11f41ab01ded03461ad30c3d0bc7c2e

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