Malicious uploads test with fake but not really image files working! :)
[mediagoblin.git] / mediagoblin / util.py
index c9f4a0ac72a48d5264b3eb692ca93cd4fe75a7e9..b46c65d94f0e04e6235183bd31c67fd309dfa5f3 100644 (file)
@@ -28,11 +28,13 @@ import copy
 import wtforms
 
 from babel.localedata import exists
+from babel.support import LazyProxy
 import jinja2
 import translitcodec
 from webob import Response, exc
 from lxml.html.clean import Cleaner
 import markdown
+from wtforms.form import Form
 
 from mediagoblin import mg_globals
 from mediagoblin import messages
@@ -93,8 +95,8 @@ def get_jinja_env(template_loader, locale):
         extensions=['jinja2.ext.i18n', 'jinja2.ext.autoescape'])
 
     template_env.install_gettext_callables(
-        mg_globals.translations.gettext,
-        mg_globals.translations.ngettext)
+        mg_globals.translations.ugettext,
+        mg_globals.translations.ungettext)
 
     # All templates will know how to ...
     # ... fetch all waiting messages and remove them from the queue
@@ -478,6 +480,66 @@ def setup_gettext(locale):
         translations=this_gettext)
 
 
+# Force en to be setup before anything else so that
+# mg_globals.translations is never None
+setup_gettext('en')
+
+
+def pass_to_ugettext(*args, **kwargs):
+    """
+    Pass a translation on to the appropriate ugettext method.
+
+    The reason we can't have a global ugettext method is because
+    mg_globals gets swapped out by the application per-request.
+    """
+    return mg_globals.translations.ugettext(
+        *args, **kwargs)
+
+
+def lazy_pass_to_ugettext(*args, **kwargs):
+    """
+    Lazily pass to ugettext.
+
+    This is useful if you have to define a translation on a module
+    level but you need it to not translate until the time that it's
+    used as a string.
+    """
+    return LazyProxy(pass_to_ugettext, *args, **kwargs)
+
+
+def pass_to_ngettext(*args, **kwargs):
+    """
+    Pass a translation on to the appropriate ngettext method.
+
+    The reason we can't have a global ngettext method is because
+    mg_globals gets swapped out by the application per-request.
+    """
+    return mg_globals.translations.ngettext(
+        *args, **kwargs)
+
+
+def lazy_pass_to_ngettext(*args, **kwargs):
+    """
+    Lazily pass to ngettext.
+
+    This is useful if you have to define a translation on a module
+    level but you need it to not translate until the time that it's
+    used as a string.
+    """
+    return LazyProxy(pass_to_ngettext, *args, **kwargs)
+
+
+def fake_ugettext_passthrough(string):
+    """
+    Fake a ugettext call for extraction's sake ;)
+
+    In wtforms there's a separate way to define a method to translate
+    things... so we just need to mark up the text so that it can be
+    extracted, not so that it's actually run through gettext.
+    """
+    return string
+
+
 PAGINATION_DEFAULT_PER_PAGE = 30
 
 class Pagination(object):