Changeset 3094

Show
Ignore:
Timestamp:
07/07/08 17:35:23 (5 years ago)
Author:
nopper
Message:

Adding localizer plugin for example

Location:
branch/UmitPlugins
Files:
12 added
2 modified

Legend:

Unmodified
Added
Removed
  • branch/UmitPlugins/umitPlugin/Atoms.py

    r3075 r3094  
    2323""" 
    2424 
     25class StringFile(str): 
     26    """ 
     27    Dummy class to avoid reimplementing of GNUTranslations 
     28    for file within a zip archive 
     29    """ 
     30 
     31    def read(self): 
     32        return self 
     33 
    2534class Singleton(object): 
    2635    """ 
  • branch/UmitPlugins/umitPlugin/Containers.py

    r3023 r3094  
    2424import gtk 
    2525 
    26 # Needs testing 
    27 from Parser import Parser 
    28  
    2926from fnmatch import fnmatch 
    3027from zipfile import ZipFile, BadZipfile 
    3128from xml.dom.minidom import parseString, getDOMImplementation 
    3229from tempfile import mkstemp 
     30 
     31# Needs testing 
     32from umitPlugin.Parser import Parser 
     33from umitPlugin.Atoms import StringFile 
    3334 
    3435# For setup functionality 
     
    168169        return self.path 
    169170 
     171    # Code ripped from gettext 
     172    def expand_lang(self, locale): 
     173        from locale import normalize 
     174        locale = normalize(locale) 
     175        COMPONENT_CODESET   = 1 << 0 
     176        COMPONENT_TERRITORY = 1 << 1 
     177        COMPONENT_MODIFIER  = 1 << 2 
     178        # split up the locale into its base components 
     179        mask = 0 
     180        pos = locale.find('@') 
     181        if pos >= 0: 
     182            modifier = locale[pos:] 
     183            locale = locale[:pos] 
     184            mask |= COMPONENT_MODIFIER 
     185        else: 
     186            modifier = '' 
     187        pos = locale.find('.') 
     188        if pos >= 0: 
     189            codeset = locale[pos:] 
     190            locale = locale[:pos] 
     191            mask |= COMPONENT_CODESET 
     192        else: 
     193            codeset = '' 
     194        pos = locale.find('_') 
     195        if pos >= 0: 
     196            territory = locale[pos:] 
     197            locale = locale[:pos] 
     198            mask |= COMPONENT_TERRITORY 
     199        else: 
     200            territory = '' 
     201        language = locale 
     202        ret = [] 
     203        for i in range(mask+1): 
     204            if not (i & ~mask):  # if all components for this combo exist ... 
     205                val = language 
     206                if i & COMPONENT_TERRITORY: val += territory 
     207                if i & COMPONENT_CODESET:   val += codeset 
     208                if i & COMPONENT_MODIFIER:  val += modifier 
     209                ret.append(val) 
     210        ret.reverse() 
     211        return ret 
     212 
     213    def bind_translation(self, mofile): 
     214        """ 
     215        @return a catalog on success or None 
     216        """ 
     217         
     218        # We foreach inside locale dir and find a proper dir 
     219         
     220        try: 
     221            import gettext 
     222            import locale 
     223            LC_ALL = locale.setlocale(locale.LC_ALL, '') 
     224        except locale.Error: 
     225            return None 
     226 
     227        LANG, ENC = locale.getdefaultlocale() 
     228 
     229        if ENC == None: 
     230            ENC = "utf8" 
     231        if LANG == None: 
     232            LANG = "en_US" 
     233         
     234        # FIXME: is the '/' os indipendent? 
     235        dir_lst = filter( \ 
     236            lambda x: x.startswith("locale/") and x.endswith("%s.mo" % mofile), \ 
     237            self.file.namelist() \ 
     238        ) 
     239        dir_lst.sort() 
     240 
     241        avaiable_langs = [] 
     242         
     243        for dirname in dir_lst: 
     244            t = dirname.split("/") 
     245 
     246            if len(t) < 3: 
     247                continue 
     248             
     249            avaiable_langs.append(t[-2]) 
     250 
     251        request = self.expand_lang(".".join([LANG, ENC])) 
     252 
     253        for req in request: 
     254            if req in avaiable_langs: 
     255                # Ok getted! Lucky day :) 
     256 
     257                return gettext.GNUTranslations(StringFile( \ 
     258                    self.file.read("locale/%s/%s.mo" % (req, mofile)) \ 
     259                )) 
     260             
     261        return None 
     262 
    170263class PluginWriter(object): 
    171264    def __init__(self, **fields): 
     
    185278            'bin'  : '*', 
    186279            'data' : '*', 
    187             'lib'  : '*' 
     280            'lib'  : '*', 
     281            'locale' : '*' 
    188282        } 
    189283