4

I'm looking for advice on how best to enhance Django's translation framework.

The aim is to modify the translation system so that instead of immediately falling back to the msgid when a mapping cannot be found, the system first attempts to obtain a translation from another (predefined) language code.

Background:

The desire to first attempt to fallback to another language, rather than msgids is because the current set of 'msgids' are not human readable. The project uses tokens/identifiers. The human readable english is in it's own translation PO file alongside all the other languages. This is advantageous when it comes to tweaking the english copy... there's no need to touch the other language files.

The attempt:

def ugettext_fb(message):
    trans_output = translation.ugettext(message)

    if trans_output == message:
        preset_language = translation.get_language()
        logger.warn('Missing \'%s\' translation for %s' % (preset_language, message))

        translation.activate(settings.LANGUAGE_FALLBACK)
        trans_output = translation.ugettext(message)
        translation.activate(preset_language)

    return trans_output

This wrapper seems to work although I'm interested if SO folks know of a better way?

Also, looking at the Django source, it looks like I'll need to wrap each of the gettext(_lazy) functions individually. Is there a smarter way to do that?

M.javid
  • 6,387
  • 3
  • 41
  • 56
Dwight Gunning
  • 2,485
  • 25
  • 39

0 Answers0