Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#16690 closed New feature (worksforme)

Add translation.activate decorator/context manager

Reported by: Ralph Broenink Owned by: nobody
Component: Internationalization Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Currently, internationalization outside of templates and views requires several lines of code, identically each time:

from django.utils import translation
def welcome_translated(language):
    cur_language = translation.get_language()
        text = translation.ugettext('welcome')
    return text


However, this could easily be fitted within both decorators and context managers, allowing the following syntax:

def welcome_translated(language):
    return translation.ugettext('welcome')


def welcome_translated(language):
    with translation.activate(language):
        return translation.ugettext('welcome')

This would decrease the lines of code and the developer would not have to care for the case when the execution somehow fails and forgets to add the try/finally clause.

Change History (3)

comment:1 Changed 7 years ago by Jannis Leidel

Resolution: worksforme
Status: newclosed

There is already one at

def welcome_translated(language):
    with translation.override(language):
        return translation.ugettext('welcome')

comment:2 Changed 7 years ago by Ralph Broenink

Didn't notice that there was one already. Does not seem to be a documented feature.

Is it an idea to also support it as a decorator?

comment:3 Changed 7 years ago by Jannis Leidel

Yeah, it's not documented yet, it should probably be mentioned on

I don't see much use in a decorator since that'd be evaluated at import time, making the variable override useless.

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