Skip to main content

A Django app which provides support for model translation.

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 (1.11, >=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']

NO MIGRATIONS needed afterwards!

Query

Use the context:

>>> from translations.context import Context
>>> # 1. query the database like before
>>> continents = Continent.objects.all()
>>> # 2. work with the translated objects
>>> with Context(continents, 'countries', 'countries__cities',) as context:
...     # -------------------------------- read the context in German
...     context.read('de')
...     print(continents[0].name)
...     print(continents[0].countries.all()[0].name)
...     # -------------------------------- update the context in German
...     continents[0].name = 'Europa (changed)'
...     continents[0].countries.all()[0].name = 'Deutschland (changed)'
...     context.update('de')
...     # -------------------------------- and more capabilties
...     context.reset()
...     print(continents[0].name)
...     print(continents[0].countries.all()[0].name)
Europa
Deutschland
Europe
Germany

This does only ONE QUERY to translate any object (instance, queryset, list) plus all its relations.

Admin

Use the admin extensions:

from translations.admin import TranslatableAdmin, TranslationInline

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

This provides specialized translation inlines for the model.

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

Uploaded Source

Built Distribution

django_translations-1.0.0rc2-py3-none-any.whl (12.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: django-translations-1.0.0rc2.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.3

File hashes

Hashes for django-translations-1.0.0rc2.tar.gz
Algorithm Hash digest
SHA256 984afb03571e9f7d8b1e5f31f3204b225bc2f013707bd701e380c5348c66562c
MD5 219894d321ec91e91bca13e76c6e70df
BLAKE2b-256 734e22777394142c5541884163baebf3d15075ab4bae74a0fba2df8ab1a1d5f7

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: django_translations-1.0.0rc2-py3-none-any.whl
  • Upload date:
  • Size: 12.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.3

File hashes

Hashes for django_translations-1.0.0rc2-py3-none-any.whl
Algorithm Hash digest
SHA256 d03a666291ce9513c68d1156762fe5b7bc4533ebadd6757328d8ab1e8c882951
MD5 4d0380fa57374c32aaa70df4884be213
BLAKE2b-256 9957cb4560efa64e8aea6b034e11bf904163d2cf582fc8b66b0c8d557430ee96

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