An easy, efficient and modular way of translating django models.
Project description
Translations app provides an easy, efficient and modular way of translating Django models.
Requirements
Python (>=3.5)
Django (1.11, >=2.0)
Installation
Install Translations using PIP (use --pre, still in development):
$ pip install --pre django-translations
Add 'translations' to INSTALLED_APPS in the settings of your Django project:
INSTALLED_APPS += [ 'translations', ]
Run migrate:
$ python manage.py migrate
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.
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-translations-1.0.0rc3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9269f8eb6f48a6be107642ff3848baa187fa40f418357d043103ae92cef5363 |
|
MD5 | adcf85bae25a3d0ebdb092c12eaafae7 |
|
BLAKE2b-256 | 7e5c03e5cbdba3b0e83628f0d4eaeae739e2af793030d33f18977fabb96391af |
Hashes for django_translations-1.0.0rc3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e5cbb229b28ca91591c29965f0ff28bcdcc18ddad067fbe1530c7ed8fe46ee8c |
|
MD5 | 9e28ced80ad553402611c03a379cf43c |
|
BLAKE2b-256 | 3cae8c4e20a3c4d2525b0c156b18cfbdb281e9438aadf4bdcda52d865daa8943 |