3

I am able to change the language of the user with built-in function of django

path('i18n/', include('django.conf.urls.i18n')),

In order to send email to user translated in his language, I want to know the activated language of the user.

How can I save the language of the in db?

Is there another way to know the language?

This is the session:

from django.utils import translation
request.session[translation.LANGUAGE_SESSION_KEY]
Eu Chi
  • 533
  • 1
  • 6
  • 22

2 Answers2

1

In your User model, you need to add a new field:

    interface_language = models.CharField(max_length=10, blank=True)

Then you need to create middleware like this:

from django.utils import translation

def language_middleware(get_response):
    def middleware(request):
        user = getattr(request, 'user', None)
        if user is not None and user.is_authenticated:
            translation.activate(user.interface_language)
        response = get_response(request)
        translation.deactivate()
        return response
    return middleware

The middleware needs to be added to your settings after django.contrib.auth.middleware.AuthenticationMiddleware:

MIDDLEWARE = [
    # ...
    'django.contrib.auth.middleware.AuthenticaitonMiddleware',
    'language_middleware',
    # ...
]

Alternatively, you can also use a Pypi package like django-user-language-middleware.

Flimm
  • 136,138
  • 45
  • 251
  • 267
  • Inevitably, If `user is None or not user.is_authenticated`, you will use `get_language()`, [no](https://github.com/laura-barluzzi/django-user-language-middleware/blob/c1498fe74bef89d4e998b4a725f727b71c748e21/user_language_middleware/middleware.py#L37)? – raratiru Sep 26 '20 at 12:13
  • I want to save the language chosen by the user into the model. So I guess this middleware doesn't work. or it doesn't do the job I want. – hcttepe Dec 13 '22 at 12:45
0

Saving the language preference to the database is an overhead that you can avoid using the tools that Django offers.

django.utils.translation.get_language, returns the language used in the current thread.

As documented in Django, this is an example:

from django.utils import translation

def welcome_translated(language):
    cur_language = translation.get_language()
    try:
        translation.activate(language)
        text = translation.gettext('welcome')
    finally:
        translation.activate(cur_language)
    return text

You can also use django.utils.translation.get_language_from_request to analyze the request to find what language the user wants the system to show, as explained in this answer.

Be sure, however, that you have correctly setup Django.

Marina Mele has written a tutorial you can check out for a second point of view.

raratiru
  • 8,748
  • 4
  • 73
  • 113
  • useful answer. but some here are more explanations about what I meant: `for user in all_users: send_email_to_user(user,his_language_preference)`.... do you understand? language will render the template in his language – Eu Chi Dec 28 '18 at 18:17
  • @EuChi OK, I see, you need to send an email after the session is concluded. I think it is better to ask the user in a registration form, what is his preferred language of communication. – raratiru Dec 28 '18 at 18:26
  • I did, there is a select dropdown where the user can choose his language, made with the built-in django function. so I want to be able to know within the loop when sending email, how to render the templates in the chosen language of a specific user. – Eu Chi Dec 28 '18 at 18:28
  • @EuChi Can you update the question with the code used to collect the information from the user and save it to the database? – raratiru Dec 28 '18 at 18:32
  • No I can't this is what I can't do. I want to knwo how to do it, how to get it when the changes his language. I thought Django had a signal for that – Eu Chi Dec 28 '18 at 19:09