Skip to main content

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 (mosty).

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"
    text = Translatable(
        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" and print(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 librarys be 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 langauge. 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

  1. Add django-garnett to your dependencies. eg. pip install django-garnett

  2. Convert your chosen field to a TranslatedCharField

    • For example: title = fields.TranslatedCharField(*args)
  3. 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".

  4. Add GARNETT_DEFAULT_TRANSLATABLE_LANGUAGE (a single language code) to your settings.

  5. Re-run django makemigrations & django migrate for any apps you've updated.

  6. Thats mostly it.

You can also add a few value adds:

  1. (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

  2. (Optional) Add the garnett app to your INSTALLED_APPS to use garnett's template_tags. If this is installed before django.contrib.admin it also include a languge switcher in the Django Admin Site.

  3. (Optional) Add a template processor:

    • Install garnett.context_processors.languages this will add garnett_languages (a list of available Languages) and garnett_current_language (the currently selected language).

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).
    • default: 'en-AU'
  • GARNETT_TRANSLATABLE_LANGUAGES:
    • Stores a list of language codes that users can use to save against TranslatableFields.
    • 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 the GARNETT_QUERY_PARAMATER_NAME for a language to display
      • garnett.selector.cookie: Checks for a cookie called GARNETT_LANGUAGE_CODE for a language to display. Note: you cannot change this cookie name.
      • garnett.selector.header: Checks for a HTTP Header called X-Garnett-Language-Code for a language to display. Note: you cannot change this Header name.
      • garnett.selector.browser: Uses Django's get_language 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']
  • GARNETT_QUERY_PARAMATER_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

need to run tests like this for now: PYTHONPATH=../ ./manage.py shell

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-garnett-0.0.4.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

django_garnett-0.0.4-py3-none-any.whl (14.4 kB view details)

Uploaded Python 3

File details

Details for the file django-garnett-0.0.4.tar.gz.

File metadata

  • Download URL: django-garnett-0.0.4.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for django-garnett-0.0.4.tar.gz
Algorithm Hash digest
SHA256 031eaab0bf191dd4c44678022ac6f79a6c6f3dbf9489d3e7cf510b55740235dc
MD5 7cc1d5c56781e15cb02a6692d5afcfeb
BLAKE2b-256 556df97e171f03bc96465c4c3081c38a27f6f4853e060d9ca915440d509d63c3

See more details on using hashes here.

Provenance

File details

Details for the file django_garnett-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: django_garnett-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.7

File hashes

Hashes for django_garnett-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ac851b2e18cd68b1d83751807eb19806feaba68e4f87a557942392076ce2bc92
MD5 32131d2a3b330503ca3373224129ec7f
BLAKE2b-256 f7bde42e45844cf2820fb5ca5983c5356f1168461d7da5513e7c67bb7554c8b5

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page