简体   繁体   中英

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 (eg 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.

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 (eg the pox library). I gave up and settled for using en-US as the default for the project and listing the other variants (eg en-au) as alternatives.

may I suggest you to put a breakpoint into the LocaleMiddleware Class?

In this way, you might found out a clue which thing was actually getting your way from getting the right Language.

Becaue according the source code of LocaleMiddleware Class and the How Django discovers language preference , there could be so many things can affect the result.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM