Merge remote-tracking branch 'refs/remotes/rodney757/issue643' into mergetest
authorChristopher Allan Webber <cwebber@dustycloud.org>
Fri, 26 Apr 2013 16:12:46 +0000 (11:12 -0500)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Fri, 26 Apr 2013 16:12:46 +0000 (11:12 -0500)
mediagoblin/auth/forms.py
mediagoblin/edit/forms.py
mediagoblin/media_types/image/processing.py
mediagoblin/plugins/oauth/forms.py
mediagoblin/submit/forms.py
mediagoblin/templates/mediagoblin/utils/wtforms.html
mediagoblin/tests/test_util.py
mediagoblin/tools/processing.py
mediagoblin/tools/template.py
mediagoblin/tools/translate.py
mediagoblin/user_pages/forms.py

index 5484c178bf9158d5ba92309c19a628864a34d3d5..33e1f45c95cff95f2399dddb87a6e6fca086171e 100644 (file)
@@ -17,7 +17,7 @@
 import wtforms
 
 from mediagoblin.tools.mail import normalize_email
-from mediagoblin.tools.translate import fake_ugettext_passthrough as _
+from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
 
 def normalize_user_or_email_field(allow_email=True, allow_user=True):
     """Check if we were passed a field that matches a username and/or email pattern
index 2673967bce69e18474c86b6a16d01c7c9fc9b6a7..ef2702370e62d86e0be96dc7e120e16932d2b10b 100644 (file)
@@ -17,7 +17,7 @@
 import wtforms
 
 from mediagoblin.tools.text import tag_length_validator, TOO_LONG_TAG_WARNING
-from mediagoblin.tools.translate import fake_ugettext_passthrough as _
+from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
 from mediagoblin.tools.licenses import licenses_as_choices
 
 class EditForm(wtforms.Form):
index 142ec2bfb73474417d2268da7f3284ef64092e01..16ffcedd3d9844439393c7069fc9f749026de434 100644 (file)
@@ -37,13 +37,12 @@ PIL_FILTERS = {
     'ANTIALIAS': Image.ANTIALIAS}
 
 
-def resize_image(entry, filename, new_path, exif_tags, workdir, new_size,
-                 size_limits=(0, 0)):
+def resize_image(proc_state, filename, new_path, exif_tags, workdir, new_size):
     """
     Store a resized version of an image and return its pathname.
 
     Arguments:
-    entry -- the entry for the image to resize
+    proc_state -- the processing state for the image to resize
     filename -- the filename of the original image being resized
     new_path -- public file path for the new resized image
     exif_tags -- EXIF data for the original image
@@ -120,7 +119,7 @@ def process_image(proc_state):
     # Always create a small thumbnail
     thumb_filepath = create_pub_filepath(
         entry, name_builder.fill('{basename}.thumbnail{ext}'))
