Changeset d7b4ae in indico


Ignore:
Timestamp:
08/01/11 18:01:56 (22 months ago)
Author:
Pedro Ferreira <jose.pedro.ferreira@…>
Branches:
master, hello-world-walkthrough, ipv6, v0.98-series, v0.98.2, v0.98.3, v0.98b2, v0.99, 051b2622c51afb171a1dedb46a0df4fbb0cbd02e, 4c7d4152dff271ba5df5a8606605969cab454080
Children:
0d631e0
Parents:
6c87d3
git-author:
Pedro Ferreira <jose.pedro.ferreira@…> (08/01/11 17:14:22)
git-committer:
Pedro Ferreira <jose.pedro.ferreira@…> (08/01/11 18:01:56)
Message:

[FIX] Lazy translation - no "caching"

  • Babel LazyProxy? objects are not stateless and were thus provoking the accidental "caching" of translated strings;
  • Added Django-like gettext_lazy, that forces lazy translation if there is already a defined locale;
  • Will propose patch upstream (Babel);
Location:
indico/util
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • indico/util/i18n.py

    r331c68 rd7b4ae  
    3737    ngettext = ungettext = lazyTranslations.ungettext 
    3838 
    39 except: 
     39    gettext_lazy = forceLazyTranslations.ugettext 
     40 
     41except ImportError: 
    4042    # no Babel 
    4143    pass 
  • indico/util/translations.py

    ra346fb rd7b4ae  
    2020 
    2121 
    22 from babel.support import Translations, LazyProxy 
     22from babel.support import Translations, LazyProxy as _LazyProxy 
    2323from babel.core import Locale 
    2424from gettext import NullTranslations 
     
    3030nullTranslations = NullTranslations() 
    3131 
     32 
     33class LazyProxy(_LazyProxy): 
     34    """ 
     35    Stateless version of Babel's LazyProxy 
     36    """ 
     37    def value(self): 
     38        # just return 
     39        return  self._func(*self._args, **self._kwargs) 
     40    value = property(value) 
    3241 
    3342 
     
    4554def _tr_eval(func, *args, **kwargs): 
    4655    # ok, eval time... is there a translation? 
     56 
    4757    if 'translation' in ContextManager.get(): 
    4858        # yes? good, let's do it 
    4959        tr = ContextManager.get('translation') 
    5060    else: 
    51         # no? too bad, just don't transate anything 
     61        # no? too bad, just don't translate anything 
    5262        tr = nullTranslations 
    53     return getattr(tr, func)(*args, **kwargs).encode('utf-8') 
     63    res = getattr(tr, func)(*args, **kwargs).encode('utf-8') 
     64    return res 
    5465 
    5566 
     
    5970    It will be then done when the value is finally used 
    6071    """ 
     72 
     73    def __init__(self, forceLazy=False): 
     74        self.force = forceLazy 
     75        super(LazyTranslations, self).__init__() 
     76 
    6177    def _wrapper(self, func, *args, **kwargs): 
    62         # if there is a locale already defined 
    63         if 'translation' in ContextManager.get(): 
     78        # if there is a locale already defined, use it 
     79        # (unless we have forced "lazy mode") 
     80        if 'translation' in ContextManager.get() and not self.force: 
    6481            # straight translation 
    6582            translation = ContextManager.get('translation') 
     
    7794 
    7895lazyTranslations = LazyTranslations() 
     96forceLazyTranslations = LazyTranslations(forceLazy=True) 
    7997lazyTranslations.install(unicode=True) 
Note: See TracChangeset for help on using the changeset viewer.