views:

110

answers:

1

Is there a way in django to translate a string to another language than the one specified in the language session or cookie?

I mean doing it while calling ugettext. Something like this, where 'en' is the language code:

from django.utils.translation import ugettext as _

def translate():
   translated_string =  _('Translate me', 'en')
+3  A: 

Looking through django/middleware/locale.py, it seems like you can just call:

from django.utils import translation
translation.activate(language)

Here's the entire LocaleMiddleware class, for your edification:

def process_request(self, request):
    language = translation.get_language_from_request(request)
    translation.activate(language)
    request.LANGUAGE_CODE = translation.get_language()

def process_response(self, request, response):
    patch_vary_headers(response, ('Accept-Language',))
    if 'Content-Language' not in response:
        response['Content-Language'] = translation.get_language()
    translation.deactivate()
    return response
David Wolever
Thank you! This works like a charm.
kaba