Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django translations does not work

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 ""
like image 923
jeff Avatar asked Apr 16 '15 23:04

jeff


People also ask

How does Django translation work?

Django then provides utilities to extract the translation strings into a message file. This file is a convenient way for translators to provide the equivalent of the translation strings in the target language. Once the translators have filled in the message file, it must be compiled.

What is the use of Gettext_lazy?

gettext_lazy() In definitions like forms or models you should use gettext_lazy because the code of this definitions is only executed once (mostly on django's startup); gettext_lazy translates the strings in a lazy fashion, which means, eg.

What is gettext lazy in Django?

1. It will translate the string to the activated language if you defined a translation. See docs.djangoproject.com/en/4.0/topics/i18n/translation.


2 Answers

and keep in mind if your language has a country in it e.g. fa-IR, in your settings file you must add:

LANGUAGE_CODE = 'fa-IR'

but when you want to call makemessages command you should change - to _:

makemessages -l fa_IR
like image 184
Hojat Modaresi Avatar answered Oct 07 '22 01:10

Hojat Modaresi


I tried almost all of the offered solutions above without any success. Then I realised that I forgot to run compilemessages after makemessages command. So make sure you run:

python manage.py compilemessages 

After running makemessages. What the compilemessages command does is it converts .po files to .mo files in your locale directories. This is needed because .mo files are the only files that gettext can understand. Without .mo files translation won't work, which was the case for me.

like image 30
Cebrail Yilmaz Avatar answered Oct 07 '22 02:10

Cebrail Yilmaz