Django

Code

Changeset 904

Show
Ignore:
Timestamp:
10/17/05 08:54:52 (3 years ago)
Author:
hugo
Message:

i18n: fixed handling of gettext_lazy - now it doesn't memoize function values any more

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/i18n/django/utils/functional.py

    r884 r904  
    33        return args[0](*(args[1:]+moreargs), **dict(kwargs.items() + morekwargs.items())) 
    44    return _curried 
    5  
    6 class NoneSoFar: 
    7     """ 
    8     NoneSoFar is a singleton that denotes a missing value. This can be 
    9     used instead of None - because None might be a valid return value. 
    10     """ 
    11     pass 
    12 NoneSoFar = NoneSoFar() 
    13  
    14 def force(value): 
    15     """ 
    16     This function forces evaluation of a promise. It recognizes a promise 
    17     by it's magic __force__ function and just applies it and returns 
    18     the result. If the value isn't a promise, it just returns the value. 
    19     """ 
    20  
    21     return getattr(value, '__force__', lambda : value)() 
    22  
    23 def forced(value): 
    24     """ 
    25     This function returns true if a value is either not a promise or 
    26     is already forced. This uses the __forced__ magic method. 
    27     """ 
    28     return getattr(value, '__forced__', lambda : True)() 
    295 
    306def lazy(func, *resultclasses): 
     
    3410    you need to give result classes or types - at least one is needed 
    3511    so that the automatic forcing of the lazy evaluation code is 
    36     triggered. 
     12    triggered. Results are not memoized - the function is evaluated 
     13    on every access. 
    3714    """ 
    3815 
     
    5027           self.__args = args 
    5128           self.__kw = kw 
    52            self.__result = NoneSoFar 
    5329           self.__dispatch = {} 
    5430           for resultclass in resultclasses: 
     
    5733                   setattr(self, k, self.__promise__(resultclass, k, v)) 
    5834         
    59         def __force__(self): 
    60            """ 
    61            This function forces the evaluation of a promise and 
    62            returns the value itself. 
    63            """ 
    64            if self.__result is NoneSoFar: 
    65               self.__result = self.__func(*self.__args, **self.__kw) 
    66            return self.__result 
    67          
    68         def __forced__(self): 
    69            """ 
    70            This returns true if the promise is forced and false if not. 
    71            """ 
    72            if self.__result is NoneSoFar: return False 
    73            else: return True 
    74    
    7535        def __promise__(self, klass, funcname, func): 
    7636         
    77            """ 
    78            This function builds a wrapper around some magic method and 
    79            registers that magic method for the given type and methodname. 
    80            """ 
     37            """ 
     38            This function builds a wrapper around some magic method and 
     39            registers that magic method for the given type and methodname. 
     40            """ 
    8141     
    82            def __wrapper__(*args, **kw): 
    83               """ 
    84               This wrapper function automatically forces the evaluation of 
    85               a lazy value if the value isn't already forced. It then applies 
    86               the given magic method of the result type. 
    87               """ 
    88              res = self.__force__(
    89               return self.__dispatch[type(res)][funcname](res, *args, **kw) 
     42            def __wrapper__(*args, **kw): 
     43                """ 
     44                This wrapper function automatically triggers the evaluation of 
     45                a lazy value. It then applies the given magic method of the 
     46              result type. 
     47                """ 
     48                res = self.__func(*self.__args, **self.__kw
     49                return self.__dispatch[type(res)][funcname](res, *args, **kw) 
    9050   
    91            if not self.__dispatch.has_key(klass): self.__dispatch[klass] = {} 
    92            self.__dispatch[klass][funcname] = func 
    93            return __wrapper__ 
     51            if not self.__dispatch.has_key(klass): 
     52                self.__dispatch[klass] = {} 
     53            self.__dispatch[klass][funcname] = func 
     54            return __wrapper__ 
    9455   
    9556    def __wrapper__(*args, **kw): 
     
    10263    return __wrapper__ 
    10364 
    104 if __name__ == '__main__': 
    105     def anton(a,b): 
    106         return a+b 
    10765 
    108     anton = lazy(anton, int, str) 
    109  
    110     print type(anton(5,6)) 
    111     print anton(5,6) 
    112     print anton('anton','berta') 
    113     print type(force(anton(5,6))) 
    114     print forced(1) 
    115     print forced(anton(5,6)) 
    116     print forced(force(anton(5,6))) 
    117  
  • django/branches/i18n/django/utils/translation.py

    r884 r904  
    248248        lang_code = request.GET.get('django_language', None) or request.POST.get('django_language', None) 
    249249        if lang_code is not None: 
     250            if lang_code == 'en' or lang_code.startswith('en_'): 
     251                return lang_code 
    250252            lang = gettext_module.find('django', globalpath, [lang_code]) 
    251253            if lang is not None: 
     
    259261        lang_code = request.session.get('django_language', None) 
    260262        if lang_code is not None: 
     263            if lang_code == 'en' or lang_code.startswith('en_'): 
     264                return lang_code 
    261265            lang = gettext_module.find('django', globalpath, [lang_code]) 
    262266            if lang is not None: 
     
    265269    lang_code = request.COOKIES.get('django_language', None) 
    266270    if lang_code is not None: 
     271        if lang_code == 'en' or lang_code.startswith('en_'): 
     272            return lang_code 
    267273        lang = gettext_module.find('django', globalpath, [lang_code]) 
    268274        if lang is not None: