Package: django-translations
Django TranslationsDjango model translation for perfectionists with deadlines.GoalThere are two types of content, each of which has its own challenges for translation:Static content: This is the content which is defined in the code.e.g. "Please enter a valid email address."Django already provides asolutionfor translating static content.Dynamic content: This is the content which is stored in the database.(We can't know it beforehand!)Django Translations provides a solution
for translating dynamic content.CompatibilityCurrently, this project is incompatible with PostgreSQL.RequirementsPython (>=3.6, <4)Django (>=2.2, <4)InstallationInstall Django Translations using pip:$pipinstalldjango-translationsAddtranslationsto theINSTALLED_APPSin the settings of your
project:INSTALLED_APPS+=['translations',]Runmigrate:$pythonmanage.pymigrateConfigure Django internationalization and localization settings:USE_I18N=True# use internationalizationUSE_L10N=True# use localizationMIDDLEWARE+=[# locale middleware'django.middleware.locale.LocaleMiddleware',]LANGUAGE_CODE='en-us'# default (fallback) languageLANGUAGES=(# supported languages('en','English'),('en-gb','English (Great Britain)'),('de','German'),('tr','Turkish'),)Please note that these settings are for Django itself.Basic UsageModelInheritTranslatablein any model you want translated:fromtranslations.modelsimportTranslatableclassContinent(Translatable):code=models.Charfield(...)name=models.Charfield(...)denonym=models.Charfield(...)classTranslatableMeta:fields=['name','denonym']No migrations needed afterwards.AdminUse the admin extensions:fromtranslations.adminimportTranslatableAdmin,TranslationInlineclassContinentAdmin(TranslatableAdmin):inlines=[TranslationInline,]This provides specialized translation inlines for the model.QuerySetUse the queryset extensions:>>>fromsample.modelsimportContinent>>>continents=Continent.objects.all(...).distinct(# familiar distinct...).probe(['en','de']# probe (filter, exclude, etc.) in English and German...).filter(# familiar filtering...countries__cities__name__startswith='Köln'...).translate('de'# translate the results in German...).translate_related(# translate these relations as well...'countries','countries__cities',...)>>>print(continents)<TranslatableQuerySet[<Continent:Europa>,]>>>>print(continents[0].countries.all())<TranslatableQuerySet[<Country:Deutschland>,]>>>>print(continents[0].countries.all()[0].cities.all())<TranslatableQuerySet[<City:Köln>,]>This provides a powerful yet familiar interface to work with the querysets.ContextUse the translation context:>>>fromtranslations.contextimportContext>>>fromsample.modelsimportContinent>>>continents=Continent.objects.all()>>>relations=('countries','countries__cities',)>>>withContext(continents,*relations)ascontext:...context.read('de')# read the translations onto the context...print(':')# use the objects like before...print(continents)...print(continents[0].countries.all())...print(continents[0].countries.all()[0].cities.all())......continents[0].countries.all()[0].name='Change the name'...context.update('de')# update the translations from the context......context.delete('de')# delete the translations of the context......context.reset()# reset the translations of the context...print(':')# use the objects like before...print(continents)...print(continents[0].countries.all())...print(continents[0].countries.all()[0].cities.all()):<TranslatableQuerySet[<Continent:Asien>,<Continent:Europa>,]><TranslatableQuerySet[<Country:Deutschland>,]><TranslatableQuerySet[<City:Köln>,]>:<TranslatableQuerySet[<Continent:Asia>,<Continent:Europe>,]><TranslatableQuerySet[<Country:Germany>,]><TranslatableQuerySet[<City:Cologne>,]>This can CRUD the translations of any objects (instance, queryset, list) and their relations.DocumentationFor more interesting capabilities browse through thedocumentation.Support the projectTo support the project you can:⭐️:Starit on GitHub.💻:Contributeto the code base.☕️:Buythe maintainers coffee.