Django

Code

Changeset 4905

Show
Ignore:
Timestamp:
04/02/07 05:58:43 (2 years ago)
Author:
mtredinnick
Message:

Delayed the reading of settings.USE_I18N until the first use of the i18n
functions. This solves a few import problems we are seeing. Fixed #3687. Refs #2920.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/utils/translation/__init__.py

    r4265 r4905  
    1 from django.conf import settings 
     1""" 
     2Internationalization support. 
     3""" 
     4from django.utils.functional import lazy 
    25 
    3 if settings.USE_I18N: 
    4     from trans_real import * 
    5 else: 
    6     from trans_null import * 
     6__all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext', 
     7        'ngettext_lazy', 'string_concat', 'activate', 'deactivate', 
     8        'get_language', 'get_language_bidi', 'get_date_formats', 
     9        'get_partial_date_formats', 'check_for_language', 'to_locale', 
     10        'get_language_from_request', 'install'] 
    711 
    8 del settings 
     12# Here be dragons, so a short explanation of the logic won't hurt: 
     13# We are trying to solve two problems: (1) access settings, in particular 
     14# settings.USE_I18N, as late as possible, so that modules can be imported 
     15# without having to first configure Django, and (2) if some other code creates 
     16# a reference to one of these functions, don't break that reference when we 
     17# replace the functions with their real counterparts (once we do access the 
     18# settings). 
     19 
     20def delayed_loader(*args, **kwargs): 
     21    """ 
     22    Replace each real_* function with the corresponding function from either 
     23    trans_real or trans_null (e.g. real_gettext is replaced with 
     24    trans_real.gettext or trans_null.gettext). This function is run once, the 
     25    first time any i18n method is called. It replaces all the i18n methods at 
     26    once at that time. 
     27    """ 
     28    import traceback 
     29    from django.conf import settings 
     30    if settings.USE_I18N: 
     31        import trans_real as trans 
     32    else: 
     33        import trans_null as trans 
     34    caller = traceback.extract_stack(limit=2)[0][2] 
     35    g = globals() 
     36    for name in __all__: 
     37        if hasattr(trans, name): 
     38            g['real_%s' % name] = getattr(trans, name) 
     39 
     40    # Make the originally requested function call on the way out the door. 
     41    return g[caller](*args, **kwargs) 
     42 
     43g = globals() 
     44for name in __all__: 
     45    g['real_%s' % name] = delayed_loader 
     46del g, delayed_loader 
     47 
     48def gettext_noop(message): 
     49    return real_gettext_noop(message) 
     50 
     51def gettext(message): 
     52    return real_gettext(message) 
     53 
     54 
     55def ngettext(singular, plural, number): 
     56    return real_ngettext(singular, plural, number) 
     57 
     58def string_concat(*strings): 
     59    return real_string_concat(*strings) 
     60 
     61ngettext_lazy = lazy(ngettext, str, unicode) 
     62gettext_lazy = lazy(gettext, str, unicode) 
     63string_concat = lazy(string_concat, str, unicode) 
     64 
     65def activate(language): 
     66    return real_activate(language) 
     67 
     68def deactivate(): 
     69    return real_deactivate() 
     70 
     71def get_language(): 
     72    return real_get_language() 
     73 
     74def get_language_bidi(): 
     75    return real_get_language_bidi() 
     76 
     77def get_date_formats(): 
     78    return real_get_date_formats() 
     79 
     80def get_partial_date_formats(): 
     81    return real_get_partial_date_formats() 
     82 
     83def check_for_language(lang_code): 
     84    return real_check_for_language(lang_code) 
     85 
     86def to_locale(language): 
     87    return real_to_locale(language) 
     88 
     89def get_language_from_request(request): 
     90    return real_get_language_from_request(request) 
     91 
     92def install(): 
     93    return real_install() 
     94 
  • django/trunk/django/utils/translation/trans_real.py

    r4486 r4905  
    44import gettext as gettext_module 
    55from cStringIO import StringIO 
    6 from django.utils.functional import lazy 
    76 
    87try: 
     
    277276        _default = translation(settings.LANGUAGE_CODE) 
    278277    return _default.ngettext(singular, plural, number) 
    279  
    280 gettext_lazy = lazy(gettext, str) 
    281 ngettext_lazy = lazy(ngettext, str) 
    282278 
    283279def check_for_language(lang_code): 
     
    494490    return ''.join([str(el) for el in strings]) 
    495491 
    496 string_concat = lazy(string_concat, str)