Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: default language i18n

I have a website that is written in dutch. Now I have to provide a second language for that website which is french.

So I surrounded all text that needs to be translated with the gettext function, created the po files and compiled those to mo files. I also created a view that sets the django_language session to the appropriate language code. So now the french version is working but I can't switch back to the dutch version.

So I was wondering do I need to create a po/mo file for the dutch version also? The text that's being past to gettext is already in dutch. Is there a way to say use the 'default text'?

This is the view I use to add the language code to my session:

class LanguagePickerView(RedirectView):
    url = '/'

    def get(self,request,*args, **kwargs):
        request.session['django_language'] = self.kwargs.get('language')
        return super(LanguagePickerView, self).get(request, args, kwargs)

In my templates I use the following urls:

<a href='{% url web-language 'nl-nl' %}'>NL</a>
<a href='{% url web-language 'fr' %}'>fr</a>
like image 335
Pickels Avatar asked Apr 28 '11 08:04

Pickels


People also ask

How do I change the default language in Django?

Default language strings are not stored in po/mo files, they go directly in code and templates - seems that you have this right. You can switch back to it, by setting the session variable django_language back to dutch. This made it work for me, thanks a lot.

What is i18n in Django?

{% load i18n %} is needed for internationalization. The purpose of internationalization is to allow a single application to read in multiple languages. In order to do this: you need a few hooks called translation strings. To give your template access to these tags, put {% load i18n %} toward the top of your template..

What is Gettext_lazy Django?

ugettext is a unicode version of a translatable string. ugettext_lazy is a "lazy" version of that. Lazy strings are a Django-ism; they are string-like objects that don't actually turn into the real string until the last possible minute. Often, you can't know how to translate a string until late in the process.


1 Answers

Default language strings are not stored in po/mo files, they go directly in code and templates - seems that you have this right.

You can switch back to it, by setting the session variable django_language back to dutch.

Ensure, that you have your settings set the right way:

LANGUAGE_CODE = 'nl' #default language

LANGUAGES = (
  ('nl', _('Dutch')),
  ('fr', _('French')),
)

Don't forget, that you don't have to write code to switch between languages by your self. Better to use special django view (quote from django book):

As a convenience, Django comes with a view, django.views.i18n.set_language, that sets a user’s language preference and redirects back to the previous page.

Activate this view by adding the following line to your URLconf:

(r'^i18n/', include('django.conf.urls.i18n')),
like image 171
Silver Light Avatar answered Oct 15 '22 02:10

Silver Light