[Fixed]-Access untranslated content of Django's ugettext_lazy

6👍

Another two options. Not very elegant, but not private api and is not slow.

  • Number one, define your own ugettext_lazy:

    from django.utils import translation
    
    def ugettext_lazy(str):
        t = translation.ugettext_lazy(str)
        t.message = str
        return t
    
    >>> text = ugettext_lazy('Yes')
    >>> text.message
    "Yes"
    >>> activate('lt')
    >>> unicode(text)
    u"Taip"
    >>> activate('en')
    >>>> unicode(text)
    u"Yes"
    
  • Number two: redesign your code. Define untranslated messages separately from where you use them:

    gettext = lambda s: s
    some_text = gettext('Some text')
    
    lazy_translated = ugettext_lazy(text)
    untranslated = some_text
    
👤Ski

7👍

This is the better version of your second solution

from django.utils import translation

the_string = ugettext_lazy('the content')
with translation.override('en'):
    content = unicode(the_string)

1👍

You can do that (but you shouldn’t):

the_string = ugettext_lazy('the content')
the_string._proxy____args[0]

Leave a comment