Changeset 5719

Show
Ignore:
Timestamp:
11/18/08 17:25:37 (2 months ago)
Author:
faide
Message:

Sanitizing sogettext usage and preparing to create sagettext. now configurable from the config file instead of only through code...

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/turbogears/i18n/sogettext/__init__.py

    r5718 r5719  
    55from turbogears.i18n.utils import get_locale 
    66 
    7 try: 
    8     from sqlobject import SQLObjectNotFound 
    9 except ImportError: 
    10     pass 
     7from sqlobject import SQLObjectNotFound 
     8 
    119from gettext import translation 
    1210import codecs 
     
    120118            for message in TG_Message.selectBy(domain=domain, locale=locale): 
    121119 
    122                 if message.name == "":continue # descriptive text 
     120                if message.name == "": 
     121                    continue # descriptive text 
    123122 
    124123                f.write(u""" 
  • branches/1.1/turbogears/i18n/tests/test_so_gettext.py

    r1507 r5719  
    1010 
    1111def setup_module():  
    12     turbogears.config.update({"i18n.gettext":sogettext.so_gettext}) 
     12    turbogears.config.update({"i18n.gettext": "so_gettext"}) 
    1313    basic_setup_module() 
    1414 
    1515def teardown_module():  
    16     turbogears.config.update({"i18n.gettext":tg_gettext}) 
     16    turbogears.config.update({"i18n.gettext": "tg_gettext"}) 
    1717 
    1818def test_so_gettext(): 
  • branches/1.1/turbogears/i18n/tg_gettext.py

    r4362 r5719  
    77from turbogears.i18n.utils import get_locale 
    88from turbojson.jsonify import jsonify 
     9 
     10import logging 
     11log = logging.getLogger("turbogears.i18n") 
    912 
    1013_catalogs = {} 
     
    5659 
    5760    """ 
    58     gettext_func = config.get("i18n.gettext", tg_gettext) 
    59     return gettext_func(key, locale, domain) 
     61    gettext_func_name = config.get("i18n.gettext", "tg_gettext") 
     62    gettext_func = None 
     63 
     64    if gettext_func_name == "tg_gettext": 
     65        gettext_func = tg_gettext 
     66 
     67    elif gettext_func_name == "so_gettext": 
     68        try: 
     69            from turbogears.i18n.sogettext import so_gettext 
     70            gettext_func = so_gettext 
     71 
     72        except ImportError, e: 
     73            log.error("Could not load sogettext: %s" % e) 
     74            log.error("Translation disabled") 
     75             
     76 
     77    elif gettext_func_name == "sa_gettext": 
     78        try: 
     79            from turbogears.i18n.sagettext import sa_gettext 
     80            gettext_func = so_gettext 
     81 
     82        except ImportError, e: 
     83            log.error("Could not load sogettext: %s" % e) 
     84            log.error("Translation disabled") 
     85     
     86    else: 
     87        log.error("Invalid i18n.gettext option: %s" % gettext_func_name) 
     88        log.error("Translation disabled") 
     89 
     90    if gettext_func is None: 
     91        # gettext function could not be loaded? Just avoid 
     92        # to translate and return original data 
     93        return key 
     94 
     95    else: 
     96        # gettext function loaded properly, use it :) 
     97        return gettext_func(key, locale, domain) 
    6098 
    6199def tg_gettext(key, locale=None, domain=None): 
     
    71109    if locale is None: 
    72110        locale = get_locale() 
     111 
    73112    if not is_locale_supported(locale): 
    74113        locale = locale[:2] 
     114 
    75115    if key == '': 
    76116        return '' # special case 
     117 
    77118    try: 
    78119        return get_catalog(locale, domain).ugettext(key) 
     120 
    79121    except KeyError: 
    80122        return key 
     123 
    81124    except IOError: 
    82125        return key 
     
    95138    if num==1: 
    96139        return plain_gettext(key1, locale) 
     140 
    97141    else: 
    98142        return plain_gettext(key2, locale) 
     
    135179        lazystr = lazystring(func, *args, **kw) 
    136180        return lazystr 
     181 
    137182    return newfunc 
    138183 
     
    156201    if request_available(): 
    157202        return plain_gettext(key, locale, domain) 
     203 
    158204    else: 
    159205        return lazy_gettext(key, locale, domain)