Django i18n not working

Python newbie. I want my site to support English and Chinese. So I just follow the book of django, the internationalization of chapter 19. But it doesn't seem to work for me, the line I hope to show as Chinese is still English. My code and settings are as follows.

[settings.py]

LANGUAGE_CODE = 'zh-cn' USE_I18N = True USE_L10N = True LANGUAGES = ( ('en', 'English'), ('zh-cn', 'Chinese') ) TEMPLATE_CONTEXT_PROCESSORS = { 'django.core.context_processors.i18n', } MIDDLEWARE_CLASSES = ( 'django.middleware.locale.LocaleMiddleware', 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', ) 

In my views.py application, I pre-set the language code as "zh-cn" in the index

 def index( request ): response= render_to_response( 'index.htm' ) response.set_cookie('django_language','zh-cn') return response 

then I hope that the page that will be loaded after index.htm displays the Chinese line.

The Annuar page is displayed in the upload.html file.

 {% load i18n %} <html> <head> {% block head %} {% endblock %} </head> <body> {% block body %} <h1>{% trans 'Upload Demo' %}</h1> {% endblock %} </body> </html> 

After that i do

 django-admin.py makemessages -l zh-cn -e htm 

in my django project folder, and I got django.po at national_version / wf / LC_MESSAGES / django.po whose contents look like

 #, fuzzy msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2012-05-10 18:33+0800\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME < EMAIL@ADDRESS >\n" "Language-Team: LANGUAGE < LL@li.org >\n" "Language: \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" #: uploader/template/base.htm:10 msgid "Upload Demo" msgstr "δΈŠδΌ ζ–‡δ»Ά" 

After that, I call the following command to compile the django-admin.py compilemessages message

I have a django.mo file in some folder with django.po

I go directly to the index page, then I get access to another page that has the line identifier "Download demo". In fact, I still see the English string.

And after trying debugging using the print language code, make sure that the language is installed correctly.

context = RequestContext (request) print context translation.activate ('zh-cn')

Finally i use

 gettext locale/zh-cn/LC_MESSAGES/django.mo "Upload Demo" 

really got a "Download Demo". So I think the problem is here. But why is this happening? I am really embarrassed. Can anyone help me out.

Deeply appreciated any comments or help.


 gettext locale/zh-cn/LC_MESSAGES/django.mo "Upload Demo" 

I think I made a mistake. The above command returns a string that matches the string entered as the string identifier, not the translated one. In the above command, this is β€œDownload demo”, that is, if your change to β€œDownload demo” in the command above β€œbla bla”, you will be β€œbla bla”.

+4
source share
5 answers

It may be too late, but I'm sure your problem is probably due to the lack of LOCALE_PATHS tuples in your project settings.

After an hour of hair pulling, I solved the same problem by simply adding this to the settings.py file in my project:

 LOCALE_PATHS = ( '/home/myuser/Projects/Some_project_root/My_django_project_root/locale', ) 

And here is what this directory looks like:

 locale/ β”œβ”€β”€ de β”‚  └── LC_MESSAGES β”‚  β”œβ”€β”€ django.mo β”‚  └── django.po β”œβ”€β”€ en β”‚  └── LC_MESSAGES β”‚  β”œβ”€β”€ django.mo β”‚  └── django.po └── es └── LC_MESSAGES β”œβ”€β”€ django.mo └── django.po 
+6
source

Your code blocks are a bit dirty, so reading them all is pretty hard. But you can start with your .mo file. It contains annotation #, fuzzy . Fuzzy means that the build of the script was not sure of the translation and therefore requires the attention of the translator (= you). Start by checking all tagged translations #, fuzzy . If the translation is correct (or after correcting the incorrect translation), delete the annotation #, fuzzy . Then run the compilation of messages again. This may solve your problem.

 #, fuzzy msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" "POT-Creation-Date: 2012-05-10 18:33+0800\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" "Language: \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" 

See also: django fuzzy string translation not showing

Friendship, Wout

+3
source

You can see that LANGUAGE_CODE does not match django_language. I think get_language in utils.py does not handle underscore shortcuts correctly. get_language will return zh-cn, but it should not be cut into cn. Instead, zh-cn should be converted to zh_CN.

So you should use the code below in settings.py

 LANGUAGES = ( ('en', _('English')), ('zh-cn', _('Simplified Chinese')), ) 

and run the following command from terminal

 django-admin.py makemessages -l zh_CN django-admin.py compilemessages 

This works great for me.

+2
source

I had a similar problem: compilemessages only works in locale in the current directory. It was a cure:

 find . -name "locale" | while read VAR1; do CURR="``pwd``";cd $VAR1/..; echo "in $VAR1";django-admin.py compilemessages ; cd $CURR; done` 
+1
source

I believe the problem is with the makemessages and compilemessages that you submit. @ Vishnu answer above is correct.

TL; DR

Do not use zh-cn for locale. Use zh_CN .

 django-admin.py makemessages -l zh_CN django-admin.py compilemessages -l zh_CN 

Description

"Local name" as defined in Django Docs :

The locale name, or the specification of the language of the form ll, or the combined language and the specification of the country of the form ll_CC . Examples: this, de_AT , es, pt_BR. The language part is always lowercase, and the country part is upper case. The separator is an underscore.

In the makemessages example (at the bottom of the makemessages documentation), you will notice the use of the name language :

 django-admin makemessages --locale=pt_BR django-admin makemessages --locale=pt_BR --locale=fr django-admin makemessages -l pt_BR django-admin makemessages -l pt_BR -l fr django-admin makemessages --exclude=pt_BR django-admin makemessages --exclude=pt_BR --exclude=fr django-admin makemessages -x pt_BR django-admin makemessages -x pt_BR -x fr 

You will notice the same in the compilemessages example:

 django-admin compilemessages --locale=pt_BR django-admin compilemessages --locale=pt_BR --locale=fr -f django-admin compilemessages -l pt_BR django-admin compilemessages -l pt_BR -l fr --use-fuzzy django-admin compilemessages --exclude=pt_BR django-admin compilemessages --exclude=pt_BR --exclude=fr django-admin compilemessages -x pt_BR django-admin compilemessages -x pt_BR -x fr 
0
source

Source: https://habr.com/ru/post/1411703/


All Articles