Django: Translating a string without using language session/cookie

django, internationalization

Solution

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

Problem

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') ```

Original source