this is an application for model translation
Project description
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
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
File details
Details for the file django-translation-1.2.tar.gz.
File metadata
- Download URL: django-translation-1.2.tar.gz
- Upload date:
- Size: 16.4 kB
- Tags: Source
- Uploaded using 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
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e8409896a55f0813d53240b94d31efb7305205475c651cd1ac88511d186c599
|
|
| MD5 |
49371586c8313a0953d1627bdcec7fff
|
|
| BLAKE2b-256 |
fff08f11c458090ab36c2c710950c381c2d28830a3c395b7768bbcc1a4b847eb
|