Skip to main content

Translate Django model data using gettext

Project description

Vinaigrette translates Django model data – stored in the database – using GNU gettext and Django’s standard internationalization features.

Installing

Add 'vinaigrette' to INSTALLED_APPS in your settings.

Then, tell vinaigrette which fields you want to translate. In the appropriate models.py files:

import vinaigrette
vinaigrette.register(Ingredient, ['name', 'description'])

This tells vinaigrette to translate the name and description fields on Ingredient objects.

Using

After installing vinaigrette, the PO files generated by manage.py makemessages will include strings from the registered fields. If a particular string is translated, the model value will be the string translated into the appropriate language:

>>> from django.utils.translation import activate
>>> i = Ingredient(name=u'Lettuce')
>>> i.name
u'Lettuce'
>>> activate('fr')
>>> i.name
u'Laitue'

Et cetera

There are a couple of options to restrict which objects translation strings will be collected from. See the docstring for vinaigrette.register.

Vinaigrette adds a --keep-obsolete option to manage.py makemessages, which prevents gettext from deactivating translated messages no longer present in code or in registered database fields.

Vinaigrette is designed for database content that is:

  • always edited in the default language

  • edited by site administrators, not users

In general, when a field is accessed, it will always return the translated version, if one exists. However, if a value is set, the exact value entered (and not the translated version) should be saved to the database. For example:

>>> from django.utils.translation import activate
>>> i = Ingredient(name=u'Lettuce')
>>> activate('fr')
>>> i.name
u'Laitue'
>>> i.name = 'Cabbage'
>>> i.name
u'Chou'
>>> i.save()
>>> Ingredient.objects.get(name='Cabbage').name
u'Chou'

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-vinaigrette-0.1.0.tar.gz (3.0 kB view details)

Uploaded Source

File details

Details for the file django-vinaigrette-0.1.0.tar.gz.

File metadata

File hashes

Hashes for django-vinaigrette-0.1.0.tar.gz
Algorithm Hash digest
SHA256 324fd2048bfe99da4c40d41a7a2e00e40d4ecd8aaa7155dc1ff2f07a4c2be514
MD5 9fc96051ec57f58a6dc4bc4888662281
BLAKE2b-256 b16d3630fa6d8e5692b96fea0981434dedc254de710abf53f07a7e0978c3e9e8

See more details on using hashes here.

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