24👍
Just got hit by one. I had the locale/
directory in the root of my project, but by default Django looks for translations in the INSTALLED_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 and settings.py
were both in the root of the django project I could put the following in settings.py
:
from os import path
LOCALE_PATHS = (
path.join(path.abspath(path.dirname(__file__)), 'locale'),
)
- [Django]-How to combine django "prefetch_related" and "values" methods?
- [Django]-Django class based post-only view
- [Django]-Django error: render_to_response() got an unexpected keyword argument 'context_instance'
17👍
I’m trying to provide a complete check list:
-
In
settings.py
, areUSE_I18N
,USE_L10N
,LANGUAGE_CODE
, andLOCALE_PATHS
set properly?- See this list for all allowed values of language identifiers. Note that Simplified Chinese is specified by
zh-hans
, notzh-cn
.
- See this list for all allowed values of language identifiers. Note that Simplified Chinese is specified by
-
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?- You can see an incomplete list of allowed locale name by running
ls your/path/to/python/site-packages/django/conf/locale/
on your machine, or by taking a look at the source code - Note that you have to use
_
rather than-
here. For example, to specify simplified Chinese, executedjango-admin makemessages -l zh_Hans
, notzh_CN
orzh_hans
orzh-Hans
or anything else.
- You can see an incomplete list of allowed locale name by running
-
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:
- If some of your translation is overridden by Django’s default translation, use contextual markers to bypass it. For example,
models.py
first_name = models.CharField(
pgettext_lazy('override default', 'first name'),
max_length=30
)
last_name = models.CharField(
pgettext_lazy('override default', 'last name'),
max_length=150
)
django.po
#: models.py:51
msgctxt "override default"
msgid "first name"
msgstr "姓"
#: models.py:55
msgctxt "override default"
msgid "last name"
msgstr "名"
and you’ll see 姓
, 名
instead of the default 姓氏
, 名字
.
- [Django]-Why does django ORM's `save` method not return the saved object?
- [Django]-Getting TypeError: __init__() missing 1 required positional argument: 'on_delete' when trying to add parent table after child table with entries
- [Django]-Unit testing with django-celery?
9👍
A possible cause is Lazy Translation.
In example, in views.py you should use ugettext:
from django.utils.translation import ugettext as _
But in models.py, you should use ugettext_lazy:
from django.utils.translation import ugettext_lazy as _
- [Django]-How can I render a tree structure (recursive) using a django template?
- [Django]-Django+Postgres: "current transaction is aborted, commands ignored until end of transaction block"
- [Django]-Django: best practice way to get model from an instance of that model
9👍
I was having this problem with my project right now. I had the variable LANGUAGES on settings.py set this way:
LANGUAGES = (
('en', _('English')),
('pt-br', _('Brazilian Portuguese')),
)
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!
- [Django]-Django Templating: how to access properties of the first item in a list
- [Django]-Allow only positive decimal numbers
- [Django]-WARNING: The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8)
8👍
Hi just attach some fixes I had to do in the past:
- Restart the webserver!
In the setting file
– USE_I18N = True
is needed
-
django.middleware.locale.LocaleMiddleware among middleware modules (but this is not your case for sure as long as Django will not care about your local at all)
-
django.core.context_processors.i18n among TEMPLATE_CONTEXT_PROCESSORS
for sure I had other issues related to translations but I don’t remember them all… hope this can help!
- [Django]-What is actually assertEquals in Python?
- [Django]-Django: 'current_tags' is not a valid tag library
- [Django]-How to redirect with post data (Django)
0👍
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 be locale
for an app, not conf/locale
.) Note that the management commands work fine even with the wrong directory structure.
- [Django]-Anyone using Django in the "Enterprise"
- [Django]-Django-allauth: Linking multiple social accounts to a single user
- [Django]-Accessing function as attribute in a Python class
0👍
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 %%
.
- [Django]-Django – makemigrations – No changes detected
- [Django]-ImportError: No module named 'django.core.urlresolvers'
- [Django]-You are trying to add a non-nullable field 'new_field' to userprofile without a default
0👍
My answer here, all my translations were working except for 2 DateField
s that I was using in a ModelForm
.
Turns out that I had a widget in my forms.py
that was not working well with the translations.
I just removed it for now so I can enjoy Xmas =D
- [Django]-Elegant setup of Python logging in Django
- [Django]-How to handle request.GET with multiple variables for the same parameter in Django
- [Django]-What are the options for overriding Django's cascading delete behaviour?