Package: django-garnett
django-garnettDjango 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.Want a demo?https://django-garnett.herokuapp.com/Made with♥byAristotle MetadataIn summary it allows you to do this:models.pyYou can do this!By changing your models from this...classGreeting(models.model):text=CharField(max_length=150)target=models.CharField()def__str__(self):returnf"{self.greeting},{self.target}"to this...# Import garnettfromgarnett.fieldsimportTranslatedclassGreeting(models.model):# Convert greeting to a translatable fieldtext=Translated(CharField(max_length=150))target=models.CharField()def__str__(self):returnf"{self.greeting}{self.target}"fromgarnett.contextimportset_field_languagegreeting=Greeting(text="Hello",target="World")withset_field_language("en"):greeting.text="Hello"withset_field_language("fr"):greeting.text="Bonjour"greeting.save()greeting.refresh_from_db()withset_field_language("en"):print(greeting.text)print(greeting)# >>> "Hello"# >>> "Hello World"withset_field_language("fr"):print(greeting.text)print(greeting)# >>> "Bonjour"# >>> "Bonjour World!"withset_field_language("en"):print(greeting.text)print(greeting)# >>> "Hello"# >>> "Hello World"Greeting.objects.filter(title="Hello").exists()# >>> TrueGreeting.objects.filter(title="Bonjour").exists()# >>> FalseGreeting.objects.filter(title__fr="Bonjour").exists()# >>> True!!# Assuming that GARNETT_DEFAULT_TRANSLATABLE_LANGUAGE="en"# Or a middleware has set the language contextprint(greeting.text)# >>> Helloprint(greeting)# >>> Hello World!Tested on:Django 3.1+Postgres, SQLite, MariaDBPython 3.7+Tested with the following libraries:Django Rest Frameworkdjango-reversion&django-reversion-comparedjango-filtersPros:Battletested in production -Aristotle Metadatabuilt, support and uses this library for 2 separate products, served to government and enterprise clients!Fetching all translations for a model requires a single queryTranslations are stored in a single database field with the modelAll translations act like a regular field, soModel.field_name = "some string"andprint(Model.field_name)work as you would expectIncludes a configurable middleware that can set the current language context based on users cookies, query string or HTTP headersWorks nicely with Django Rest Framework - translatable fields can be set as strings or as json dictionariesWorks nicely with DjangoF()andQ()objects within the ORM - and when it doesn't we have a language awareLangF()replacement you can use.Cons:You need to alter the models, so you can't make third-party libraries translatable.It doesn't work onqueryset.values_listorqueryset.valuesnatively - but we have an easy Queryset Mixin to add language support for both below.Why write a new Django field translator?A few reasons:Most existing django field translation libraries are static, and add separate database columns or extra tables per translation.Other libraries may not be compatible with common django libraries, like django-rest-framework.We had a huge codebase that we wanted to upgrade to be multilingual - so we needed a library that could be added in without requiring a rewriting every access to fields on models, and only required minor tweaks.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.Garnettdoes notuse 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 or French based on their needs.How to installAdddjango-garnettto your dependencies. eg.pip install django-garnettConvert your chosen field using theTranslatedfunctionFor example:title = fields.Translated(models.CharField(*args))AddGARNETT_TRANSLATABLE_LANGUAGES(a callable or list oflanguage codes) to your django settings.Note: At the moment there is no way to allow "a user to enter any language".AddGARNETT_DEFAULT_TRANSLATABLE_LANGUAGE(a callable or singlelanguage code) to your settings.Re-rundjango makemigrations, perform a data migration to make your existing data translatable (See 'Data migrations') &django migratefor any apps you've updated.Thats mostly it.You can also add a few optional 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.TranslationContextMiddlewareYou 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.TranslationContextRedirectDefaultMiddlewareIf you want to cache the current language in session storage usegarnett.middleware.TranslationCacheMiddlewareafter one of the above middleware (this is useful with the session selector mentioned below)(Optional) Add thegarnettapp to yourINSTALLED_APPSto use garnett's template_tags. If this is installed beforedjango.contrib.adminit also include a language switcher in the Django Admin Site.(Optional) Add a template processor:Installgarnett.context_processors.languagesthis will addgarnett_languages(a list of availableLanguages) andgarnett_current_language(the currently selected language).(Optional) Add a custom translation fallback:By default, if a language isn't available for a field, Garnett will show a mesage like:No translation of this field available in EnglishYou can override this either by creating a custom fallback method:Translated(CharField(max_length=150), fallback=my_fallback_method))Wheremy_fallback_methodtakes a dictionary of language codes and corresponding strings, and returns the necessary text.Additionally, you can customise how django outputs text in templates by creating a newTranslationStrclass, and overriding the__html__method.Data migrationsIf you have lots of existing data (and if you are using this library you probably do) you will need to perform data migrations to make sure all of your existing data is multi-lingual aware. Fortunately, we've added some well tested migration utilities that can take care of this for you.Once you have rundjango-admin makemigrationsyou just need to add thestep_1_safe_encode_contentbefore andstep_2_safe_prepare_translationsafter your schema migrations, like in the following example:# Generated by Django 3.1.13 on 2022-01-11 10:13fromdjango.dbimportmigrations,modelsimportgarnett.fieldsimportlibrary_app.models#### Add this line infromgarnett.migrateimportstep_1_safe_encode_content,step_2_safe_prepare_translations#### Define the models and fields you want ot migratemodel_fields={"book":["title","description"],}classMigration(migrations.Migration):dependencies=[("library_app","0001_initial"),]operations=[## Add this operation at the startstep_1_safe_encode_content("library_app",model_fields),## These are the automatically generated migrationsmigrations.AlterField(# ... migrate title to TranslatedField),migrations.AlterField(# ... migrate description to TranslatedField),## Add this operation at the startstep_2_safe_prepare_translations("library_app",model_fields),]Languagevs languageDjango Garnett uses the pythonlangcodeslibrary 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 aLanguageobject.Django Settings options:GARNETT_DEFAULT_TRANSLATABLE_LANGUAGEStores 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' thelanguage codefor 'Strayan, the native tongue of inhabitants of 'Straya (or more commonly known as Australia).This can also be a callable that returns list of language codes. Combined with storing user settings in something like (django-solo)[https://github.com/lazybird/django-solo] users can dynamically add or change their language settings.default:'en-AU'GARNETT_TRANSLATABLE_LANGUAGES:Stores a list oflanguage codesthat users can use to save against TranslatableFields.This can also be a callable that returns list of language codes. Combined with storing user settings in something like (django-solo)[https://github.com/lazybird/django-solo] users can dynamically add or change their language settings.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_NAMEfor a language to displaygarnett.selector.cookie: Checks for a cookie calledGARNETT_LANGUAGE_CODEfor a language to display.
Note: you cannot change this cookie name.garnett.selector.session: Checks for a session keyGARNETT_LANGUAGE_CODEfor a language to display.
Note: you cannot change this key name.garnett.selector.header: Checks for a HTTP Header calledX-Garnett-Language-Codefor a language to display.
Note: you cannot change this Header name.garnett.selector.browser: Uses Django'sget_languagefunction to get the users browser/UI languageas determined by Django.For example, if you only want to check headers and cookies in that order, set this to['garnett.selectors.header', 'garnett.selectors.cookie'].default:['garnett.selectors.header', 'garnett.selectors.query', 'garnett.selectors.cookie']GARNETT_QUERY_PARAMETER_NAME:The query parameter used to determine the language requested by a user during a HTTP request.default:glangGARNETT_ALLOW_BLANK_FALLBACK_OVERRIDEIf set to true, when allpying the current language middleware, this will check for an extra get URL parameter to override the fallback to return blank strings where a languge has no content. This is useful for APIsdefault: FalseAdvanced 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_fieldsGARNETT_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:translationsUsing GarnettIf you did everything above correctly, garnett should for the most part "just work".Switching the active languageGarnett comes with a handy context manager that can be used to specify the current language. In any place where you want to manually control the current language, wrap your code inset_field_languageand garnett will correctly store the language. This can be nested, or you can change the language for a context multiple times before saving.fromgarnett.contextimportset_field_languagegreeting=Greeting(text="Hello",target="World")withset_field_language("en"):greeting.text="Hello"withset_field_language("fr"):greeting.text="Bonjour"greeting.save()Using Garnett withvalues_listorvaluesThis is one of the areas that garnettdoesn'twork immediately, but there is a solution.In the places you are using values lists orvalues, wrap any translated field in an L-expression and the values list will return correctly. For example:fromgarnett.expressionsimportLBook.objects.values_list(L("title"))Book.objects.values(L("title"))Using Garnett with Django-Rest-FrameworkAsTranslationFields are based on JSONField, by default Django-Rest-Framework renders these as a JSONField, which may not be ideal.You can get around this by using theTranslatableSerializerMixinas the first mixin, which adds the necessary hooks to your serializer. This will mean class changes, but you won't need to update or override every field.For example:from rest_framework import serializers
from library_app import models
from garnett.ext.drf import TranslatableSerializerMixin


