Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django - how to make translation work?

I'm trying to render a template in a different language using i18n. I did everything I could read about, from setting the language code, creating and compiling translation files, including the translation tags in the template and all that, and my template still renders in English, even through the {{ LANGUAGE_CODE }} variable points to the correct (and different) code I intended to render. What am I missing?

template:

{% extends "base.html" %} {% load i18n %} {% get_current_language as LANGUAGE_CODE %} {% get_available_languages as LANGUAGES %} {% get_current_language_bidi as LANGUAGE_BIDI %} {% block title %}{% trans "translation test" %}{% endblock %} {% block content %} <div id="some-text">   {% trans "some translated text goes here" %}   {% blocktrans %}   <ol>     <li>here are some</li>     <li>items that should be</li>     <li>translated as well</li>   </ol>   {% endblocktrans %}   <ul>       <li>The current language is <b>{{ LANGUAGE_CODE }}</b></li>       {% if LANGUAGE_BIDI %}         <li>The current language is bidirectional</li>       {% else %}         <li>The current language is <b>not</b> bidirectional</li>       {% endif %}       <li>Available languages are:         <ul>         {% for lang in LANGUAGES %}           <li>{{ lang.1}}</li>         {% endfor %}         </ul>       </li>   </ul> </div> {% endblock %} 

view:

from django.shortcuts import render_to_response from django.template import RequestContext from pdb import set_trace as debugger def check(request):     return render_to_response('index.html', context_instance=RequestContext(request) 

command line (I did fill in the correct translations in .po files):

$ django-admin.py makemessages -l he-il -e html $ django-admin.py compilemessages 

settings.py:

# Language code for this installation. All choices can be found here: # http://www.i18nguy.com/unicode/language-identifiers.html LANGUAGE_CODE = 'he-il'  gettext = lambda s: s LANGUAGES = (     ('he-il', gettext('Hebrew')),     ('en-us', gettext('English')), )  # If you set this to False, Django will make some optimizations so as not # to load the internationalization machinery. USE_I18N = True  TEMPLATE_CONTEXT_PROCESSORS = (     "django.core.context_processors.auth",     "django.core.context_processors.i18n", )  MIDDLEWARE_CLASSES = (     'django.middleware.common.CommonMiddleware',     'django.contrib.sessions.middleware.SessionMiddleware',     'django.middleware.locale.LocaleMiddleware',     'django.contrib.auth.middleware.AuthenticationMiddleware', ) 
like image 309
sa125 Avatar asked Dec 02 '09 12:12

sa125


2 Answers

Just add the paths of the locale files generated to the settings.py file like the following

LOCALE_PATHS = ( "/xxx/xxx/Projects/xxx/sites/avb/locale/",) 
like image 110
AbuFida Avatar answered Sep 23 '22 19:09

AbuFida


This is a full solution that I have been using from Django 1.4 and still in 1.7.1:

In settings.py …

Add to MIDDLEWEAR_CLASSES, locale, it enables language selection based on request:

'django.middleware.locale.LocaleMiddleware', 

Add LOCALE_PATHS, this is where your translation files will be stored:

LOCALE_PATHS = (     os.path.join(PROJECT_PATH, 'locale/'), ) 

Enable I18N:

USE_I18N = True     

Set LANGUAGES that you will be translating the site to:

ugettext = lambda s: s LANGUAGES = (     ('en', ugettext('English')),     ('fr', ugettext('French')),     ('pl', ugettext('Polish')), ) 

Add i18n template context processor to TEMPLATE_CONTEXT_PROCESSORS, requests will now include LANGUAGES and LANGUAGE_CODE:

'django.core.context_processors.i18n', 

In urls.py :

In url_patterns, add the below, it will enable the set language redirect view:

url(r'^i18n/', include('django.conf.urls.i18n')), 

See Miscellaneous in Translations for more on this.

Add the following imports, and encapsulate the urls you want translated with i18n_patterns. Here is what mine looks like:

from django.conf.urls.i18n import i18n_patterns from django.utils.translation import ugettext_lazy as _  urlpatterns = patterns('',     url(r'^admin/', include(admin.site.urls)),     url(r'^i18n/', include('django.conf.urls.i18n')), )  urlpatterns += i18n_patterns('',     (_(r'^dual-lang/'), include('duallang.urls')),     (r'^', include('home.urls')), ) 

Now anywhere you use text and want to convert it, import lazytext and wrap every string with it like so _('text'), you can even go to your other urls.py files and do url translation like so:

url(_(r'^dual_language/$'), landing, name='duallang_landing'), 

You can wrap text that you want translated in your other files, such as models.py, views.py etc.. Here is an example model field with translations for label and help_text:

name = models.CharField(_('name'), max_length=255, unique=True, help_text=_("Name of the FAQ Topic")) 

In your html templates...

Do same for your templates and load the i18n templatetag and use trans and transblock on the static stuff you want to translate. Here is an example:

{% load i18n %}  {% trans "This is a translation" %}<br><br> {% blocktrans with book_t='book title'|title author_t='an author'|title %} This is {{ book_t }} by {{ author_t }}. Block trans is powerful! {% endblocktrans %} 

Now run a makemessages for each of your locales:

./manage.py makemessages -l pl 

And now all is left is to go into your /locales folder, and edit each of the .po files. Fill in the data for each msgstr. Here is one such example of that:

msgid "English" msgstr "Angielski" 

And finally compile the messages:

./manage.py compilemessages 

For model instance data translation you can use some of the reusable packages available like

There is a lot more to learn with translations and internationalization is closely related to this topic, so check out the docs for it too. I also recommend checking out some of the internationalization packages available for Django like django-rosetta, and django-linguo. They help translate model content, django-rosetta does not create new entries for this in your database, while django-linguo does.

If you followed this you should be off to a good start. I believe this is the most standardized way to get your site running in multiple languages. Cheers!

like image 21
radtek Avatar answered Sep 22 '22 19:09

radtek