Django

Code

Changeset 5055

Show
Ignore:
Timestamp:
04/21/07 09:16:21 (2 years ago)
Author:
mtredinnick
Message:

unicode: Added ugettext(), ungettext() and *_lazy() versions of same.

Files:

Legend:

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

    r4907 r5055  
    88        'get_language', 'get_language_bidi', 'get_date_formats', 
    99        'get_partial_date_formats', 'check_for_language', 'to_locale', 
    10         'get_language_from_request', 'install', 'templatize'] 
     10        'get_language_from_request', 'install', 'templatize', 'ugettext', 
     11        'ungettext'] 
    1112 
    1213# Here be dragons, so a short explanation of the logic won't hurt: 
     
    4950    return real_gettext_noop(message) 
    5051 
     52ugettext_noop = gettext_noop 
     53 
    5154def gettext(message): 
    5255    return real_gettext(message) 
    53  
    5456 
    5557def ngettext(singular, plural, number): 
    5658    return real_ngettext(singular, plural, number) 
    5759 
     60def ugettext(message): 
     61    return real_ugettext(message) 
     62 
     63def ungettext(singular, plural, number): 
     64    return real_ungettext(singular, plural, number) 
     65 
    5866def string_concat(*strings): 
    5967    return real_string_concat(*strings) 
    6068 
    61 ngettext_lazy = lazy(ngettext, str, unicode) 
    62 gettext_lazy = lazy(gettext, str, unicode) 
     69ngettext_lazy = lazy(ngettext, str) 
     70gettext_lazy = lazy(gettext, str) 
     71ungettext_lazy = lazy(ungettext, unicode) 
     72ugettext_lazy = lazy(ugettext, unicode) 
    6373string_concat = lazy(string_concat, str, unicode) 
    6474 
  • django/branches/unicode/django/utils/translation/trans_null.py

    r4932 r5055  
    44 
    55from django.conf import settings 
     6from django.utils.encoding import smart_unicode 
    67 
    78def ngettext(singular, plural, number): 
     
    910    return plural 
    1011ngettext_lazy = ngettext 
     12 
     13def ungettext(singular, plural, number): 
     14    return smart_unicode(ngettext(singular, plural, number)) 
    1115 
    1216string_concat = lambda *strings: ''.join([str(el) for el in strings]) 
     
    3135    return TECHNICAL_ID_MAP.get(message, message) 
    3236 
     37def ugettext(message): 
     38    return smart_unicode(gettext(message)) 
     39 
    3340gettext_noop = gettext_lazy = _ = gettext 
    3441 
  • django/branches/unicode/django/utils/translation/trans_real.py

    r4905 r5055  
    44import gettext as gettext_module 
    55from cStringIO import StringIO 
     6from django.utils.encoding import smart_str, smart_unicode 
    67 
    78try: 
     
    5859        # identical with the translation file charset. 
    5960        try: 
    60             self.set_output_charset(settings.DEFAULT_CHARSET
     61            self.set_output_charset('utf-8'
    6162        except AttributeError: 
    6263            pass 
    63         self.django_output_charset = settings.DEFAULT_CHARSET 
     64        self.django_output_charset = 'utf-8' 
    6465        self.__language = '??' 
    6566 
     
    239240    return _default 
    240241 
     242def do_translate(message, translation_function): 
     243    """ 
     244    Translate 'message' using the given 'translation_function' name -- which 
     245    will be either gettext or ugettext. 
     246    """ 
     247    global _default, _active 
     248    t = _active.get(currentThread(), None) 
     249    if t is not None: 
     250        return getattr(t, translation_function)(message) 
     251    if _default is None: 
     252        from django.conf import settings 
     253        _default = translation(settings.LANGUAGE_CODE) 
     254    return getattr(_default, translation_function)(message) 
     255 
    241256def gettext(message): 
    242257    """ 
     
    246261    message will be run through the default translation object. 
    247262    """ 
     263    return do_translate(message, 'gettext') 
     264 
     265def ugettext(message): 
     266    return do_translate(message, 'ugettext') 
     267 
     268def gettext_noop(message): 
     269    """ 
     270    Marks strings for translation but doesn't translate them now. This can be 
     271    used to store strings in global variables that should stay in the base 
     272    language (because they might be used externally) and will be translated 
     273    later. 
     274    """ 
     275    return message 
     276 
     277def do_ntranslate(singular, plural, number, translation_function): 
    248278    global _default, _active 
     279 
    249280    t = _active.get(currentThread(), None) 
    250281    if t is not None: 
    251         return t.gettext(message
     282        return getattr(t, translation_function)(singular, plural, number
    252283    if _default is None: 
    253284        from django.conf import settings 
    254285        _default = translation(settings.LANGUAGE_CODE) 
    255     return _default.gettext(message) 
    256  
    257 def gettext_noop(message): 
    258     """ 
    259     Marks strings for translation but doesn't translate them now. This can be 
    260     used to store strings in global variables that should stay in the base 
    261     language (because they might be used externally) and will be translated later. 
    262     """ 
    263     return message 
     286    return getattr(_default, translation_function)(singular, plural, number) 
    264287 
    265288def ngettext(singular, plural, number): 
    266289    """ 
    267     Returns the translation of either the singular or plural, based on the number. 
    268     """ 
    269     global _default, _active 
    270  
    271     t = _active.get(currentThread(), None) 
    272     if t is not None
    273         return t.ngettext(singular, plural, number) 
    274     if _default is None: 
    275         from django.conf import settings 
    276         _default = translation(settings.LANGUAGE_CODE) 
    277     return _default.ngettext(singular, plural, number
     290    Returns a UTF-8 bytestring of the translation of either the singular or 
     291    plural, based on the number. 
     292    """ 
     293    return do_ntranslate(singular, plural, number, 'ngettext') 
     294 
     295def ungettext(singular, plural, number)
     296    """ 
     297    Returns a unicode strings of the translation of either the singular or 
     298    plural, based on the number. 
     299    """ 
     300    return do_ntranslate(singular, plural, number, 'ungettext'
    278301 
    279302def check_for_language(lang_code): 
    280303    """ 
    281     Checks whether there is a global language file for the given language code. 
    282     This is used to decide whether a user-provided language is available. This is 
    283     only used for language codes from either the cookies or session. 
     304    Checks whether there is a global language file for the given language 
     305    code. This is used to decide whether a user-provided language is 
     306    available. This is only used for language codes from either the cookies or 
     307    session. 
    284308    """ 
    285309    from django.conf import settings 
     
    292316def get_language_from_request(request): 
    293317    """ 
    294     Analyzes the request to find what language the user wants the system to show. 
    295     Only languages listed in settings.LANGUAGES are taken into account. If the user 
    296     requests a sublanguage where we have a main language, we send out the main language. 
     318    Analyzes the request to find what language the user wants the system to 
     319    show. Only languages listed in settings.LANGUAGES are taken into account. 
     320    If the user requests a sublanguage where we have a main language, we send 
     321    out the main language. 
    297322    """ 
    298323    global _accepted