I am making a multilingual Django website. I created a messages file, populated and compiled it. I checked the site (the admin in this case,) in my wanted language (Hebrew) and most phrases appear in Hebrew like they should, but some don't. I checked the source and these still appear as _('Whatever')
like they should, also they are translated on the messages file, and yes, I remembered to do compilemessages
.
What are some common causes for translations not to appear like that?
I was having this problem with my project right now. I had the variable LANGUAGES on settings.py set this way:
And a folder structure with a locale folder, and a subfolder pt-br inside. Turns out that my translations weren't loading. The LANGUAGES variable follows the pt-br pattern, and the folders must be on pt_BR pattern. At least that's the only way it's working here!
Another cause can be a wrong directory structure.
Read well the manage command's error message about which directory to create before running the
makemassages
command for the app translation. (It must belocale
for an app, notconf/locale
.) Note that the management commands work fine even with the wrong directory structure.I'm trying to provide a complete check list:
In
settings.py
, areUSE_I18N
,USE_L10N
,LANGUAGE_CODE
, andLOCALE_PATHS
set properly?zh-hans
, notzh-cn
.In
settings.py
, isdjango.middleware.locale.LocaleMiddleware
included inMIDDLEWARE
in correct order?Have you (re)run
django-admin makemessages -l <locale-name>
with the correct local name, from the correct place?ls your/path/to/python/site-packages/django/conf/locale/
on your machine, or by taking a look at the source code_
rather than-
here. For example, to specify simplified Chinese, executedjango-admin makemessages -l zh_Hans
, notzh_CN
orzh_hans
orzh-Hans
or anything else.Have you removed all
fuzzy
tags in your PO file(s)?Have you (re)compiled the OP file(s) with
django-admin compilemessages
?Have you restarted the web server?
Additional notes:
and you'll see
姓
,名
instead of the default姓氏
,名字
.I noticed that when I had
%
in my text, the translated text was not being used. There may be other characters that can cause this problem. I fixed the problem by escaping the%
as%%
.A possible cause is Lazy Translation.
In example, in views.py you should use ugettext:
But in models.py, you should use ugettext_lazy:
Just got hit by one. I had the
locale/
directory in the root of my project, but by default Django looks for translations in theINSTALLED_APPS
directories, and in the default translations. So it didn't find the translations I added. But some of my strings were in the default translations that come with Django (eg "Search") so a few strings were translated, which confused me.To add the directory where my translations were to the list of places that Django will look for translations, I had to set the LOCALE_PATHS setting. So in my case, where the
locale/
directory andsettings.py
were both in the root of the django project I could put the following insettings.py
: