domination / domination /

Full commit
import os
import gettext
from StringIO import StringIO

from flask import _request_ctx_stack
from flaskext.babel import get_locale
from import Translations
from babel.messages.pofile import read_po
from babel.messages.mofile import write_mo
from jinja2 import Markup

class Translatable(unicode):
    def __new__(cls, string, parameters=()):
        inst = super(Translatable, cls).__new__(cls, string)
        inst.pristine_str = string
        inst.parameters = parameters
        return inst

    def __str__(self):
        raise NotImplementedError

    def __unicode__(self):
        t = get_translations()
        if t is None:
            return self % self.parameters
        return t.ugettext(self[:]) % self.parameters

    def __reduce__(self):
        return (Translatable, (self.pristine_str, self.parameters))

def _(string, parameters=()):
    return Translatable(string, parameters)

def taint_filename(basename):
    Make a filename that is supposed to be a plain name secure, i.e.
    remove any possible path components that compromise our system.

    @param basename: (possibly unsafe) filename
    @rtype: string
    @return: (safer) filename
    for x in (os.pardir, ':', '/', '\\', '<', '>'):
        basename = basename.replace(x, '_')

    return basename

def get_translations():
    """ Load .po file, cache .mo file repr in memory and return
    a Babel Translations object. This function is meant for monkey patching
    into Flask-Babel."""
    ctx =
    if ctx is None:
        return None
    translations_dict =
    lock =
    locale = str(get_locale())
    if translations_dict.get(locale) is None:
        mo_file = StringIO()
        dirname = os.path.join(, 'translations')
        transfilename = os.path.join(dirname, taint_filename(locale),
                'LC_MESSAGES', "messages.po")
        if os.path.exists(transfilename):
            catalog = read_po(file(transfilename, "r"))
            write_mo(mo_file, catalog)
            translations = Translations(fileobj=mo_file)
            translations = gettext.NullTranslations()
        translations_dict[locale] = translations
        translations = translations_dict[locale]
    return translations

def ngettext(sing, plu, n, args=None):
    raise NotImplementedError