Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django default language

I have a django application which has default language set to French. All translation strings in the code and html pages are in french. Switching between differents language works fine. But now I need to hide french language, so I changed LANGUAGE_CODE to 'en-us', but default page always displays in french, did I miss something ?

Thanks

like image 982
ark42 Avatar asked Oct 19 '11 12:10

ark42


People also ask

What languages are in Django?

Django is written in Python, which runs on many platforms. That means that you are not tied to any particular server platform, and can run your applications on many flavors of Linux, Windows, and macOS.

What is Django translation?

Django then provides utilities to extract the translation strings into a message file. This file is a convenient way for translators to provide the equivalent of the translation strings in the target language. Once the translators have filled in the message file, it must be compiled.


4 Answers

I faced this problem just recently, here's how I could manage to fix it without changing any browser's locale language:

the idea is to create a middleware to force language translation based on the LANGUAGE_CODE setting, here's how the middleware might look like:

from django.conf import settings
from django.utils import translation

class ForceLangMiddleware:

    def process_request(self, request):
        request.LANG = getattr(settings, 'LANGUAGE_CODE', settings.LANGUAGE_CODE)
        translation.activate(request.LANG)
        request.LANGUAGE_CODE = request.LANG

save this snippet as middleware.py in your main app (I assume it's called main) and then add main.middleware.ForceLangMiddleware to your MIDDLEWARE_CLASSES

like image 187
mmbrian Avatar answered Sep 25 '22 15:09

mmbrian


i found this https://gist.github.com/vstoykov/1366794 . It forces I18N machinery to choose settings.LANGUAGE_CODE as the default initial language.

like image 35
Sandeep Balagopal Avatar answered Sep 23 '22 15:09

Sandeep Balagopal


I had some trouble with this too once... It's because most modern web-browsers send their locale setting in the request, and Django automatically uses that language instead if it can.

Unfortunately I don't remember what I did to solve this, but I hope it gives you some pointers where to look or search for.

like image 30
Some programmer dude Avatar answered Sep 21 '22 15:09

Some programmer dude


In your settings.py file you have a LANGUAGES tuple.

LANGUAGES = (
    ('en', gettext('English')),
    ('sv', gettext('Swedish')),
    ('no', gettext('Norwegian')),
)

If you're using Django Multilingual, you can also set DEFAULT_LANGUAGE setting:

DEFAULT_LANGUAGE = 1 # the first one in the list
like image 41
ApPeL Avatar answered Sep 25 '22 15:09

ApPeL