Translate fields of django models without having to restart the server, without changing the model code.
Project description
django-tof
Django models translation on fly 🛸️
This project was initiated, promoted and accompanied by winePad GmbH. All development based on ideas, experience and financing by winePad GmbH (winePad.at).
The background and objectives of this projects are described here
An Application for dynamic translation of existing Django models into any number of languages.
- without need to change existing model classes
- without need to reboot servers
- without changing the use of translated fields
- ready to work after install and indicated in INSTALLED_APPS
- fully integrated in Django admin
Installation
pip install django-tof
python manage.py migrate tof
# settings.py
...
INSTALLED_APPS = [
...
'tof',
...
]
Don't forget to do if it necessary python manage.py collectstatic
How to use
- In the simplest use case django-tof allows you to store translation into the current language. You don't need special settings for this, just add this field into admin panel to the "Translatable fields" model. In this case if current language is 'en', then the value saved in the model will be displayed only if the current language is 'en'.
- If you need to support a certain number of languages and add them at the same time, you can use
TofAdmin
. Using theclass CustomModelAdmin(TofAdmin)
will cause the translated fields (added to the "Translatable fields") will be able to specify a specific language. At the same time, it is possible to leave some fields in the previous form by specify them inTofAdmin
with attributeonly_current_lang = ('description', )
.
- You can also use inline translation submission forms. To do this, specify admin class (always inherited from "TofAdmin")
inlines = (TranslationTabularInline, )
orinlines = (TranslationStackedInline, )
Programmatic use
Like a standard using, but it is possible to get a specific translation.
from django.utils.translation import activate
activate('en')
book = Book.objects.first()
book.title # => Title en
book.title.de # => Title de
Settings
The value for these variables can be specified in your settings.py
DEFAULT_LANGUAGE: default "en" - Default language is stub, used if not other translations is found.
FALLBACK_LANGUAGES: default {SITE_ID: ('en', 'de', 'ru'), 'fr': ('nl', ),}
- Determinate the order of search of languages for translation if the translation is in desired
no language. The key can be SITE_ID, None or language.
The processing order is this, if a translation into current/requested language is not found, then first we checked by the language key, if there is, looking translations for requested languages, if not - we take the SIDE_ID key. For example:
- if current language "fr", then searching order will be next: "fr" -> "nl" -> DEFAULT_LANGUAGE -> then if there is an original value that was before the declaration of this field like translatable.
- if current language "en", then searching order will be next "en" -> "de" -> "ru" -> DEFAULT_LANGUAGE -> then if there is an original value that was before the declaration of this field like translatable.
DEFAULT_FILTER_LANGUAGE: default "current" - Indicates in which translations search/filter values. May be in the next forms __all__
, current
, ['en', 'de']
, {'en', ('en', 'de', 'ru')}
current
- if this value is assigned, the filtering is occurs only on the translation into the current language. This is a default value.__all__
- if this value is assigned, the filtering is occurs for all translations.['en', 'de']
- if this value is assigned, the filtering is occurs according to translations of the specified languages.{'en', ('en', 'de', 'ru')}
- if this value is assigned, the filtering is occurs according to translations of languages received by the key of current language.
CHANGE_DEFAULT_MANAGER: default "True" - Changing the default manager of the model. If it True, then standard manager is transferred into class attribute "objects_origin", and "objects" becomes the standard manager inherited from standard with adding the functionality that recognized translated fields and takes into account settings from DEFAULT_FILTER_LANGUAGE.
Requirements
- Python (>=3.6)
- Django (>=2.2)
How to start development
- Fork this project
- Clone the repo
- Create new branch
- Change directory
example_project
- You can use pyenv to select the version of python
pyenv local 3.8.0
- We are using poetry
- Run:
poetry env use python
to use your python version. - Run:
poetry install
to install all requirements. - Run:
poetry shell
for activation virtual environment. - Run:
python manage.py runserver
to start the development server.
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
File details
Details for the file django-tof-0.3.2.tar.gz
.
File metadata
- Download URL: django-tof-0.3.2.tar.gz
- Upload date:
- Size: 23.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.0 CPython/3.8.0 Linux/5.0.0-1027-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d188a89168d5383f781a00ec9cfd4c372cfa479b507e993cd327e6e329b87c68 |
|
MD5 | 8a6935d3749d1bd74c72d5ecac31ddc2 |
|
BLAKE2b-256 | 09eb2283964f78035e1c6918305e9dd4cf49d3394c2ed5312c9e81a736d4f493 |
File details
Details for the file django_tof-0.3.2-py3-none-any.whl
.
File metadata
- Download URL: django_tof-0.3.2-py3-none-any.whl
- Upload date:
- Size: 28.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.0.0 CPython/3.8.0 Linux/5.0.0-1027-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8dcf51ae2ab12607f75865124c4563a857f8127ce956535528f0f098641c250b |
|
MD5 | f5c8e14784e6c97b3a0b4e130a0a4d9b |
|
BLAKE2b-256 | 748c994f014380e4d19e44f9f97ee0ce1d5ae666e7e2fbb5e355df6342b918b5 |