Skip to main content

Translator

Translator is an app for collecting translations for specified keys in django admin.

Quick start

  1. Install django-translator: pip install django-translator

  2. Add “translator, taggit, modeltranslation” to your INSTALLED_APPS setting. Please note that modeltranslation needs to be before django.contrib.admin:

    INSTALLED_APPS = (
    'modeltranslation',
    'django.contrib.admin',
    ...
    'taggit',
    'translator',
    )
  3. You have to set the migrations folder for the translator, because we have to add migrations for the set languages. Add the following to your settings file:

    MIGRATION_MODULES = {
        'translator': 'my_project.translator_migrations',
    }
  4. Create a translator_migrations python package in your project folder (where your settings.py usually is).

  5. Run python manage.py makemigrations translator to create the translator models based on the languages you specified in your settings file.

  6. Run python manage.py migrate to migrate the translator models to your database.

  7. If you intend to use it in the templates, add ‘translator.context_processors.translator’ to TEMPLATE_CONTEXT_PROCESSORS

    TEMPLATE_CONTEXT_PROCESSORS = (
           ...
       'translator.context_processors.translator',
    )
  8. Create translation keys in your templates and models.

    Examples:

    Template:

    {{ translator.a_key }}

    models.py:

    from translator.util import translator_lazy as _
    ...
    
    class Product(models.Model):
        name = models.TextField(verbose_name=_('a_key'))
  9. Visit the templates. The keys get collected lazy.

  10. Translate the keys in the admin.

  11. You can disable the translator by setting DJANGO_TRANSLATOR_ENABLED to False.

  12. Use a double underscore in your translation keys to make use of the filter in the admin (e.g. “header__title” creates a filter called “header”). If you need another separator, set it as DJANGO_TRANSLATOR_CATEGORY_SEPARATOR in your setting file.

Custom Models

If you find yourself in a situation where you need to use the features of django-translator in a second isolated model, feel free to add one:

  1. Create a new model in your app:

    from translator.models import TranslationBase
    
    class MyCustomTranslation(TranslationBase):
        pass
  2. Create a new file translation.py and register your model for modeltranslation support:

    from modeltranslation.translator import translator, TranslationOptions
    from myapp.models import MyCustomTranslation
    
    class MyCustomTranslationOptions(TranslationOptions):
        fields = ('description',)
    
    translator.register(MyCustomTranslation, MyCustomTranslationOptions)
  3. Add a django admin in admin.py:

    from django.contrib import admin
    from translator.admin import TranslationAdministration
    from myapp.models import MyCustomTranslation
    
    @admin.register(MyCustomTranslation)
    class CustomTranslationAdmin(TranslationAdministration):
        pass
  4. Add your model to your settings file:

    DJANGO_TRANSLATOR_MODELS = {
        'custom_translation': 'myapp.models.MyCustomTranslation',
    }
  5. Create translation keys in your templates and models.

    Examples:

    Template:

    {{ custom_translation.a_key }}

    models.py:

    from myapp.util import custom_translation_lazy
    ...
    
    class Product(models.Model):
        name = models.TextField(verbose_name=translator_lazy('a_key', 'custom_translation'))

Settings

Customize the translator in your settings.py file with these settings:

DJANGO_TRANSLATOR_CACHE_TIMEOUT = timeout in seconds, if not set defaults to DEFAULT_TIMEOUT, which is either the CACHES['TIMEOUT'] setting or 300 (5 minutes)

Project Home

https://github.com/dreipol/django-translator

PyPi

https://pypi.python.org/pypi/django-translator

Metadata

Release files for django-translator 2.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-translator 2.0.1
File Size Uploaded Provenance
django_translator-2.0.1.tar.gz 6.1 kB Without provenance Details

Release files / django_translator-2.0.1.tar.gz

Download URL django_translator-2.0.1.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
59bad454280b226d81da085fb836ab4315d4f8822812c0fcc561096659e640b6
BLAKE2b-256 checksum
How to use checksums
7aff812348bdf7231412e7246582421d1dbd449a3ee554a61d1ad6fca7f1bb4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.13

Security for django-translator 2.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page