Override default Django translations

Related searches

I have a template with this:

{% trans "Log out" %}

This is translated automatically by Django to Spanish as Terminar sesión. However I would like to translate it as Cerrar sesión.

I have tried to add this literal to the .po file, however I get an error saying this literal is duplicated when I compile the messages.

Is there a way to change/override default Django translations?

Thanks.


The easiest way is to collect the .po file found in the django.contrib.admin locale folder and re-compiling it (you can use POEdit for doing so).

You could also override the django.contrib.admin templates by putting them in your projects templates folder (for example: yourproject/templates/admin/change_form.html) then running makemessages from the project root (although this is no longer supported for django 1.4 alpha if i'm correct)

edit: Robert Lujo's answer is the clean method

How to override the django admin translation?, Django Internationalization override translation mechanisim not working as expected Project-level translations should be used by default, overriding those � When you need to translate all or most of django default messages, the other possibility (which I didn't tried) is to copy default django .po file in our locale or some other special folder, and fix translations and register the folder (if new) in LOCALE_PATHS django settings file as first entry in the list.


Translation | Django documentation, This page shows Python examples of django.utils.translation.override. By default, logging out will generate a fresh sessionid cookie. We want to use the� Django uses this language as the default translation – the final attempt if no better matching translation is found through one of the methods employed by the locale middleware (see below). If all you want is to run Django with your native language all you need to do is set LANGUAGE_CODE and make sure the corresponding message files and their


#11384 (Django Internationalization override translation , however I get an error saying this literal is duplicated when I compile the messages. Is there a way to change/override default Django translations? Thanks. override is a callable within the django.utils.translation module of the Django project. Example 1 from django-cms. django-cms (project website) is a Python-based content management system (CMS) library for use with Django web apps that is open sourced under the BSD 3-Clause "New" license.


This is another solution we deployed. It involved monkey patching the _add_installed_apps_translations method of the DjangoTranslation class to prioritize the translations of the project apps over the translations of the Django apps.

# patches.py
from __future__ import absolute_import, unicode_literals

import os

from django.apps import apps
from django.core.exceptions import AppRegistryNotReady
from django.utils.translation.trans_real import DjangoTranslation


def patchDjangoTranslation():
    """
    Patch Django to prioritize the project's app translations over
    its own. Fixes GitLab issue #734 for Django 1.11.
    Might needs to be updated for future Django versions.
    """

    def _add_installed_apps_translations_new(self):
        """Merges translations from each installed app."""
        try:
            # Django apps
            app_configs = [
                app for app in apps.get_app_configs() if app.name.startswith('django.')
            ]

            # Non Django apps
            app_configs = [
                app for app in apps.get_app_configs() if not app.name.startswith('django.')
            ]
            app_configs = reversed(app_configs)
        except AppRegistryNotReady:
            raise AppRegistryNotReady(
                "The translation infrastructure cannot be initialized before the "
                "apps registry is ready. Check that you don't make non-lazy "
                "gettext calls at import time.")
        for app_config in app_configs:
            localedir = os.path.join(app_config.path, 'locale')
            if os.path.exists(localedir):
                translation = self._new_gnu_trans(localedir)
                self.merge(translation)

    DjangoTranslation._add_installed_apps_translations = _add_installed_apps_translations_new

Then in the .ready() method of your main app, call patchDjangoTranslation:

from .patches import patchDjangoTranslation

class CommonApp(MayanAppConfig):
    app_namespace = 'common'
    app_url = ''
    has_rest_api = True
    has_tests = True
    name = 'mayan.apps.common'
    verbose_name = _('Common')

    def ready(self):
        super(CommonApp, self).ready()
        patchDjangoTranslation()  # Apply patch

The main change are these lines:

        # Django apps
        app_configs = [
            app for app in apps.get_app_configs() if app.name.startswith('django.')
        ]

        # Non Django apps
        app_configs = [
            app for app in apps.get_app_configs() if not app.name.startswith('django.')
        ]
        app_configs = reversed(app_configs)

The original are:

        app_configs = reversed(list(apps.get_app_configs()))

Instead of interpreting the translations of the apps in the order they appear in the INSTALLED_APPS setting, this block outputs the list of apps placing the project apps before the Django apps. Since this only happens when determining the translation to use, it doesn't affect any other part of the code and no other changes are necessary.

It works on Django version 1.11 up to 2.2.

Python Examples of django.utils.translation.override, The internationalization override is not working as expected. The documentation says: you can write applications that include their own translations, and you can override base translations in your project path. Or, you can just build a big project out of several apps and put all translations into one big project message file. The choice is yours.


Override default Django translations - django - iOS, Overriding attribute access (defaults, fallbacks) There are no default values or fallbacks, only a wrapped attribute access. The default attribute getter and setter functions simply return or set the field for the current language (as returned by django.utils.translation.get_language).


The fix of #30439 created a new problem: Under some circumstances, translations for one language in different territories can override each other. In the case we've run into, users in New Zealand (en-NZ) received translations for South Africa (en-ZA).


The Django admin site¶. One of the most powerful parts of Django is the automatic admin interface. It reads metadata from your models to provide a quick, model-centric interface where trusted users can manage content on your site.