def translate(self, locale, native=False, allCountries=False): """ get a locale code and output a human readable name """ returnVal = '' macr = macros.LangpackMacros(self._datadir, locale) if native == True: current_language = None if "LANGUAGE" in os.environ: current_language = os.environ["LANGUAGE"] os.environ["LANGUAGE"] = macr["LOCALE"] lang_name = self.translate_language(macr["LCODE"]) returnVal = lang_name if len(macr["CCODE"]) > 0: country_name = self.translate_country(macr["CCODE"]) # get all locales for this language l = filter(lambda k: k.startswith(macr['LCODE']), self.generated_locales()) # only show region/country if we have more than one if (allCountries == False and len(l) > 1) or allCountries == True: mycountry = self.country(macr['CCODE']) if mycountry: returnVal = "%s (%s)" % (lang_name, country_name) if len(macr["VARIANT"]) > 0: returnVal = "%s - %s" % (returnVal, macr["VARIANT"].encode('UTF-8')) if native == True: if current_language: os.environ["LANGUAGE"] = current_language else: del os.environ["LANGUAGE"] return returnVal
def writeUserFormatsSetting(self, userFormats): """ write various LC_* variables (e.g. de_DE.UTF-8) """ uid = os.getuid() if uid == 0: warnings.warn("No formats locale saved for user '%s'." % os.getenv('USER')) return bus = dbus.SystemBus() obj = bus.get_object('org.freedesktop.Accounts', '/org/freedesktop/Accounts/User%i' % uid) iface = dbus.Interface(obj, dbus_interface='org.freedesktop.Accounts.User') macr = macros.LangpackMacros(self._datadir, userFormats) iface.SetFormatsLocale(macr['SYSLOCALE'])
def generated_locales(self): """ return a list of locales available on the system (running locale -a) """ locales = [] p = subprocess.Popen(["locale", "-a"], stdout=subprocess.PIPE) for line in p.communicate()[0].split("\n"): tmp = line.strip() if tmp.find('.utf8') < 0: continue # we are only interessted in the locale, not the codec macr = macros.LangpackMacros(self._datadir, tmp) locale = macr["LOCALE"] if not locale in locales: locales.append(locale) #print locales return locales
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 not code: return '' macr = macros.LangpackMacros(self._datadir, code) langcode = macr['LCODE'] locale = macr['LOCALE'] # first check if we got somethign from languagelist if locale in self._languagelist: langlist = self._languagelist[locale] # if not, fall back to "dumb" behaviour elif locale == langcode: langlist = locale else: langlist = "%s:%s" % (locale, langcode) if not (langlist.endswith(':en') or langlist == 'en'): langlist = "%s:en" % langlist return langlist
def setConfig(self, locale): """ set the configuration for 'locale'. if locale can't be found a NoConfigurationForLocale exception it thrown """ macr = macros.LangpackMacros(self._datadir, locale) locale = macr["LOCALE"] # check if we have a config if locale not in self.getAvailableConfigs(): raise ExceptionNoConfigForLocale() # remove old symlink self.removeConfig() # do the symlinks, link from /etc/fonts/conf.avail in /etc/fonts/conf.d basedir = "%s/conf.avail/" % self.globalConfDir for pattern in ["*-language-selector-%s-%s.conf" % (macr["LCODE"], macr["CCODE"].lower()), "*-language-selector-%s.conf" % macr["LCODE"], ]: for f in glob.glob(os.path.join(basedir,pattern)): fname = os.path.basename(f) from_link = os.path.join(self.globalConfDir,"conf.avail",fname) to_link = os.path.join(self.globalConfDir, "conf.d", fname) os.symlink(from_link, to_link) return True