-    resize_image(entry, queued_filename, thumb_filepath,
+    resize_image(proc_state, queued_filename, thumb_filepath,
                 exif_tags, conversions_subdir,
                 (mgg.global_config['media:thumb']['max_width'],
                  mgg.global_config['media:thumb']['max_height']))
@@ -136,7 +135,7 @@ def process_image(proc_state):
         medium_filepath = create_pub_filepath(
             entry, name_builder.fill('{basename}.medium{ext}'))
         resize_image(
-            entry, queued_filename, medium_filepath,
+            proc_state, queued_filename, medium_filepath,
             exif_tags, conversions_subdir,
             (mgg.global_config['media:medium']['max_width'],
              mgg.global_config['media:medium']['max_height']))
index d0a4e9b8ab45359f5c848828fa3b185bfb8b8579..5edd992a509a991676d0fd569b70248ce9806472 100644 (file)
@@ -19,7 +19,7 @@ import wtforms
 from urlparse import urlparse
 
 from mediagoblin.tools.extlib.wtf_html5 import URLField
-from mediagoblin.tools.translate import fake_ugettext_passthrough as _
+from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
 
 
 class AuthorizationForm(wtforms.Form):
index bd1e904f8b3cafb0aa5d2ec7bba6ea32e3ac56dd..e9bd93fde6427de0c6f4d3dc37fbf3b9717adda5 100644 (file)
@@ -18,7 +18,7 @@
 import wtforms
 
 from mediagoblin.tools.text import tag_length_validator
-from mediagoblin.tools.translate import fake_ugettext_passthrough as _
+from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
 from mediagoblin.tools.licenses import licenses_as_choices
 
 
index 35b4aa0499bcac10ed87f1d30727fb797fd1b7ef..be6976c2b324f8de7ca9bf9b94452005d228d5aa 100644 (file)
@@ -19,7 +19,7 @@
 {# Render the label for a field #}
 {% macro render_label(field) %}
   {%- if field.label.text -%}
-    <label for="{{ field.label.field_id }}">{{ _(field.label.text) }}</label>
+    <label for="{{ field.label.field_id }}">{{ field.label.text }}</label>
   {%- endif -%}
 {%- endmacro %}
 
     {{ field }}
     {%- if field.errors -%}
       {% for error in field.errors %}
-        <p class="form_field_error">{{ _(error) }}</p>
+        <p class="form_field_error">{{ error }}</p>
       {% endfor %}
     {%- endif %}
     {%- if field.description %}
-      <p class="form_field_description">{{ _(field.description)|safe }}</p>
+      <p class="form_field_description">{{ field.description|safe }}</p>
     {%- endif %}
   </div>
 {%- endmacro %}
@@ -59,7 +59,7 @@
 {% macro render_table(form) -%}
   {% for field in form %}
     <tr>
-      <th>{{ _(field.label.text) }}</th>
+      <th>{{ field.label.text }}</th>
       <td>
         {{field}}
         {% if field.errors %}
index e4c04b7a24699dc40f9a5c08ab34f62798037510..bc14f528e7c02499a6146255c526ed8e900cf4fe 100644 (file)
@@ -104,6 +104,28 @@ def test_locale_to_lower_lower():
     assert translate.locale_to_lower_lower('en_us') == 'en-us'
 
 
+def test_gettext_lazy_proxy():
+    from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
+    from mediagoblin.tools.translate import pass_to_ugettext, set_thread_locale
+    proxy = _(u"Password")
+    orig = u"Password"
+
+    set_thread_locale("es")
+    p1 = unicode(proxy)
+    p1_should = pass_to_ugettext(orig)
+    assert p1_should != orig, "Test useless, string not translated"
+    assert p1 == p1_should
+
+    set_thread_locale("sv")
+    p2 = unicode(proxy)
+    p2_should = pass_to_ugettext(orig)
+    assert p2_should != orig, "Test broken, string not translated"
+    assert p2 == p2_should
+
+    assert p1_should != p2_should, "Test broken, same translated string"
+    assert p1 != p2
+
+
 def test_html_cleaner():
     # Remove images
     result = text.clean_html(
index cff4cb9d5d37e0c0fdf9d3a041dac3ef403c46d1..2abe64525dd8af46860f4ea453d09662e7ac0227 100644 (file)
@@ -21,8 +21,6 @@ import traceback
 from urllib2 import urlopen, Request, HTTPError
 from urllib import urlencode
 
-from mediagoblin.tools.common import TESTS_ENABLED
-
 _log = logging.getLogger(__name__)
 
 TESTS_CALLBACKS = {}
index 78d656542c55d4e4678ab5120530f650f3fbeddd..54aeac926971e4247f1dd52ee4b8fa3420b6d1ec 100644 (file)
@@ -14,7 +14,6 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from math import ceil
 
 import jinja2
 from jinja2.ext import Extension
@@ -27,7 +26,7 @@ from mediagoblin import mg_globals
 from mediagoblin import messages
 from mediagoblin import _version
 from mediagoblin.tools import common
-from mediagoblin.tools.translate import get_gettext_translation
+from mediagoblin.tools.translate import set_thread_locale
 from mediagoblin.tools.pluginapi import get_hook_templates
 from mediagoblin.tools.timesince import timesince
 from mediagoblin.meddleware.csrf import render_csrf_form_token
@@ -44,7 +43,7 @@ def get_jinja_env(template_loader, locale):
     (In the future we may have another system for providing theming;
     for now this is good enough.)
     """
-    mg_globals.thread_scope.translations = get_gettext_translation(locale)
+    set_thread_locale(locale)
 
     # If we have a jinja environment set up with this locale, just
     # return that one.
index 4acafac788157fd0d9aeef2fd1bfefff8a4b3325..b20e57d1ea2791e1e9773ed95d532cc41e5a8c7b 100644 (file)
@@ -42,6 +42,22 @@ def set_available_locales():
     AVAILABLE_LOCALES = locales
 
 
+class ReallyLazyProxy(LazyProxy):
+    """
+    Like LazyProxy, except that it doesn't cache the value ;)
+    """
+    @property
+    def value(self):
+        return self._func(*self._args, **self._kwargs)
+
+    def __repr__(self):
+        return "<%s for %s(%r, %r)>" % (
+            self.__class__.__name__,
+            self._func,
+            self._args,
+            self._kwargs)
+
+
 def locale_to_lower_upper(locale):
     """
     Take a locale, regardless of style, and format it like "en_US"
@@ -112,6 +128,11 @@ def get_gettext_translation(locale):
     return this_gettext
 
 
+def set_thread_locale(locale):
+    """Set the current translation for this thread"""
+    mg_globals.thread_scope.translations = get_gettext_translation(locale)
+
+
 def pass_to_ugettext(*args, **kwargs):
     """
     Pass a translation on to the appropriate ugettext method.
@@ -122,7 +143,6 @@ def pass_to_ugettext(*args, **kwargs):
     return mg_globals.thread_scope.translations.ugettext(
         *args, **kwargs)
 
-
 def pass_to_ungettext(*args, **kwargs):
     """
     Pass a translation on to the appropriate ungettext method.
@@ -133,6 +153,7 @@ def pass_to_ungettext(*args, **kwargs):
     return mg_globals.thread_scope.translations.ungettext(
         *args, **kwargs)
 
+
 def lazy_pass_to_ugettext(*args, **kwargs):
     """
     Lazily pass to ugettext.
@@ -144,7 +165,7 @@ def lazy_pass_to_ugettext(*args, **kwargs):
 
     you would want to use the lazy version for _.
     """
-    return LazyProxy(pass_to_ugettext, *args, **kwargs)
+    return ReallyLazyProxy(pass_to_ugettext, *args, **kwargs)
 
 
 def pass_to_ngettext(*args, **kwargs):
@@ -166,7 +187,7 @@ def lazy_pass_to_ngettext(*args, **kwargs):
     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)
+    return ReallyLazyProxy(pass_to_ngettext, *args, **kwargs)
 
 def lazy_pass_to_ungettext(*args, **kwargs):
     """
@@ -176,7 +197,7 @@ def lazy_pass_to_ungettext(*args, **kwargs):
     level but you need it to not translate until the time that it's
     used as a string.
     """
-    return LazyProxy(pass_to_ungettext, *args, **kwargs)
+    return ReallyLazyProxy(pass_to_ungettext, *args, **kwargs)
 
 
 def fake_ugettext_passthrough(string):
index e9746a6caa1aa88e7f466edb28c49f0fdae0bd89..9a1936805500f6cfda70204575e68651160f0046 100644 (file)
@@ -16,7 +16,7 @@
 
 import wtforms
 from wtforms.ext.sqlalchemy.fields import QuerySelectField
-from mediagoblin.tools.translate import fake_ugettext_passthrough as _
+from mediagoblin.tools.translate import lazy_pass_to_ugettext as _
 
 class MediaCommentForm(wtforms.Form):
     comment_content = wtforms.TextAreaField(