web/lib/django/utils/translation/__init__.py
changeset 38 77b6da96e6f1
equal deleted inserted replaced
37:8d941af65caf 38:77b6da96e6f1
       
     1 """
       
     2 Internationalization support.
       
     3 """
       
     4 from django.utils.encoding import force_unicode
       
     5 from django.utils.functional import lazy, curry
       
     6 
       
     7 
       
     8 __all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext',
       
     9         'ngettext_lazy', 'string_concat', 'activate', 'deactivate',
       
    10         'get_language', 'get_language_bidi', 'get_date_formats',
       
    11         'get_partial_date_formats', 'check_for_language', 'to_locale',
       
    12         'get_language_from_request', 'templatize', 'ugettext', 'ugettext_lazy',
       
    13         'ungettext', 'deactivate_all']
       
    14 
       
    15 # Here be dragons, so a short explanation of the logic won't hurt:
       
    16 # We are trying to solve two problems: (1) access settings, in particular
       
    17 # settings.USE_I18N, as late as possible, so that modules can be imported
       
    18 # without having to first configure Django, and (2) if some other code creates
       
    19 # a reference to one of these functions, don't break that reference when we
       
    20 # replace the functions with their real counterparts (once we do access the
       
    21 # settings).
       
    22 
       
    23 def delayed_loader(real_name, *args, **kwargs):
       
    24     """
       
    25     Call the real, underlying function.  We have a level of indirection here so
       
    26     that modules can use the translation bits without actually requiring
       
    27     Django's settings bits to be configured before import.
       
    28     """
       
    29     from django.conf import settings
       
    30     if settings.USE_I18N:
       
    31         from django.utils.translation import trans_real as trans
       
    32     else:
       
    33         from django.utils.translation import trans_null as trans
       
    34 
       
    35     # Make the originally requested function call on the way out the door.
       
    36     return getattr(trans, real_name)(*args, **kwargs)
       
    37 
       
    38 g = globals()
       
    39 for name in __all__:
       
    40     g['real_%s' % name] = curry(delayed_loader, name)
       
    41 del g, delayed_loader
       
    42 
       
    43 def gettext_noop(message):
       
    44     return real_gettext_noop(message)
       
    45 
       
    46 ugettext_noop = gettext_noop
       
    47 
       
    48 def gettext(message):
       
    49     return real_gettext(message)
       
    50 
       
    51 def ngettext(singular, plural, number):
       
    52     return real_ngettext(singular, plural, number)
       
    53 
       
    54 def ugettext(message):
       
    55     return real_ugettext(message)
       
    56 
       
    57 def ungettext(singular, plural, number):
       
    58     return real_ungettext(singular, plural, number)
       
    59 
       
    60 ngettext_lazy = lazy(ngettext, str)
       
    61 gettext_lazy = lazy(gettext, str)
       
    62 ungettext_lazy = lazy(ungettext, unicode)
       
    63 ugettext_lazy = lazy(ugettext, unicode)
       
    64 
       
    65 def activate(language):
       
    66     return real_activate(language)
       
    67 
       
    68 def deactivate():
       
    69     return real_deactivate()
       
    70 
       
    71 def get_language():
       
    72     return real_get_language()
       
    73 
       
    74 def get_language_bidi():
       
    75     return real_get_language_bidi()
       
    76 
       
    77 def get_date_formats():
       
    78     return real_get_date_formats()
       
    79 
       
    80 def get_partial_date_formats():
       
    81     return real_get_partial_date_formats()
       
    82 
       
    83 def check_for_language(lang_code):
       
    84     return real_check_for_language(lang_code)
       
    85 
       
    86 def to_locale(language):
       
    87     return real_to_locale(language)
       
    88 
       
    89 def get_language_from_request(request):
       
    90     return real_get_language_from_request(request)
       
    91 
       
    92 def templatize(src):
       
    93     return real_templatize(src)
       
    94 
       
    95 def deactivate_all():
       
    96     return real_deactivate_all()
       
    97 
       
    98 def _string_concat(*strings):
       
    99     """
       
   100     Lazy variant of string concatenation, needed for translations that are
       
   101     constructed from multiple parts.
       
   102     """
       
   103     return u''.join([force_unicode(s) for s in strings])
       
   104 string_concat = lazy(_string_concat, unicode)