Simple translatable Django fields
Project description
django-garnett
Django Garnett is a field level translation library that allows you to store strings in multiple languages for fields in Django - with minimal changes to your models and without having to rewrite your code (mostly).
In summary it allows you to do this:
models.py | You can do this! |
By changing your models from this...
class Greeting(models.model):
text = CharField(max_length=150)
target = models.CharField()
def __str__(self):
return f"{self.greeting}, {self.target}"
to this... # Import garnett
from garnett.fields import Translated
class Greeting(models.model):
# Convert greeting to a translatable field
text = Translated(CharField(max_length=150))
target = models.CharField()
def __str__(self):
return f"{self.greeting} {self.target}"
|
greeting = Greeting(text="Hello", target="World")
with set_field_language("en"):
greeting.text = "Hello"
with set_field_language("fr"):
greeting.text = "Bonjour"
greeting.save()
greeting.refresh_from_db()
with set_field_language("en"):
print(greeting.text)
print(greeting)
# >>> "Hello"
# >>> "Hello World"
with set_field_language("fr"):
print(greeting.text)
print(greeting)
# >>> "Bonjour"
# >>> "Bonjour World!"
with set_field_language("en"):
print(greeting.text)
print(greeting)
# >>> "Hello"
# >>> "Hello World"
Greeting.objects.filter(title="Hello").exists()
# >>> True
Greeting.objects.filter(title="Bonjour").exists()
# >>> False
Greeting.objects.filter(title__fr="Bonjour").exists()
# >>> True!!
# Assuming that GARNETT_DEFAULT_TRANSLATABLE_LANGUAGE="en"
# Or a middleware has set the language context
print(greeting.text)
# >>> Hello
print(greeting)
# >>> Hello World!
|
Tested on:
- Django 3.1+
- Postgres, SQLite, MariaDB
- Python 3.7+
Pros:
- Fetching all translations for a models requires a single query
- Translations are stored in a single database field with the model
- Translations act like regular a field
Model.field_name = "some string"
andprint(Model.field_name)
work as you'd expect - Includes a configurable middleware that can set the current language context based on users cookies, query string or HTTP headers
- Works nicely with Django Rest Framework
Cons:
- You need to alter the models, so you can't make third-party libraries translatable.
Why write a new Django field translator?
A few reasons:
- Most existing django field translation libraries are static, and add separate database columns per translation.
- We needed a library that could be added in without requiring a rewrite of a large code base.
Note: Field language is different to the django display language. Django can be set up to translate your pages based on the users browser and serve them with a user interface in their preferred language.
Garnett does not use the browser language by design - a user with a French browser may want the user interface in French, but want to see content in English.
How to install
-
Add
django-garnett
to your dependencies. eg.pip install django-garnett
-
Convert your chosen field using the
Translated
function- For example:
title = fields.Translated(models.CharField(*args))
- For example:
-
Add
GARNETT_TRANSLATABLE_LANGUAGES
(a callable or list of language codes) to your django settings.Note: At the moment there is no way to allow "a user to enter any language".
-
Add
GARNETT_DEFAULT_TRANSLATABLE_LANGUAGE
(a callable or single language code) to your settings. -
Re-run
django makemigrations
&django migrate
for any apps you've updated. -
Thats mostly it.
You can also add a few value adds:
-
(Optional) Add a garnett middleware to take care of field language handling:
-
You want to capture the garnett language in a context variable available in views use:
garnett.middleware.TranslationContextMiddleware
-
You want to capture the garnett language in a context variable available in views, and want to raise a 404 if the user requests an invalid language use:
garnett.middleware.TranslationContextNotFoundMiddleware
-
(Future addition) You want to capture the garnett language in a context variable available in views, and want to redirect to the default language if the user requests an invalid language use:
garnett.middleware.TranslationContextRedirectDefaultMiddleware
-
If you want to cache the current language in session storage use
garnett.middleware.TranslationCacheMiddleware
after one of the above middleware (this is useful with the session selector mentioned below)
-
-
(Optional) Add the
garnett
app to yourINSTALLED_APPS
to use garnett's template_tags. If this is installed beforedjango.contrib.admin
it also include a language switcher in the Django Admin Site. -
(Optional) Add a template processor:
- Install
garnett.context_processors.languages
this will addgarnett_languages
(a list of availableLanguage
s) andgarnett_current_language
(the currently selected language).
- Install
Language
vs language
Django Garnett uses the python langcodes
to determine more information about the languages being used - including the full name and local name of the language being used. This is stored as a Language
object.
Django Settings options:
GARNETT_DEFAULT_TRANSLATABLE_LANGUAGE
- Stores the default language to be used for reading and writing fields if no language is set in a context manager or by a request.
- By default it is 'en-AU' the language code for 'Strayan, the native tongue of inhabitants of 'Straya (or more commonly known as Australia).
- Can also be callable that returns list of language codes
- default:
'en-AU'
GARNETT_TRANSLATABLE_LANGUAGES
:- Stores a list of language codes that users can use to save against TranslatableFields.
- Can also be callable that returns default language code
- default
[GARNETT_DEFAULT_TRANSLATABLE_LANGUAGE]
GARNETT_REQUEST_LANGUAGE_SELECTORS
:- A list of string modules that determines the order of options used to determine the language selected by the user. The first selector found is used for the language for the request, if none are found the DEFAULT_LANGUAGE is used. These can any of the following in any order:
garnett.selector.query
: Checks theGARNETT_QUERY_PARAMETER_NAME
for a language to displaygarnett.selector.cookie
: Checks for a cookie calledGARNETT_LANGUAGE_CODE
for a language to display. Note: you cannot change this cookie name.garnett.selector.session
: Checks for a session keyGARNETT_LANGUAGE_CODE
for a language to display. Note: you cannot change this key name.garnett.selector.header
: Checks for a HTTP Header calledX-Garnett-Language-Code
for a language to display. Note: you cannot change this Header name.garnett.selector.browser
: Uses Django'sget_language
function to get the users browser/UI language as determined by Django.
- For example, if you only want to check headers and cookies in that order, set this to
['garnett.selector.header', 'garnett.selector.cookie']
. - default:
['garnett.selector.query', 'garnett.selector.cookie', 'garnett.selector.header']
- A list of string modules that determines the order of options used to determine the language selected by the user. The first selector found is used for the language for the request, if none are found the DEFAULT_LANGUAGE is used. These can any of the following in any order:
GARNETT_QUERY_PARAMETER_NAME
:- The query parameter used to determine the language requested by a user during a HTTP request.
- default:
glang
Advanced Settings (you probably don't need to adjust these)
GARNETT_TRANSLATABLE_FIELDS_PROPERTY_NAME
:- Garnett adds a property to all models that returns a list of all TranslatableFields. By default, this is 'translatable_fields', but you can customise it here if you want.
- default:
translatable_fields
GARNETT_TRANSLATIONS_PROPERTY_NAME
:- Garnett adds a property to all models that returns a dictionary of all translations of all TranslatableFields. By default, this is 'translations', but you can customise it here if you want.
- default:
translations
Why call it Garnett?
- Libraries need a good name.
- Searching for "Famous Translators" will tell you about Constnace Garnett.
- Searching for "Garnett Django" shows there was no library with this name. It did however talk about Garnet Clark (also spelled Garnett), a jazz pianist who played with Django Reinhart - the namesake of the Django Web Framework.
- Voila - a nice name
Warnings
contains == icontains
For cross database compatibility reasons this library treatscontains
likeicontains
. I don't know why - https://www.youtube.com/watch?v=PgGNWRtceag- Due to how django sets admin form fields you will not get the admin specific widgets like
AdminTextAreaWidget
on translated fields in the django admin site by default. They can however be specified explicitly on the corresponding model form
need to run tests like this for now: PYTHONPATH=../ ./manage.py shell
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-garnett-0.1.1.tar.gz
.
File metadata
- Download URL: django-garnett-0.1.1.tar.gz
- Upload date:
- Size: 16.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.8.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d1e11de3487a65381f6f06ee24e46ce74bf84ba4766787e527f1c3d4641b63e1 |
|
MD5 | 33b939212be894335b689567128e872f |
|
BLAKE2b-256 | ac977418e3ddcf36c83b267f0fc781e8c775d944bcd1903e2a33e5c719353ebf |
Provenance
File details
Details for the file django_garnett-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: django_garnett-0.1.1-py3-none-any.whl
- Upload date:
- Size: 16.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/53.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.8.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 15e880d26dbc58850ea2b3637c8c716a11659591f95e4441f7f39e222dbfece9 |
|
MD5 | 1354c5e4db1e603e7abf2a118885c1ec |
|
BLAKE2b-256 | e2c5c8ca699e1f3a7722666b2faf7e034cedaddc4a564b004c7bed51ddcdc2a9 |