class BookSerializer(TranslatableSerializerMixin, serializers.ModelSerializer):
    class Meta:
        model = models.Book
        fields = "__all__"This will allow you to set the value for a translatable as either a string for the active langauge, or by setting a dictionary that has all languages to be saved (note: this will override the existing language set).
For example:To override just the active language:curl -X PATCH ... -d "{  \"title\": \"Hello\"}"To specifically override a single language (for example, Klingon):curl -X PATCH ...  -H  "X-Garnett-Language-Code: tlh" -d "{  \"title\": \"Hello\"}"To override all languages:curl -X PATCH ... -d "{  \"title\": {\"en\": \"Hello\", \"fr\": \"Bonjour\"}}"Using Garnett with django-reversion and django-reversion-compareThere are a few minor tweaks required to get Garnett to operate properly with
django-reversion and django-reversion-compare based on how they serialise and display data.This is because Garnett does not use the same 'field.attname' and 'field.name' which means serialization in Django will not work correctly.To get django-reversion to work you will need to use a translation-aware serialiser and apply a patch to ensure that django-reversion-compare can show the right information.An example json translation-aware serializer is included with Garnett and this can be applied with the following two settings insettings.py:# In settings.py

GARNETT_PATCH_REVERSION_COMPARE = True
SERIALIZATION_MODULES = {"json": "garnett.serializers.json"}TranslatedFields will list the history and changes in json, but it does do comparisons correctly.Why call it Garnett?Libraries need a good name.Searching for "Famous Translators" will tell you aboutConstance Garnett.Searching for "Django Garnett" showed there was no python library with this name.It did however talk aboutGarnet Clark(also spelled Garnett), a jazz pianist who played with Django Reinhart - the namesake of the Django Web Framework.Voila - a nice nameWarningscontainsacts likeicontainson SQLite only when doing a contains query
it does a case insensitive search. I don't know why -https://www.youtube.com/watch?v=PgGNWRtceagDue to how django sets admin form fields you will not get the admin specific widgets likeAdminTextAreaWidgeton translated fields in the django admin site by default. They can however
be specified explicitly on the corresponding admin model form.Want to help maintain this library?There is a/dev/directory with a docker-compose stack you can ues to bring up a database and clean development environment.Want other options?There are a few good options for adding translatable strings to Django that may meet other use cases. We've included a few other options here, their strengths and why we didn't go with them.django-modeltranslationPros:this library lets you apply translations to external apps, without altering their models.Cons:each translation adds an extra column, which means languages are specified in code, and can't be altered by users later.django-translated-fieldsPros:uses a great context processor for switching lanugages (which is where we got the idea for ours).Cons:Languages are specified in django-settings, and can't be altered by users later.django-parlerPros:Django admin site support.Cons:Languages are stored in a separate table and can't be altered by users later. Translated fields are specified in model meta, away from the fields definition which makes complex lookups harder.