Opened 7 years ago

Closed 7 years ago

Last modified 5 years ago

#10122 closed (wontfix)

setting locales for a single HTTP context

Reported by: mathijs Owned by: jacob
Component: Documentation Version: 1.0
Severity: Keywords: internationalisation, context, rendering, template, locale
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Currently there's no good explenation on how to set the locale temporarily, eg. when rendering one template in a different language used in emails.
I recently needed this as my users trigger email notifications for other users. These should be translated into the recieving users' languages. The code I used was:

from django.utils.translation import check_for_language, activate, deactivate, to_locale, get_language, ugettext as _
from django.http import HttpRequest
from django.template.loader import render_to_string
from django.template import RequestContext
from django.contrib.sessions.middleware import SessionMiddleware

# 1. Get the current language: (if any)
cur_locale = to_locale(get_language())
# 2. create a request (& optionally add session middleware)
request = HttpRequest()
# 3. activate the users language: this loads the translation files
if check_for_language(target.user.language):
locale = to_locale(get_language())
# 4. set the language code in the request (& optionally session)
request.LANGUAGE_CODE = locale
request.session['django_language'] = locale
# 5. translate any string here (passed into the template):
msg_dict = { 'title': _('This is the title and will be translated'), 'body': _('Message body') }
# 6. render template with correct language:
rendered = render_to_string("some_template.html",  msg_dict, RequestContext(request));
# 7. reset back to the old locale:

I don't know if there is any easier way to do this, if so let me know ;) if not, this could be included in the docs

Change History (8)

comment:1 Changed 7 years ago by mathijs

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

I forgot to mention that target.user is my UserProfile class and the language property is thus the language preference for that user.

comment:2 Changed 7 years ago by mathijs

  • milestone set to post-1.0

comment:3 Changed 7 years ago by mathijs

  • Needs documentation set

comment:4 Changed 7 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

comment:5 Changed 7 years ago by jacob

  • milestone set to 1.1
  • Triage Stage changed from Unreviewed to Accepted

comment:6 Changed 7 years ago by jacob

  • Owner changed from nobody to jacob
  • Status changed from new to assigned

comment:7 Changed 7 years ago by jacob

  • Resolution set to wontfix
  • Status changed from assigned to closed

This is a pretty specific need, so it's not of interest to most doc readers. I"d suggest posting this on; that's a better place for it.

comment:8 Changed 5 years ago by jacob

  • milestone 1.1 deleted

Milestone 1.1 deleted

Note: See TracTickets for help on using tickets.
Back to Top