Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django translation creates po and mo files, but fails in translation and displays translation placeholders

I am using Django 1.6.5 with virtualenv (Vitrual Environment) and apache2

I have following settings.py structure:

from django.utils.translation import ugettext_lazy as _

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

LANGUAGE_CODE = 'tr_TR'
LANGUAGE_COOKIE_NAME = 'wm_lang'
LOCALE_PATHS = (
    '/home/mustafa/python/myproject/locale'
)

USE_I18N = True
USE_L10N = True

LANGUAGES = (
    ('tr', _(u'Türkçe')),
    ('en', _(u'English')),
)

I have a template file looks like that

{% extends .... %}
{% load i18n %}

{% block content %}
    <h2>{% trans 'Hosgeldin' %}</h2>
....
....

I run following to create po files

django-admin.py makemessages  --locale=tr --locale=en

which creates following files

/home/mustafa/python/myproject/locale/tr/LC_MESSAGES/django.po
/home/mustafa/python/myproject/locale/en/LC_MESSAGES/django.po

I Apply translation strings so my django.po translation files look like that

en:

#: templates/websitesi/index.html:6
msgid "Hosgeldin"
msgstr "Welcome, that is a test"

tr:

#: templates/websitesi/index.html:6
msgid "Hosgeldin"
msgstr "Hoşgeldiniz, bu bir deneme"

Then I compile them with

django-admin.py compilemessages

and django creates django.mo files near django.po files for both languages.

But when I visit the page, I see Hosgeldin (translation placeholder) instead of translated text.

Writing {{ LANGUAGE_CODE }} to template displays selected language code. When my address is someadress/en, language code displayed as en...

What am I missing?

UPDATE: This is a section of my root urls.py

urlpatterns = patterns('',
    (r'^i18n/', include('django.conf.urls.i18n')),
)

urlpatterns += i18n_patterns('',
    url(r'^admin/docs/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

urlpatterns += staticfiles_urlpatterns()

urlpatterns += i18n_patterns('websitesi.views',
    url(r'^$', 'index', name='index'),  # This is my home page
                         ....
                         ....

Then I go to my_local_server/ or my_local_server/tr or my_local_server/en to test but it all fails in translation.

When I go to admin via my_local_server/tr/admin or my_local_server/en/admin, django trqnslations works just fine.

like image 674
FallenAngel Avatar asked Jun 13 '14 14:06

FallenAngel


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 a .MO file Poedit?

poedit is a tool for translators. It allows merging between new texts in a POT file (compared to the current translation in the PO file) and produces both PO and MO as output. There are some great open-source web-based tools for translating GetText files.

What is Django PO file?

Finally, we add Locale_paths which is folder inside our app created by django-admin startapp. 4) Make sure you create this folder structure under app's directory. folder structure. This way Django will know where to place generated .po files for each language. PO file is a portable object file, which is text-based.


1 Answers

Finally, it turns out to be a typo, as expected.

In settings.py

LOCALE_PATHS = (
    '/home/mustafa/python/myproject/locale'
)

But Django expects LOCALE_PATHS to be a tuple, so adding a comma solved the issue...

LOCALE_PATHS = (
    '/home/mustafa/python/myproject/locale',
)
like image 100
FallenAngel Avatar answered Oct 07 '22 07:10

FallenAngel