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

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

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.

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

Uploaded Source

Built Distribution

django_translations-1.0.0rc3-py3-none-any.whl (12.3 kB view hashes)

Uploaded Python 3

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