django-multilingual-model
Django Simple Multilingual Support for models
What is it?
An inefficient, minimal and utterly simple approach to model translation based on foreign key relations and attribute proxying.
The project code is forked from the original project by yazzgoth on Google code.
Usage
When a property of a translation is set for the language currently selected in this thread, it will automatically yield the right value of that property for the current language. So for a book which has a tranlation in en, requesting book.title from within a template from a client of which the detected language is English, will simply yield the English translation of the book title.
models.py:
from multilingual_model.models import MultilingualModel, MultilingualTranslation
class BookTranslation(MultilingualTranslation):
class Meta:
unique_together = ('parent', 'language_code')
parent = models.ForeignKey('Book', related_name='translations')
title = models.CharField(max_length=32)
description = models.TextField()
class Book(MultilingualModel):
ISBN = models.IntegerField()
>>> book = Book(ISBN="1234567890")
>>> book.save()
>>> book_en = BookTranslation(language_code='en')
>>> book_en.title = "Django for Dummies"
>>> book_en.description = "Django described in simple words."
>>> book_en.parent = book
>>> book_en.save()
>>> book_pl = BookTranslation(language_code='pl')
>>> book_pl.title = "Django dla Idiotow"
>>> book_pl.description = "Django opisane w prostych slowach"
>>> book_pl.parent = book
>>> book_pl.save()
>>> # now here comes the magic
>>> book.title_en
u'Django for Dummies'
>>> book.description_pl
u'Django opisane w prostych slowach'
Compatibility
Currently Django 1.4 through 1.6 is maintained for Python 2.6, 2.7 and 3.3.
Admin integration
admin.py:
from django.contrib import admin import models from multilingual_model.admin import TranslationStackedInline class BookTranslationInline(TranslationStackedInline): model = models.BookTranslation class BookAdmin(admin.ModelAdmin): list_display = ["ISBN"] inlines = [BookTranslationInline] admin.site.register(models.Book, BookAdmin)
__unicode__ representation using translated field
In order to make translation of the __unicode__ function work, some magic is required. A helper method for this is included by default, allowing you to do the following:
def __unicode__(self):
return self.unicode_wrapper('title', default='Unnamed')
Upgrade from previous versions
If you upgrade from previous versions you need to be aware of two important facts:
The Model MultilingualTranslation in multilingual_model.models has a field named “language_code”. In prior versions the max_length was set to 5. This has been changed to 7 to allow longer language codes to be valid. This could break validation in certain situations. A way to reflect this change on the level of the database is to manually change the database table of the models that are translated.
In admin.py the class TranslationInline has been renamed to TranslationStackedInline. TranslationInline will be deprecated soon. Additionally to TranslationStackedInline there now is a TranslationTabularInline.
Settings
- MULTILINGUAL_FAIL_SILENTLY
Return None instead of raising a ValueError when a translation for the current language cannot be found and MULTILINGUAL_FALL_BACK_TO_DEFAULT is False. Defaults to the inverse of DEBUG.
- MULTILINGUAL_FALL_BACK_TO_DEFAULT
Whether or not to fall back to MULTILINGUAL_LANGUAGE_CODE when no translation can be found for the currently activated language. Defaults to True.
- MULTILINGUAL_LANGUAGE_CODE
The default language used when MULTILINGUAL_FALL_BACK_TO_DEFAULT is True. Defaults to LANGUAGE_CODE.
- MULTILINGUAL_LANGUAGES
Set of languages available for translation. Defaults to LANGUAGES.
- MULTILINGUAL_HIDE_LANGUAGE
Hide functionality for selecting the language and removing translations in the admin. Defaults to True when MULTILINGUAL_LANGUAGES contains of a single language.
License
This application is released under the GNU Affero General Public License version 3.
Metadata
Release files for django-multilingual-model 0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| django-multilingual-model-0.6.tar.gz | 22.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_multilingual_model-0.6-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 34.7 kB
Release files / django-multilingual-model-0.6.tar.gz
| Download URL | django-multilingual-model-0.6.tar.gz |
|---|---|
| Size | 22.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b7b2057c4d9abcda77190fdb58b15d940e7838100a524b298681ea98b5c94bbb
|
|
BLAKE2b-256 checksum How to use checksums |
6c09ff01f514ea1aba703962eb05f38ce1bc92f1d7df1dfd008f81da7cef866f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / django_multilingual_model-0.6-py2.py3-none-any.whl
| Download URL | django_multilingual_model-0.6-py2.py3-none-any.whl |
|---|---|
| Size | 12.7 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
35c17bb70d885125b5f4a407d26d6b8cc2decaf0244493f5a695804f32f1097d
|
|
BLAKE2b-256 checksum How to use checksums |
24bc534f8f9387b471229954166ed4f2e1c31fbbd0f135ebaf9ef1126a77a714
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for django-multilingual-model 0.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.