Model Translation
Configuration
in settings.py file
INSTALLED_APPS = [
...,
'rest_framework',
'translation'
]
MAIN_LANGUAGE = 'ar' #you could add any language could you want
in urls
urlpatterns = [
....
path('', include('translation.urls', namespace='translation'))
]
usage
In model:
- Use
from translation.translation import TranslatableModelinstead ofdjango.db.models.Model - Use
django.db.models.JSONFieldfor every translatable field - define "translatable" property which defines the translatable fields in the model
- translatable is a dictionary that references the translatable fields in any table
- translatable key is field name
- translatable value is a dictionary that can take two items form field and form field widget
- the default widget is
forms.TextInput
- you can access the current locale of certain field by using
model.translated_fieldfor example ifmodelhas fieldnameto get translated value to current locale usemodel.translated_name
Example
from django import forms
from django.db import models
from translation.models import TranslatableModel
class Foo(TranslatableModel):
...
# define translatable
translatable = {
'name': {"field": forms.CharField},
'bio': {"field": forms.CharField, "widget": forms.Textarea},
}
...
# define your fields here
name = models.JSONField(blank=True, null=True)
bio = models.JSONField(blank=True, null=True)
...
In Forms:
Use from translation.translation import TranslatableModelForm instead of django.forms.ModelForm
TranslationURl
To use translation form for any model just redirect to this url
please note "next" query parameter is mandatory parameter
from django.urls import reverse
next_url = '/any/url/you/want/to/redirect/to/after/translation'
reverse("{yourpath}:translate", kwargs={
"app_label": "app_label",
"model": "model",
"pk": "pk"
}) + '?next={}'.format(next_url)
In Serializer
Use utils.serializers.TranslatableModelSerializer instead of rest_framework.serializers.ModelSerializer
from translation.serializers import TranslatableModelSerializer
class FooSerializer(TranslatableModelSerializer):
class Meta:
fields = '__all__'
model = TranslatableTestModel
...
data = {
"title": {
"en": "en_title",
"ar": "ar_title"
},
"description": {
"en": "en_title",
"ar": "ar_title"
}
}
serializer = FooSerializer(data=data)
serializer.is_valid()
serializer.save()
.to_representation(self, instance)
-
it returns value by current locale
serializer.data["title"]=>"en_title"if current locale isen -
when current does not exist it returns value by fallback locale
.to_internal_value(self, data)
- it accepts single value and set it by current locale
data = {"title": "foo title"}=>{"title": {"en": "foo title"}}ifenis current locale
- it accepts json value and set it as it is
data = {"title": {"en": "en foo title", "ar": "ar foo title"}}=>{"title": {"en": "en foo title", "ar": "ar foo title"}}- it raises validation error when add language code that doe not exists in supported languages
In templates
get_field_original_translation
to get original field translation "main language" use filter for example
EX:
name|get_field_original_translation
Metadata
Release files for django-translation 1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django-translation-1.2.tar.gz | 16.4 kB | Details |
Release files / django-translation-1.2.tar.gz
| Download URL | django-translation-1.2.tar.gz |
|---|---|
| Size | 16.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8e8409896a55f0813d53240b94d31efb7305205475c651cd1ac88511d186c599
|
|
BLAKE2b-256 checksum How to use checksums |
fff08f11c458090ab36c2c710950c381c2d28830a3c395b7768bbcc1a4b847eb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.11.3 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.63.1 CPython/3.8.10
|