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
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.
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.
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
i found this https://gist.github.com/vstoykov/1366794 . It forces I18N machinery to choose settings.LANGUAGE_CODE as the default initial language.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With