Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to maintain different country versions of same language in Django?

I would like to have a few different versions of the same language in Django, customized for different countries (e.g. locale/en, locale/en_CA, locale/en_US, etc.). If there is no language for specific country I would expect to use the default language version (locale/en)).

Then in the settings file for each site I specify LANGUAGE_CODE and LANGUAGES.

For some reason, even if I specify the following settings, the locale/en_US translations are still used:

LANGUAGE_CODE = 'en'
LANGUAGES = (
    ('en', ugettext('English')),
)

Though I clearly specify that the language code should be en (not en-us).

Am I missing something? Already tried to find the answer in multiple places, including Django documentation.

like image 809
Jordan Jambazov Avatar asked Sep 19 '16 16:09

Jordan Jambazov


People also ask

Is Django multilingual?

Django Simple Multilingual Support for models An inefficient, minimal and utterly simple approach to model translation based on foreign key relations and attribute proxying. The project code is forked from the original project by yazzgoth on Google code.

What is internationalization in Django?

The goal of internationalization and localization is to allow a single web application to offer its content in languages and formats tailored to the audience. Django has full support for translation of text, formatting of dates, times and numbers, and time zones.

How do I change the language on Django?

Create a language switcher template Firstly, add a {% load i18n %} line at the top to load Django i18n-related tags. Open up an HTML form with its action set to send a POST request to set_language view. Note: As mentioned in the Django documentation, this view specifically expects a POST request to be sent.

How does Django discover language preferences?

This header is sent by your browser and tells the server which language(s) you prefer, in order by priority. Django tries each language in the header until it finds one with available translations. Failing that, it uses the global LANGUAGE_CODE setting.


2 Answers

This is a quirk of Python (not specifically Django) and the gettext module.

Ticket 8626 was raised on the Django issue tracker around the time of the 1.0 release and after some suggestions and debate, the Django devs deemed it a "won't fix".

There are suggestions in the ticket thread to use 'en-en' as the default. My memory is a little rough but if I recall correctly this approach didn't play well with other parts of my i18n tooling (e.g. the pox library). I gave up and settled for using en-US as the default for the project and listing the other variants (e.g. en-au) as alternatives.

like image 152
Dwight Gunning Avatar answered Oct 07 '22 19:10

Dwight Gunning


A workaround to the issue would be to add following snippet to your settings.py file.

import locale
locale.locale_alias.pop('en', None)

Special credit to Venelin Stoykov who was able to investigate the behavior of the Python locale module.

like image 29
Jordan Jambazov Avatar answered Oct 07 '22 18:10

Jordan Jambazov