home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / dist-packages / LanguageSelector / LocaleInfo.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  6.4 KB  |  190 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import string
  5. import re
  6. import subprocess
  7. import gettext
  8. import os.path as os
  9. from gettext import gettext as _
  10. from xml.etree.ElementTree import ElementTree
  11.  
  12. class LocaleInfo(object):
  13.     ''' class with handy functions to parse the locale information '''
  14.     environments = [
  15.         '/etc/default/locale',
  16.         '/etc/environment']
  17.     
  18.     def __init__(self, languagelist_file):
  19.         self._lang = { }
  20.         self._country = { }
  21.         self._languagelist = { }
  22.         et = ElementTree(file = '/usr/share/xml/iso-codes/iso_639.xml')
  23.         it = et.getiterator('iso_639_entry')
  24.         for elm in it:
  25.             lang = elm.attrib['name']
  26.             if elm.attrib.has_key('iso_639_1_code'):
  27.                 code = elm.attrib['iso_639_1_code']
  28.             else:
  29.                 code = elm.attrib['iso_639_2T_code']
  30.             self._lang[code] = lang
  31.         
  32.         et = ElementTree(file = '/usr/share/xml/iso-codes/iso_639_3.xml')
  33.         it = et.getiterator('iso_639_3_entry')
  34.         for elm in it:
  35.             lang = elm.attrib['name']
  36.             code = elm.attrib['id']
  37.             if not self._lang.has_key(code):
  38.                 self._lang[code] = lang
  39.                 continue
  40.         
  41.         et = ElementTree(file = '/usr/share/xml/iso-codes/iso_3166.xml')
  42.         it = et.getiterator('iso_3166_entry')
  43.         for elm in it:
  44.             if elm.attrib.has_key('common_name'):
  45.                 descr = elm.attrib['common_name']
  46.             else:
  47.                 descr = elm.attrib['name']
  48.             if elm.attrib.has_key('alpha_2_code'):
  49.                 code = elm.attrib['alpha_2_code']
  50.             else:
  51.                 code = elm.attrib['alpha_3_code']
  52.             self._country[code] = descr
  53.         
  54.         for line in open(languagelist_file):
  55.             tmp = line.strip()
  56.             if tmp.startswith('#') or tmp == '':
  57.                 continue
  58.             
  59.             w = tmp.split(';')
  60.             localeenv = w[6].split(':')
  61.             self._languagelist[localeenv[0]] = '%s' % w[6]
  62.         
  63.  
  64.     
  65.     def lang(self, code):
  66.         ''' map language code to language name '''
  67.         if self._lang.has_key(code):
  68.             return self._lang[code]
  69.         return ''
  70.  
  71.     
  72.     def country(self, code):
  73.         ''' map country code to country name'''
  74.         if self._country.has_key(code):
  75.             return self._country[code]
  76.         return ''
  77.  
  78.     
  79.     def generated_locales(self):
  80.         ''' return a list of locales available on the system
  81.             (running locale -a) '''
  82.         locales = []
  83.         p = subprocess.Popen([
  84.             'locale',
  85.             '-a'], stdout = subprocess.PIPE)
  86.         for line in string.split(p.communicate()[0], '\n'):
  87.             tmp = line.strip()
  88.             if tmp.startswith('#') and tmp == '' and tmp == 'C' or tmp == 'POSIX':
  89.                 continue
  90.             
  91.             locale = string.split(tmp)[0]
  92.             locale = string.split(locale, '.')[0]
  93.             locale = string.split(locale, '@')[0]
  94.             if locale not in locales:
  95.                 locales.append(locale)
  96.                 continue
  97.         
  98.         return locales
  99.  
  100.     
  101.     def translate_language(self, lang):
  102.         '''return translated language'''
  103.         lang_name = gettext.dgettext('iso_639', self._lang[lang])
  104.         if lang_name == self._lang[lang]:
  105.             lang_name = gettext.dgettext('iso_639_3', self._lang[lang])
  106.         
  107.         return lang_name
  108.  
  109.     
  110.     def translate_locale(self, locale):
  111.         '''
  112.         return translated language and country of the given
  113.         locale into the given locale, e.g. 
  114.         (Deutsch, Deutschland) for de_DE
  115.         '''
  116.         (lang, country) = string.split(locale, '_')
  117.         current_language = None
  118.         if 'LANGUAGE' in os.environ:
  119.             current_language = os.environ['LANGUAGE']
  120.         
  121.         os.environ['LANGUAGE'] = locale
  122.         lang_name = self.translate_language(lang)
  123.         country_name = gettext.dgettext('iso_3166', self._country[country])
  124.         if current_language:
  125.             os.environ['LANGUAGE'] = current_language
  126.         
  127.         return (lang_name, country_name)
  128.  
  129.     
  130.     def translate(self, locale):
  131.         ''' get a locale code and output a human readable name '''
  132.         if '_' in locale:
  133.             (lang, country) = string.split(locale, '_')
  134.             (lang_name, country_name) = self.translate_locale(locale)
  135.             l = (filter,)((lambda k: k.startswith(lang + '_')), self.generated_locales())
  136.             if len(l) > 1:
  137.                 mycountry = self.country(country)
  138.                 if mycountry:
  139.                     return '%s (%s)' % (lang_name, country_name)
  140.                 return lang_name
  141.             len(l) > 1
  142.             return lang_name
  143.         '_' in locale
  144.         return self.translate_language(locale)
  145.  
  146.     
  147.     def makeEnvString(self, code):
  148.         ''' input is a language code, output a string that can be put in
  149.             the LANGUAGE enviroment variable.
  150.             E.g: en_DK -> en_DK:en
  151.         '''
  152.         if self._languagelist.has_key(code):
  153.             return self._languagelist[code]
  154.         if '_' not in code:
  155.             return code
  156.         (lang, region) = string.split(code, '_')
  157.         return '%s:%s' % (code, lang)
  158.  
  159.     
  160.     def getDefaultLanguage(self):
  161.         ''' returns the current default language (e.g. zh_CN) '''
  162.         for environment in self.environments:
  163.             if not os.path.exists(environment):
  164.                 continue
  165.             
  166.             for line in open(environment).readlines():
  167.                 line = line.strip()
  168.                 if line.startswith('LANGUAGE='):
  169.                     (key, value) = line.split('=')
  170.                     value = value.strip('"')
  171.                     return value.split(':')[0]
  172.             
  173.             for line in open(environment).readlines():
  174.                 match = re.match('LANG="([a-zA-Z_]*).*"$', line)
  175.                 if match:
  176.                     return match.group(1)
  177.             
  178.         
  179.  
  180.  
  181. if __name__ == '__main__':
  182.     datadir = '/usr/share/language-selector/'
  183.     li = LocaleInfo('%s/data/languages' % datadir, '%s/data/countries' % datadir, '%s/data/languagelist' % datadir)
  184.     print "default: '%s'" % li.getDefaultLanguage()
  185.     print li._lang
  186.     print li._country
  187.     print li._languagelist
  188.     print li.generated_locales()
  189.  
  190.