Reputation: 101
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
Upvotes: 10
Views: 9013
Reputation: 4921
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
Upvotes: 1
Reputation: 1983
i found this https://gist.github.com/vstoykov/1366794 . It forces I18N machinery to choose settings.LANGUAGE_CODE as the default initial language.
Upvotes: 5
Reputation: 1662
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
Upvotes: 10
Reputation: 409432
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.
Upvotes: 3