Changeset 3261

Show
Ignore:
Timestamp:
07/18/07 17:13:33 (1 year ago)
Author:
ed
Message:

Re-wrote dLocalize to simplify the way it works. I'm actually able to get newly displayed strings to update, although things such as the menu bar and existing forms will not be affected.

Added the setLanguage() method to dApp. This will allow apps to change the language that their application's localized strings appear in.

Updated dApp to work with the new interactive web update settings.

Files:

Legend:

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

    r3237 r3261  
    355355        return ret 
    356356 
    357          
    358     def _checkForUpdates(self): 
     357     
     358    def checkForUpdates(self, evt=None): 
     359        """Public interface to the web updates mechanism.""" 
     360        return self.uiApp.checkForUpdates(force=True) 
     361         
     362         
     363    def _checkForUpdates(self, force=False): 
    359364        ret = False 
    360365        prf = self._frameworkPrefs 
    361366        val = prf.getValue 
     367        runCheck = False 
    362368        now = datetime.datetime.now() 
    363         autoUpdate = val("auto_update") 
    364         if autoUpdate: 
    365             checkInterval = val("update_interval") 
    366             if checkInterval is None: 
    367                 # Default to one day 
    368                 checkInterval = 24 * 60 
    369             mins = datetime.timedelta(minutes=checkInterval) 
    370             lastCheck = val("last_check") 
    371             if lastCheck is None: 
    372                 lastCheck = datetime.datetime(1900, 1, 1) 
    373             if now > (lastCheck + mins): 
    374                 # See if there is a later version 
    375                 url = "http://dabodev.com/frameworkVersions/latest" 
    376                 try: 
    377                     vers = int(urllib.urlopen(url).read()) 
    378                 except: 
    379                     vers = -1 
    380                 localVers = self._currentUpdateVersion() 
    381                 ret = localVers < vers 
    382                  
    383                 print "LOCAL V", localVers 
    384                 print "WEB VER", vers 
     369        if not force: 
     370            webUpdate = val("web_update") 
     371            if webUpdate: 
     372                checkInterval = val("update_interval") 
     373                if checkInterval is None: 
     374                    # Default to one day 
     375                    checkInterval = 24 * 60 
     376                mins = datetime.timedelta(minutes=checkInterval) 
     377                lastCheck = val("last_check") 
     378                if lastCheck is None: 
     379                    lastCheck = datetime.datetime(1900, 1, 1) 
     380                runCheck = (now > (lastCheck + mins)) 
     381        if runCheck: 
     382            # See if there is a later version 
     383            url = "http://dabodev.com/frameworkVersions/latest" 
     384            try: 
     385                vers = int(urllib.urlopen(url).read()) 
     386            except: 
     387                vers = -1 
     388            localVers = self._currentUpdateVersion() 
     389            ret = localVers < vers 
    385390        prf.setValue("last_check", now) 
    386391        return ret 
     
    413418                except StandardError, e: 
    414419                    dabo.errorLog.write(_("Cannot update file: '%s'. Error: %s") % (fpth, e)) 
    415          
    416  
    417     def _setAutoUpdate(self, auto, interval=None): 
    418         """Sets the auto-update settings for the entire framework. If set to True, the  
     420        urllib.urlcleanup() 
     421         
     422 
     423    def _setWebUpdate(self, auto, interval=None): 
     424        """Sets the web update settings for the entire framework. If set to True, the  
    419425        interval is expected to be in minutes between checks. 
    420426        """ 
    421427        prf = self._frameworkPrefs 
    422         prf.setValue("auto_update", auto) 
     428        prf.setValue("web_update", auto) 
    423429        if auto: 
    424430            if interval is None: 
     
    426432                interval = 0 
    427433            prf.setValue("update_interval", interval) 
     434     
     435     
     436    def getWebUpdateInfo(self): 
     437        """Returns a 2-tuple that reflects the current settings for web updates. 
     438        The first position is a boolean that reflects whether auto-checking is turned 
     439        on; the second is the update frequency in minutes. 
     440        """ 
     441        return (self._frameworkPrefs.web_update, self._frameworkPrefs.update_interval) 
    428442         
    429443         
     
    682696                self.dbConnectionDefs[k] = ci 
    683697                self.dbConnectionNameToFiles[k] = connFile 
    684      
     698 
     699 
     700    def setLanguage(self, lang, charset=None): 
     701        """Allows you to change the language used for localization. If the language 
     702        passed is not one for which there is a translation file, an IOError exception 
     703        will be raised. You may optionally pass a character set to use. 
     704        """ 
     705        dabo.dLocalize.setLanguage(lang, charset) 
     706 
     707         
    685708    def showCommandWindow(self, context=None): 
    686709        """Shows a command window with a full Python interpreter. 
     
    764787         
    765788 
     789    def onWebUpdatePrefs(self, evt): 
     790        self.uiApp.onWebUpdatePrefs(evt) 
     791         
     792         
    766793    def onHelpAbout(self, evt): 
    767794        about = self.AboutFormClass 
  • trunk/dabo/dLocalize.py

    r3054 r3261  
    11# -*- coding: utf-8 -*- 
    2 import gettext, os 
     2import gettext 
     3import locale 
     4import os 
     5import re 
    36import dabo 
    4 from dabo.__version__ import version 
    57 
    6 __domain = "dabo" 
    7 __localedir = "locale" 
    88 
    9 def __translation(domain, version=None, dirs=[]): 
    10     """ Try to find """ 
    11     dirs = [os.path.join(d, __localedir) for d in dirs] 
    12     dirs.append(None) # tell to search in system dirs also 
    13     domains = [domain
    14     version and domains.insert(0, "-".join([domain, version])) 
     9def getLanguages(): 
     10    # Probably need to add more 
     11    langs = gettext.find("dabo", daboLocaleDir, languages=_trans.keys(), all=True) 
     12    langPat = re.compile(".+/locale/([a-z]{2})/LC_MESSAGES.+") 
     13    ret = [langPat.sub(r"\1", lang) for lang in langs
     14    return ret 
    1515 
    16     for domain in domains: 
    17         for dir in dirs: 
     16 
     17def setLanguage(lang=None, charset=None): 
     18    global defLang, defCharset, _trans 
     19    if charset is None: 
     20        charset = defCharset 
     21    if lang is None: 
     22        lang = defLang 
     23    else: 
     24        lang = lang.lower() 
     25        # It might be the full name instead of the two-letter abbreviation 
     26        if lang not in getLanguages(): 
    1827            try: 
    19                 return gettext.translation(domain, localedir=dir) 
    20             except IOError: 
     28                lang = {"english": "en",  
     29                        "spanish": "es", "espanol": "es", "español": "es", 
     30                        "french": "fr", "francais": "fr", "français": "fr",  
     31                        "portuguese": "pt", "portuguése": "pt", 
     32                        "russian": "ru"}[lang] 
     33            except KeyError: 
    2134                pass 
    22     return None 
     35    if not lang in getLanguages(): 
     36        raise IOError, "Invalid language '%s'" % lang 
     37     
     38    if _trans.get(lang) is None: 
     39        _trans[lang] = gettext.translation("dabo", daboLocaleDir,  
     40                languages=[lang], codeset=charset) 
     41    defLang = lang 
     42     
     43     
     44def _(s): 
     45    global defLang, _trans 
     46    return _trans[defLang].gettext(s) 
     47     
    2348 
    24 # set default Dabo translation. File is searched under cwd()/locale/$LANG/LC_MESSAGES 
    25 # and in system default places. 
    26 __trans = __translation(__domain, version["version"], ["."]) or gettext.NullTranslations() 
    27  
    28 __app_initialized = False 
    29  
    30 def __add_apptrans(): 
    31     global __trans 
    32     try: 
    33         app = dabo.dAppRef 
    34     except AttributeError: 
    35         app = None 
    36     if app: 
    37         appname, appver = [app.getAppInfo(k) for k in ("appName", "appVersion")] 
    38         if appname: 
    39             appname = appname.lower() 
    40             apptrans = __translation(appname, appver, [app.HomeDirectory]) 
    41             if apptrans: 
    42                 apptrans.add_fallback(__trans) 
    43                 __trans = apptrans 
    44                 __app_initialized = True 
    45  
    46 def _(s): 
    47     """ Default localization service. Translation is based on default 
    48     gettext interface. Translation is returned in Unicode. 
    49  
    50     Messages are searched first in application translations and in Dabo ones after. 
    51      
    52     Translation files are searched in 
    53     application.HomeDirectory/locale and system default locale 
    54     directories accoding to gettext conventions(files must be 
    55     placed under ${LANG}/LC_MESSAGES subdirectory. File basename must 
    56     be appName or appName-appVersion in lower case. 
    57      
    58     Default appName is "dabo". 
    59     """ 
    60     # application initialized, try to install messages 
    61     if not __app_initialized: 
    62         __add_apptrans() 
    63      
    64     app = dabo.dAppRef 
    65     if isinstance(s, str) and app is not None: 
    66         ss = app.str2Unicode(s) 
    67     else: 
    68         ss = s 
    69  
    70     return __trans.ugettext(ss) 
    71  
    72      
    7349def n_(s): 
    7450    """ Use it if you want to tell translation service about string 
    7551    but don't want to translate it inplace. 
    7652    """ 
    77     return s 
     53    global defLang 
     54    return _trans[defLang].gettext(s) 
    7855 
     56 
     57daboLocaleDir = os.path.join(os.path.split(dabo.__file__)[0], "locale") 
     58_trans = {"en": None, "fr": None, "es": None, "pt": None, "ru": None} 
     59defLang, defCharset = locale.getlocale() 
     60if defLang is None: 
     61    defLang = "en" 
     62else: 
     63    defLang = defLang[:2] 
     64if defCharset is None: 
     65    defCharset = "ISO8859-1" 
     66setLanguage()