diff options
-rw-r--r-- | mediagoblin/auth/forms.py | 2 | ||||
-rw-r--r-- | mediagoblin/edit/forms.py | 2 | ||||
-rw-r--r-- | mediagoblin/media_types/image/processing.py | 9 | ||||
-rw-r--r-- | mediagoblin/plugins/oauth/forms.py | 2 | ||||
-rw-r--r-- | mediagoblin/submit/forms.py | 2 | ||||
-rw-r--r-- | mediagoblin/templates/mediagoblin/utils/wtforms.html | 8 | ||||
-rw-r--r-- | mediagoblin/tests/test_util.py | 22 | ||||
-rw-r--r-- | mediagoblin/tools/processing.py | 2 | ||||
-rw-r--r-- | mediagoblin/tools/template.py | 5 | ||||
-rw-r--r-- | mediagoblin/tools/translate.py | 29 | ||||
-rw-r--r-- | mediagoblin/user_pages/forms.py | 2 |
11 files changed, 62 insertions, 23 deletions
diff --git a/mediagoblin/auth/forms.py b/mediagoblin/auth/forms.py index 5484c178..33e1f45c 100644 --- a/mediagoblin/auth/forms.py +++ b/mediagoblin/auth/forms.py @@ -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 diff --git a/mediagoblin/edit/forms.py b/mediagoblin/edit/forms.py index 2673967b..ef270237 100644 --- a/mediagoblin/edit/forms.py +++ b/mediagoblin/edit/forms.py @@ -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): diff --git a/mediagoblin/media_types/image/processing.py b/mediagoblin/media_types/image/processing.py index 142ec2bf..16ffcedd 100644 --- a/mediagoblin/media_types/image/processing.py +++ b/mediagoblin/media_types/image/processing.py @@ -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'])) diff --git a/mediagoblin/plugins/oauth/forms.py b/mediagoblin/plugins/oauth/forms.py index d0a4e9b8..5edd992a 100644 --- a/mediagoblin/plugins/oauth/forms.py +++ b/mediagoblin/plugins/oauth/forms.py @@ -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): diff --git a/mediagoblin/submit/forms.py b/mediagoblin/submit/forms.py index bd1e904f..e9bd93fd 100644 --- a/mediagoblin/submit/forms.py +++ b/mediagoblin/submit/forms.py @@ -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 diff --git a/mediagoblin/templates/mediagoblin/utils/wtforms.html b/mediagoblin/templates/mediagoblin/utils/wtforms.html index 35b4aa04..be6976c2 100644 --- a/mediagoblin/templates/mediagoblin/utils/wtforms.html +++ b/mediagoblin/templates/mediagoblin/utils/wtforms.html @@ -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 %} @@ -39,11 +39,11 @@ {{ 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 %} diff --git a/mediagoblin/tests/test_util.py b/mediagoblin/tests/test_util.py index e4c04b7a..bc14f528 100644 --- a/mediagoblin/tests/test_util.py +++ b/mediagoblin/tests/test_util.py @@ -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( diff --git a/mediagoblin/tools/processing.py b/mediagoblin/tools/processing.py index cff4cb9d..2abe6452 100644 --- a/mediagoblin/tools/processing.py +++ b/mediagoblin/tools/processing.py @@ -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 = {} diff --git a/mediagoblin/tools/template.py b/mediagoblin/tools/template.py index 78d65654..54aeac92 100644 --- a/mediagoblin/tools/template.py +++ b/mediagoblin/tools/template.py @@ -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. diff --git a/mediagoblin/tools/translate.py b/mediagoblin/tools/translate.py index 4acafac7..b20e57d1 100644 --- a/mediagoblin/tools/translate.py +++ b/mediagoblin/tools/translate.py @@ -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): diff --git a/mediagoblin/user_pages/forms.py b/mediagoblin/user_pages/forms.py index e9746a6c..9a193680 100644 --- a/mediagoblin/user_pages/forms.py +++ b/mediagoblin/user_pages/forms.py @@ -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( |