Django translation in 1.9 - django

I have a django project with one translation (call it 'en').
I have a simple menu for a user to select the language.
I used to do this:
If he selected the 'en' option, I would do:
request.session[translation.LANGUAGE_SESSION_KEY] = 'en'
and that would work.
If I wanted to disable the 'en' translation, I would do:
request.session[translation.LANGUAGE_SESSION_KEY] = ''
and that would restore the template original language.
Means if I have this in template:
{% trans "some non english text" %}
then setting the session cookie to '', would show the original text (no trans), which is what I wanted.
All of this true for django 1.7.
In django 1.9, this simply won't work, setting the session cookie language key to '', still keep the active translation.
How do I disable the translation correctly? I just want to show the website in its default language (output the template as is, no translation file).
EDIT, SOLUTION:
It worked differently in django < 1.8. Since 1.8 setting the session key to empty string does not mean "fallback to no translation". So what I did is to set the LANGUAGE_CODE setting to a code which I dont have a translation file for. That simply cause the template to appear as is.

Related

How can I use Django's translation strings with Wagtailtrans (Add-on for supporting multi language Wagtail sites)

So I have a website which is using the Wagtailtrans extension for Wagtail. I basically enables multi language by duplicating the page tree. So the url gets fixed with the language-code at the start.
I can translate all my content which I define through my models perfectly fine. Here's an example of how that works:
class ServicesPage(MetadataPageMixin, TranslatablePage):
description = models.CharField(max_length=255, blank=True,)
content_panels = Page.content_panels + [
FieldPanel('description', classname="full")
]
Instead of Page you define it as TranslatablePage in your model. All is working fine, however I still need some additional strings which I don't define in my models to be translated. I just use Django's translation feature with {% load i18n %} and then the strings wrapped inside {% trans "String" %}.
So far so good, I defined my two languages in Wagtail admin (Wagtail trans creates an option for that) which in this case is English and Dutch. I set English as the main language so the strings are in english.
I use ./manage.py makemessages and it creates a .po file for me, with all the tagged strings in there. At last I use ./manage.py compilemessages.
But translated strings are not showing up when I switch to Dutch language, it still displays the english strings. So I suspect it needs some additional tweaking to work with Wagtailtrans, but I can't seem to figure out how this set-up should be. Can anyone help me out?

Django - how to set language manually?

I set the language in my Django app during login with:
user_language = user.profile.language
translation.activate(user_language)
request.session['django_language'] = user_language
request.session[translation.LANGUAGE_SESSION_KEY] = user_language
What happens is that app doesnt load language as I would like to.
If I use in my template {% get_current_language %} I see in my template 'en' or 'pl'. I press 'refresh page' and language changes. It's also strange that if I change a language for one user, it also affects every user.
My question is how do I set the language manually, so it stays constant, and for only specific user. I would like to set the language just after the login.
There is built-in Django middleware that let's users control their own language settings.
From the Django docs (read this link):
If you want to let each individual user specify which language they prefer, then you also need to use the LocaleMiddleware. LocaleMiddleware enables language selection based on data from the request. It customizes content for each user.
You can use get_current_language() once you're sure you've fully implemented everything you need from the Translation docs.

Variable translation in django

I want to translate in any language django permission names. To do this i extend django.contrib.auth.models.Permission model in django like this :
class ExtendedPermission(Permission):
translation_name = models.CharField(_('translation_name'),max_length=255)
Then i want to update this table in management commands. First 'name' field taken from Permission model, than it must be translated into another language for instance tr to use in ExtendedPermission model as translation_name field.This function in management/commands:
def try_translate(self,permission):
translation.activate(settings.LANGUAGE_CODE)
translation.activate('tr')
translated_str =translation.ugettext(permission)
translation.deactivate()
return translated_str
When run this command the variable does not appear in .po file.But if i give string that i want to translate like this :
ugettext("name")
msgid = "name" appears in .po file and i can edit msgstr but i can not get the translated msgstr from .mo file with using ugettext.
I add 'django.middleware.locale.LocaleMiddleware', to settings file.I also run commands makemessages and compilemessages.
As I understand it, the Django i18n support works off static data, which you pre-generate running makemessages. It looks for _(), {% trains %}, etc, which require static strings.
If permissions is not know statically ( e.g. Without reading a database ), then it cannot process it during makemessages.

Django CMS and Rosetta: Can't get template messages translated

Is there any know issue about using Django CMS and Django Rosetta together.
I cant get the "static" template messages translated although it appears correctly translated in the Rosetta interface.
All the dynamic content is correctly translated. Only the one I´ve set up around the `{% trans 'blabla' %} does not work.
Gotcha!
Just add in settings.py
LOCALE_PATHS = (BASE_DIR + "/locale/",)
It worked for me.

How do I tell why Django is ignoring the Accept-Language header?

I have a Django app (on Google App Engine) that I wish to internationalize.
settings.py:
USE_I18N = True
LANGUAGE_CODE = 'en'
# Restrict supported languages (and JS media generation)
LANGUAGES = (
('en', 'English'),
('fr', 'French'),
)
MIDDLEWARE_CLASSES = (
'ragendja.middleware.ErrorMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
# i18n
'django.middleware.locale.LocaleMiddleware',
...
I have generated .po and .mo files for my app in locale/fr/LC_MESSAGES (though not at the global level).
I set my browser Accept-Language heading to "fr" and Django ignores it. When I look at request.LANGUAGE_CODE it is always "en".
I can tell the browser is proper because I visit some other i18n-aware site and it returns French.
How do I find what Django thinks is missing about my setup?
I saw this question and it didn't help me.
I am running Django 1.0 using app engine patch 1.0.2.2 on Google App Engine.
There's a certain order that Django does things in terms of i18n.
First it checks LANGUAGE_CODE. It's the site-wide language and if nothing else is set, this is the language the user gets.
Second, since you've added the LocaleMiddleware, it checks if django_language is set in the user session. I would suggest clearing the session information in the DB or creating a completely new user to try with.
Third, it checks if there's a django_language cookie set (or, actually, the name of the cookie is defined by the LANGUAGE_COOKIE_NAME). I would suggest deleting this cookie.
Fourth, it looks for the Accept-Language HTTP header. Which is where your browser setting comes in.
Good luck!
Taken from this page, you can remove the HTTP_ACCEPT_LANGUAGE from the request and fall back on the LocaleMiddleware:
class ForceDefaultLanguageMiddleware(object):
"""
Ignore Accept-Language HTTP headers
This will force the I18N machinery to always choose settings.LANGUAGE_CODE
as the default initial language, unless another one is set via sessions or cookies
Should be installed *before* any middleware that checks request.META['HTTP_ACCEPT_LANGUAGE'],
namely django.middleware.locale.LocaleMiddleware
"""
def process_request(self, request):
if request.META.has_key('HTTP_ACCEPT_LANGUAGE'):
del request.META['HTTP_ACCEPT_LANGUAGE']