[Python] Django translations does not work

Discussão em 'Python' iniciado por Stack, Julho 26, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to get Django's translation system to work, following the tutorial here.

    Here are my two views (one for direct output, one for template), neither one works.

    def home(request):
    output = _("hello") # (lazy)
    return HttpResponse(output)

    def with_template(request):
    return render(request, 'translation_template.html')


    here is the template file for the second view :

    {% extends "base_site.html" %}
    {% load i18n %}

    {% block content %}
    <p>{% trans 'hello' %}</p>
    {% language 'tr' %}
    <p>{% trans 'hello' %}</p>
    {% endlanguage %}
    {% language 'tr-TR' %}
    <p>{% trans 'hello' %}</p>
    {% endlanguage %}
    {% endblock %}


    in my settings file, I added the following: (may add parts from before if requested)

    LANGUAGE_CODE = 'en-us'
    # also tried LANGUAGE_CODE = 'tr' and LANGUAGE_CODE = 'tr-TR'
    PROJECT_DIR = os.path.dirname(__file__)

    """
    # tried but didn't work
    from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS
    TEMPLATE_CONTEXT_PROCESSORS = (
    "django.contrib.context_processors.auth",
    "django.core.context_processors.i18n",
    )
    """
    LOCALE_PATHS = ( os.path.join(PROJECT_DIR, 'locale'), )
    LANGUAGES = (
    ('tr', _('Turkish')),
    ('en', _('English')),
    )


    after saving these, I executed in the terminal :

    python ./manage.py makemessages -l tr


    then edited the newly created myproject/locale/tr/LC_MESSAGES/django.po to have this :

    msgid "hello"
    msgstr "merhaba"


    then executed

    python ./manage.py compilemessages


    and restarted the server. the terminal commands show no error, but when I load the views, none of the "hello"s are translated.

    What am I doing wrong here?

    Thanks for any help!

    Edit:


    I found a suspicious code in en/../django.po, probably not relevant, but maybe it is. This is the very beginning of the file. The fuzzy (empty->empty) translation, could it be the problem?

    # SOME DESCRIPTIVE TITLE.
    # Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
    # This file is distributed under the same license as the PACKAGE package.
    # FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
    #
    #, fuzzy
    msgid ""
    msgstr ""

    Continue reading...

Compartilhe esta Página