gettext for specific locales

django, internationalization, python

Solution

Context manager django.utils.translation.override activates a new language on enter and reactivates the previous active language on exit

from django.utils import translation

def get_translation_in(language, s):
    with translation.override(language):
        return translation.gettext(s)

print(get_translation_in('de', 'text'))

Problem

I can get the translation in current locale using. ``` from django.utils.translation import ugettext as _ _("password") ``` However in my code (a form to be specific) I want to get the translation in a specific language. I want to be able to say. ``` ugettext_magic("de", "password") ``` I already have the strings translated in the languages I need.

Original source

Related problems