Django: language in field description in forms won't change - django

When I change language on my Django-powered site, everything works fine except the translation of the field descriptions in the forms. The description is still displayed in the old language. Only the forms already visited that session are affected.
It seems like some sort of caching problem, but I don't use any caching (as far as I know) and all other parts of the site behave as expected (templates and random text using django.utils.translaction.ugettext work just fine).
Summarized:
When I open the site and change language, and then visit a form: everything works as expected. All text is translated.
When I visit a form, change language and return to the form: everything is translated, but the form stays in the old language.
Restarting the web server forces the language to change: then Django behaves as in (1) for the current session. Until the language is changed again.
Help is appreciated!
Kind regards,
Patrick

Fixed it by changing
from django.utils.translation import ugettext as _
to
from django.utils.translation import ugettext_lazy as _
for my forms.

Related

How to preview a Wagtail page with translated fields in different languages?

I have a multi-language Wagtail website with two languages (English and German) using duplicated fields in my page models, e.g. a text block has two fields, text_de and text_en, and I define text as a translated field following the example in the translating content documentation. (I.e. I am NOT duplicating the whole page tree.) Here's an example how that looks in my code:
[models.py]
class MyPage(Page):
...
text_de = models.CharField(max_length=1024)
text_en = models.CharField(max_length=1024)
text = TranslatedField('text_en', 'text_de')
Everything works perfectly fine, in templates I can just use {{ text }} and depending on the active language (using i18n patterns and LocaleMiddleware) the correct version is displayed.
BUT: I have issues with getting a page preview in both languages.
When an editor creates a draft page in Wagtail admin and clicks on 'preview', then a page preview is shown in the language used within the Wagtail admin, i.e. in the language defined by the current editor's language preferences in his or her account settings.
How could the editor also preview the page in another language (without switching the language in his or her account settings back and forth)?
Is there maybe a way to construct a view that sets a different language before creating the page preview? Or is there another way to solve this?
I've tried to find out when Wagtail/Django decides which language it should serve and found the method get_url_parts of wagtail/admin/core/models, Page class. The page_path returned from this function is suffixed with '/de' or '/en', depending on the editor's account settings.
I can permanently change the language in which a page is displayed using translation.activate(). E.g. if I add translation.activate('en') to the get_context method of the MyPage class, then a MyPage-page and its previews are always shown in English. That's not really helpful.
I tried to construct a view, that first sets the language and then redirects to the preview like this:
[views.py]
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.utils import translation
def preview_language(request, pk=None, language='en'):
if language == 'de':
translation.activate(language)
else:
translation.activate('en')
return HttpResponseRedirect(reverse('wagtailadmin_pages:preview_on_edit', args=(pk,)))
But the language is set back to the editor's language from the account settings when the redirect is performed. And I am not sure that this is actually the way to go ...
Does anyone have a (different?) idea how to enable editors to easily preview a page with translated fields in different languages?
I am currently using Wagtail 2.4, Django 2.1, Python 3.5.
Any help is greatly appreciated! :-)
Wagtail's "preview modes" feature would probably help here: https://docs.wagtail.io/en/stable/reference/pages/model_reference.html#wagtail.core.models.Page.preview_modes
You could define English and German as two preview modes on your page model, and override the serve_preview method to activate the appropriate translation based on the received mode_name argument.

Django multi-lingual site : how to?

I want to have my website in three languages and allow user to change preffered language somewhere in my template. My models will need translating. I found a few libraries perhaps I should use :
https://code.google.com/p/django-multilingual/ for traslating my models
https://pypi.python.org/pypi/django-localeurl/1.4 for language prefix in URL's
https://docs.djangoproject.com/en/dev/topics/i18n/ for translating static parts of my site
However I can't understand how these come together. Perhaps someone could give me a rundown of steps to translate my website.
You can use i18n_patterns instead of django-localeurl if you use Django 1.4. or later.
i18n_patterns will set active language based on URL prefix.
Django I18n will serve translated messages in python code and templates based on active language . (You will have to makemessages and compilemessages to create and compile translations).
For translations in model there are lot of libraries, majority of them use active language to serve field translation. I prefer django-modeltranslation, which allows translating of 3rd party app models without changing their code.
Hope this helps!

django admin language changes randomly

I have recently started working with django internationalization.
There is only one view and matching template for which I added translatable strings and made translations for 3 languages.
Now I notice that the django admin language changes in what seems to be random. It changes to more languages than the ones I translated to (english, french and german).
Anyone know what might have caused it?
Seems like this is cause by a weird behaviour of django's translation module.
translation.activate() changes the language for the whole process.
What I had to do is call translation.deactivate() after rendering the template.

how to make interaction between different django apps in a single site?

I have just learnt about Django apps. I want to know that within one site, if I make different apps. like, users, profiles, polls, blogs,comments, jobs , applications then how can I manage them to make them intereactive? And is it how the concept of app should be? I want to have things loosely coupled that's why asking? Rails work on the way of REST, so do Django support that also with the use of apps? May be my questions seems a bit ambiguous because I am new to django and some of my concepts are still messed up.
Please tell what ever you know.
The general idea is that apps should be as loosely coupled as possible. The goal is to have completely self-contained functionality. Now, of course, that's not always possible and many times it even makes sense to bring in functionality from another app. To do that, you simply import whatever you need. For example, if your "blogs" app needed to work with your Comment model in your "comments" app you'd simply add the following to the top of the python file you're working in:
from comments.models import Comment
You can then use Comment as if it were defined right in the same file.
As far as REST goes, Django's views are much more fluid. You can name your view whatever you like; you need only hook it up to the right urlpattern in urls.py. Django views can return any content type, you just prepare the response and tell it what mimetype to serve it as (the default is HTML).

python/django problem with sessions and language

I have the following problem: on the main page I can change language. New language is saved in request.session['django_language']. I also have SESSION_COOKIE_DOMAIN set to my site, so session should be inherited by subdomains. And it is, because after changing language I check request.session['django_language'] in subdomains and it's fine. Then I use
django.middleware.locale.LocaleMiddleware
to translate my pages. And it works perfectly... only on main site! If I change language and refresh main site - it is ok. However, if I change language and go to a subpage (for example /LogIn), then the page is NOT translated at all. It stays on default language. This is really strange, because if I use
{% load i18n %}
{% get_current_language as lang %}
in this subpage, then lang is good language. There is no mistake. What kind of problem can it be? Some suggestions?
It seems that using ugettext was the problem. I changed to ugettext_lazy and now it works perfectly fine. For some reason ugettext translates with old language. It works fine now.