I need to change the default language for each user, but in my case the translation is not applied with the selected language. I store the preferred language for each user and set it as follows:
# set default language
try:
user_language_filter = UserFilters.objects.get(user=request.user, element__iexact='defaultLanguage')
user_language = user_language_filter.value.lower()
except UserFilters.DoesNotExist:
user_language = 'en'
if translation.LANGUAGE_SESSION_KEY in request.session:
del request.session[translation.LANGUAGE_SESSION_KEY]
translation.activate(user_language)
request.session[translation.LANGUAGE_SESSION_KEY] = user_language
print(translation.get_language())
Here I retrieve the stored language for the user and if not available, set it to 'en'. print() shows the correct language. In my settings I have:
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
....
LANGUAGES = (
('en', _('English')),
('fr', _('French'))
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, '../locale'),
)
makemessages and compilemessages create the files correctly in the given path.
In my ListView I have:
def get_context_data(self, **kwargs):
context = super(CompanyListView, self).get_context_data(**kwargs)
context.update({
'page_title': _('Manage Companies'),
})
return context
The text to translate is in the locale files and also translated. I also printed the {{ LANGUAGE_CODE }} in the template which shows the wrong one. And in the template the page_title is not translated. Every help is appreciated. Update Seems that the translate in the view is not performed. If in a template I use the {% trans 'to translate' %} this is translated as well ass the model fields in the ModelForms.