A Django application providing translation functionalities for Django Rest Framework
Project description
django_restful_translator
django_restful_translator
is a Django library for managing and serving translations of Django models while keeping the original values untouched. This package is designed to integrate seamlessly with Django REST framework.
Functionality
django_restful_translator
provides:
-
Model translations: The
TranslatableModel
mixin adds translation capabilities to your models, allowing you to specify which fields should be translatable, while leaving the original field values intact. -
Translation management: Admin inlines for managing translations from the Django Admin site.
-
Translation synchronization: Commands
generatelocales
andupdate_database_translations
export translations to.po
files and import them back into the database, keeping translations synchronized across different environments. -
REST API support: Provided serializers (
TranslatableDBSerializer
,TranslatableDBDictSerializer
,TranslatableGettextSerializer
,TranslatableGettextDictSerializer
) ensure translated fields are properly serialized in API responses.
Advantages
-
Preserves original values:
django_restful_translator
stores all translations in a separate model, ensuring that your original model's values remain unchanged. -
Ease of use: It integrates seamlessly with the Django Admin interface for easy management of translations.
-
Flexibility: The
TranslatableModel
mixin can be added to any Django model with specified fields to translate. -
Efficiency: By storing translations in the database and only translating fields when necessary,
django_restful_translator
minimizes unnecessary work. -
Integration with Django REST framework: If you're using Django REST framework,
django_restful_translator
is designed to work with it out of the box.
Installation
Use the package manager pip to install django_restful_translator.
pip install django-restful-translator
Configuration
In your settings file, add the middleware for handling language preferences:
MIDDLEWARE = [
...
'django.middleware.locale.LocaleMiddleware',
...
]
In your settings file, add django_restful_translator
to instaled apps:
INSTALLED_APPS = [
...
'django_restful_translator',
...
]
Set up your languages and locale paths. Make sure to include both the standard locale directory and the one for django_restful_translator:
LOCALE_PATHS = (
BASE_DIR / "locale",
BASE_DIR / "drt_locale",
)
LANGUAGES = (
("en", "English"),
("sv", "Swedish"),
)
LANGUAGE_CODE = "en"
Migrations
After setting up, you need to create the necessary database tables. You can do this by running migrations:
python manage.py migrate
Usage
- Import the
TranslatableModel
in your models.py and use it as a base class for any model you want to be translatable.
from django_restful_translator.models import TranslatableModel
class YourModel(TranslatableModel):
...
- Add your translatable fields to the
translatable_fields
attribute.
class YourModel(TranslatableModel):
title = models.CharField(max_length=100)
description = models.TextField()
translatable_fields = ['title', 'description']
- Use the provided serializers in your API.
from django_restful_translator.drf.serializers import TranslatableDBSerializer
class YourModelSerializer(TranslatableDBSerializer):
...
- To enable managing translations in the Django admin, add
TranslationInline
to your model admin.
from django_restful_translator.admin import TranslationInline
class YourModelAdmin(admin.ModelAdmin):
inlines = [TranslationInline,]
5.When querying your translatable models, you can optimize your database queries by using prefetch_related
to prefetch translations.
YourModel.objects.all().prefetch_related('translations')
- Run the provided management commands to generate
.po
files and update the database with translations.
python manage.py generatelocales
python manage.py update_database_translations
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
License
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
Built Distribution
Hashes for django_restful_translator-0.1.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9aa00b464be6e8d641e03e130e9f4cae6d97bf3c0d95bee8172cf5074d13896 |
|
MD5 | 94bd89adc4ac8f670a62e5a690cd6811 |
|
BLAKE2b-256 | 6f83ff0a5a87e0c4f3197f1116bdf535e0dbffb9ecdc4d0fc54f28c06937a530 |
Hashes for django_restful_translator-0.1.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c14796751da5de5908758fcb2ae5bf4bf05a3b8ef8a227a869b9e15f1d452f4f |
|
MD5 | 0e1279ded12d2243a65ac704c5d05b59 |
|
BLAKE2b-256 | ed7c845a621517a572b9901eb90a2fe303d5c399dd21b4b3cf15790882d29500 |