Changeset 3287

Show
Ignore:
Timestamp:
07/27/2007 03:21:05 PM (1 year ago)
Author:
ed
Message:

One functional change: added a check for appShortName being None, which can happen early in the startup process.

The rest are all cosmetic. I changed the under_score_separated_names to interCappedNames, and got rid of the leading double underscore, which isn't needed at the module level (e.g., you could write dabo.dLocalize.daboTranslate, and it would be returned without any mangling issues).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dabo/dLocalize.py

    r3286 r3287  
    1212import dabo 
    1313 
    14 __app_initialized = False 
    15 __app_has_locale = False 
    16 __dabo_trans = None 
    17 __app_trans = None 
    18 _localedir = "locale" 
    19 _frozenlocaledir = "dabo.locale" 
     14_appInitialized = False 
     15_appHasLocale = False 
     16_daboTranslate = None 
     17_appTranslate = None 
     18_localeDir = "locale" 
     19_frozenLocaleDir = "dabo.locale" 
    2020 
    21 language_aliases = {"english": "en", 
     21languageAliases = {"english": "en", 
    2222        "spanish": "es", "espanol": "es", "español": "es", 
    2323        "french": "fr", "francais": "fr", "français": "fr",  
     
    4343    "daboapplication.mo".  
    4444    """ 
    45     global defLang, __app_initialized, __app_has_locale 
     45    global defLang, _appInitialized, _appHasLocale 
    4646     
    47     if not __app_initialized: 
     47    if not _appInitialized: 
    4848        try: 
    4949            app = dabo.dAppRef 
     
    5252             
    5353        if app: 
    54             __app_initialized = True 
     54            _appInitialized = True 
    5555            # If appShortName not changed in user app, defaults to "daboapplication" 
    56             __app_has_locale = setLanguage(domain=app.getAppInfo("appShortName").lower(), 
    57                     localedir=os.path.join(app.HomeDirectory, _localedir)) 
     56            shortName = app.getAppInfo("appShortName") 
     57            if shortName is not None: 
     58                _appHasLocale = setLanguage(domain=shortName.lower(), 
     59                        localedir=os.path.join(app.HomeDirectory, _localeDir)) 
    5860 
    5961 
    6062    # Always return Unicode strings 
    61     if __app_initialized and __app_has_locale: 
     63    if _appInitialized and _appHasLocale: 
    6264        # Use app's localization, with Dabo's as a fallback: 
    63         return __app_trans.ugettext(s) 
     65        return _appTranslate.ugettext(s) 
    6466    else: 
    6567        # App's localization is not in place; use only Dabo's: 
    66         return __dabo_trans.ugettext(s) 
     68        return _daboTranslate.ugettext(s) 
     69         
    6770 
    6871def n_(s): 
     
    7780    #      Do we use it even? 
    7881 
     82 
    7983def setLanguage(lang=None, charset=None, domain="dabo", localedir=None): 
    8084    """Use it if you want to switch to another localizations than your default. 
    8185    You should call it twice - once for dabo framework, and once for app. 
    8286    """ 
    83     global defLang, defCharset, __dabo_trans, __app_trans 
     87    global defLang, defCharset, _daboTranslate, _appTranslate 
    8488     
    8589    #TODO: we should search system localizations directory as well 
    8690 
    8791    if localedir is None: 
    88         localedir = os.path.join(os.path.split(dabo.__file__)[0], _localedir) 
     92        localedir = os.path.join(os.path.split(dabo.__file__)[0], _localeDir) 
    8993        if not os.path.exists(localedir): 
    9094            # Frozen app? 
     
    96100                    break 
    97101            if domain == "dabo": 
    98                 frozenLocaleDir = _frozenlocaledir 
     102                frozenLocaleDir = _frozenLocaleDir 
    99103            else: 
    100                 frozenLocaleDir = _localedir 
     104                frozenLocaleDir = _localeDir 
    101105            localedir = os.path.join(startupDir, frozenLocaleDir) 
    102106 
     
    109113        lang = lang.lower() 
    110114        # It might be the full name instead of the two-letter abbreviation: 
    111         lang = language_aliases.get(lang, lang) 
     115        lang = languageAliases.get(lang, lang) 
    112116         
    113117    localefile = gettext.find(domain, localedir, languages=[lang], all=True) 
     
    116120        if not localefile: 
    117121            raise IOError, "No translation files found for Dabo. Looked in %s." % localedir 
    118         __dabo_trans = gettext.translation(domain, localedir, languages=[lang], codeset=charset) 
     122        _daboTranslate = gettext.translation(domain, localedir, languages=[lang], codeset=charset) 
    119123        defLang = lang 
    120124    else: 
    121125        if localefile: 
    122             __app_trans = gettext.translation(domain, localedir, languages=[lang], codeset=charset) 
    123             if __app_trans
    124                 __app_trans.add_fallback(__dabo_trans
    125             return bool(__app_trans
     126            _appTranslate = gettext.translation(domain, localedir, languages=[lang], codeset=charset) 
     127            if _appTranslate
     128                _appTranslate.add_fallback(_daboTranslate
     129            return bool(_appTranslate
    126130        return False 
    127131         
     
    145149    sys.setdefaultencoding(locale.getdefaultlocale()[1]) 
    146150     
    147     print 'user locale is ', locale.getdefaultlocale() 
    148     print 'framework locale is ', defLang, defCharset 
     151    print "user locale is ", locale.getdefaultlocale() 
     152    print "framework locale is ", defLang, defCharset 
    149153    print _("Framework localization test")