home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.4)
-
- import string
- import subprocess
- from gettext import gettext as _
-
- class LocaleInfo(object):
- ''' class with handy functions to parse the locale information '''
- environment = '/etc/environment'
-
- def __init__(self, lang_file, country_file, languagelist_file):
- self._lang = { }
- self._country = { }
- self._languagelist = { }
- self._langFile = lang_file
- for line in open(lang_file):
- tmp = line.strip()
- if tmp.startswith('#') or tmp == '':
- continue
-
- (code, lang) = tmp.split(':')
- self._lang[code] = lang
-
- for line in open(country_file):
- tmp = line.strip()
- if tmp.startswith('#') or tmp == '':
- continue
-
- (un, code, long_code, descr, cap) = tmp.split(':')
- self._country[code] = descr
-
- for line in open(languagelist_file):
- tmp = line.strip()
- if tmp.startswith('#') or tmp == '':
- continue
-
- w = tmp.split(';')
- localeenv = w[6].split(':')
- self._languagelist[localeenv[0]] = '%s' % w[6]
-
-
-
- def lang(self, code):
- ''' map language code to language name '''
- if self._lang.has_key(code):
- return self._lang[code]
-
- return ''
-
-
- def country(self, code):
- ''' map country code to country name'''
- if self._country.has_key(code):
- return self._country[code]
-
- return ''
-
-
- def generated_locales(self):
- ''' return a list of locales avaialble on the system
- (runing locale -a) '''
- locales = []
- p = subprocess.Popen([
- 'locale',
- '-a'], stdout = subprocess.PIPE)
- for line in p.stdout.readlines():
- tmp = line.strip()
- if tmp.startswith('#') and tmp == '' and tmp == 'C' or tmp == 'POSIX':
- continue
-
- locale = string.split(tmp)[0]
- locale = string.split(locale, '.')[0]
- locale = string.split(locale, '@')[0]
- if locale not in locales:
- locales.append(locale)
- continue
-
- return locales
-
-
- def translate(self, locale):
- ''' get a locale code and output a human readable name '''
- if '_' in locale:
- (lang, country) = string.split(locale, '_')
- l = filter((lambda k: k.startswith(lang + '_')), self.generated_locales())
- if len(l) > 1:
- mycountry = self.country(country)
- if mycountry:
- return '%s (%s)' % (_(self.lang(lang)), _(mycountry))
- else:
- return '%s' % _(self.lang(lang))
- else:
- return _(self.lang(lang))
-
- return _(self.lang(locale))
-
-
- def makeEnvString(self, code):
- ''' input is a language code, output a string that can be put in
- the LANGUAGE enviroment variable.
- E.g: en_DK -> en_DK:en
- '''
- if self._languagelist.has_key(code):
- return self._languagelist[code]
-
- if '_' not in code:
- return code
-
- (lang, region) = string.split(code, '_')
- return '%s:%s' % (code, lang)
-
-
- def getDefaultLanguage(self):
- ''' returns the current default language (e.g. zh_CN) '''
- for line in open(self.environment).readlines():
- line = line.strip()
- if line.startswith('LANGUAGE='):
- (key, value) = line.split('=')
- value = value.strip('"')
- return value.split(':')[0]
- continue
-
-
-
- if __name__ == '__main__':
- datadir = '/usr/share/language-selector/'
- li = LocaleInfo('%s/data/languages' % datadir, '%s/data/countries' % datadir, '%s/data/languagelist' % datadir)
- print li.getDefaultLanguage()
- print li._lang
- print li._country
- print li._languagelist
- print li.generated_locales()
-
-