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

Only model instances are translated. Data accessed via the Django QuerySet ``values`` method will
not be translated.

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'

Help! The Admin is messing up all the vinaigrette fields whenever I save changes!
---------------------------------------------------------------------------------

Use `vinaigrette.VinaigrettteAdminLanguageMiddleware` to force the admin to
always use the main language, and not have vinaigrette mess with your
change views.

=============
Release Notes
=============

0.1.3
-----

* Support for Django 1.6.

0.2.0
-----

* New VinaigrettteAdminLanguageMiddleware middleware.
* Bug fix for the --all option, it now works again.

0.3.0
-----

* Support for python 3.3.

0.4.0
-----

* Support for Django 1.7

0.5.0
-----

* Can specify properties to use instead of database field names in .register() function.

1.0.0
-----

* Remove support for Django versions < 1.8
* Added support for Django 1.9
* Add the `--keep-vinaigrette-temp` option which keeps the temporary file containing the generated list of translations

1.0.1
-----

* Remembered to update version properly

1.1.0
-----

* Django 2.0 support


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-1.1.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

django_vinaigrette-1.1.0-py2.py3-none-any.whl (9.7 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

File hashes

Hashes for django-vinaigrette-1.1.0.tar.gz
Algorithm Hash digest
SHA256 75b6fe53237fa2b5d6fe71f43628da0faea99fd310dfe691bd6d2a5b62b15015
MD5 12887c4b25caf20048cc5d7be2c89301
BLAKE2b-256 8443051c74fe1d19c84333d1256b87d2636dd1ca933d1eeb2812e381d20f749d

See more details on using hashes here.

File details

Details for the file django_vinaigrette-1.1.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_vinaigrette-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fe7188319bf32791b6b95be9ae4a84032e257f01d571d212904a4ea2affb1ce7
MD5 4855299286230d7b175ecf166a1b6dd3
BLAKE2b-256 a3e7a95933afd5e3d32513bc5262fb9008a63140db7abf65c839a449790ae355

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