home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / LanguageSelector / LocaleInfo.py < prev    next >
Encoding:
Python Source  |  2006-08-29  |  5.0 KB  |  141 lines

  1. # LoclaeInfo.py (c) 2006 Canonical, released under the GPL
  2. #
  3. # a helper class to get locale info
  4.  
  5. import string
  6. import subprocess
  7.  
  8. from gettext import gettext as _
  9.  
  10. class LocaleInfo(object):
  11.     " class with handy functions to parse the locale information "
  12.     
  13.     environment = "/etc/environment"
  14.     def __init__(self, lang_file, country_file, languagelist_file):
  15.         # map language to human readable name, e.g.:
  16.         # "pt"->"Portugise", "de"->"German", "en"->"English"
  17.         self._lang = {}
  18.  
  19.         # map country to human readable name, e.g.:
  20.         # "BR"->"Brasil", "DE"->"Germany", "US"->"United States"
  21.         self._country = {}
  22.         
  23.         # map locale (language+country) to the LANGUAGE environment, e.g.:
  24.         # "pt_PT"->"pt_PT:pt:pt_BR:en_GB:en"
  25.         self._languagelist = {}
  26.         
  27.         # read lang file
  28.         self._langFile = lang_file
  29.         for line in open(lang_file):
  30.             tmp = line.strip()
  31.             if tmp.startswith("#") or tmp == "":
  32.                 continue
  33.             (code, lang) = tmp.split(":")
  34.             self._lang[code] = lang
  35.             
  36.         # read countries
  37.         for line in open(country_file):
  38.             tmp = line.strip()
  39.             if tmp.startswith("#") or tmp == "":
  40.                 continue
  41.             (un, code, long_code, descr, cap) = tmp.split(":")
  42.             self._country[code] = descr
  43.             
  44.         # read the languagelist
  45.         for line in open(languagelist_file):
  46.             tmp = line.strip()
  47.             if tmp.startswith("#") or tmp == "":
  48.                 continue
  49.             w = tmp.split(";")
  50.             # FIXME: the latest localechoosers "languagelist" does
  51.             # no longer have this field for most languages, so
  52.             # deal with it and don't set LANGUAGE then
  53.             # - the interessting question is what to do
  54.             # if LANGUAGE is already set and the new
  55.             localeenv = w[6].split(":")
  56.             #print localeenv
  57.             self._languagelist[localeenv[0]] = '%s' % w[6]
  58.  
  59.     def lang(self, code):
  60.         """ map language code to language name """
  61.         if self._lang.has_key(code):
  62.             return self._lang[code]
  63.         return ""
  64.  
  65.     def country(self, code):
  66.         """ map country code to country name"""
  67.         if self._country.has_key(code):
  68.             return self._country[code]
  69.         return ""
  70.  
  71.     def generated_locales(self):
  72.         """ return a list of locales avaialble on the system
  73.             (runing locale -a) """
  74.         locales = []
  75.         p = subprocess.Popen(["locale", "-a"], stdout=subprocess.PIPE)
  76.         for line in p.stdout.readlines():
  77.             tmp = line.strip()
  78.             if tmp.startswith("#") or tmp == "" or tmp == "C" or tmp == "POSIX":
  79.                 continue
  80.             # we are only interessted in the locale, not the codec
  81.             locale = string.split(tmp)[0]
  82.             locale = string.split(locale,".")[0]
  83.             locale = string.split(locale,"@")[0]
  84.             if not locale in locales:
  85.                 locales.append(locale)
  86.         #print locales
  87.         return locales
  88.  
  89.     def translate(self, locale):
  90.         """ get a locale code and output a human readable name """
  91.         if "_" in locale:
  92.             (lang, country) = string.split(locale, "_")
  93.             # get all locales for this language
  94.             l = filter(lambda k: k.startswith(lang+"_"), self.generated_locales())
  95.             # only show region/country if we have more than one 
  96.             if len(l) > 1:
  97.                 mycountry = self.country(country)
  98.                 if mycountry:
  99.                     return "%s (%s)" % (_(self.lang(lang)), _(mycountry))
  100.                 else:
  101.                     return "%s" % (_(self.lang(lang)))
  102.             else:
  103.                 return _(self.lang(lang))
  104.         return _(self.lang(locale))
  105.  
  106.     def makeEnvString(self, code):
  107.         """ input is a language code, output a string that can be put in
  108.             the LANGUAGE enviroment variable.
  109.             E.g: en_DK -> en_DK:en
  110.         """
  111.         # first check if we got somethign from languagelist
  112.         if self._languagelist.has_key(code):
  113.             return self._languagelist[code]
  114.         # if not, fall back to "dumb" behaviour
  115.         if not "_" in code:
  116.             return code
  117.         (lang, region) = string.split(code, "_")
  118.         return "%s:%s" % (code, lang)
  119.  
  120.     def getDefaultLanguage(self):
  121.         """ returns the current default language (e.g. zh_CN) """
  122.         for line in open(self.environment).readlines():
  123.             line = line.strip()
  124.             if line.startswith("LANGUAGE="):
  125.                 (key,value) = line.split("=")
  126.                 value = value.strip('"')
  127.                 return value.split(":")[0]
  128.  
  129. if __name__ == "__main__":
  130.     datadir = "/usr/share/language-selector/"
  131.     li = LocaleInfo("%s/data/languages" % datadir,
  132.                     "%s/data/countries" % datadir,
  133.                     "%s/data/languagelist" % datadir)
  134.  
  135.     print li.getDefaultLanguage()
  136.  
  137.     print li._lang
  138.     print li._country
  139.     print li._languagelist
  140.     print li.generated_locales()
  141.