def check_locale_config(title, stats, locale_config): '''Display and return locales that are translated above a fixed threshold''' title2(title) LIMIT = 0.8 sorted_config = list(locale_config) sorted_config.sort() good_locale = [] for locale in sorted_config: if stats.has_key(locale): if stats[locale][3] < LIMIT: print u'* {:s} ({:s})'.format((descriptions[locale] if descriptions.has_key(locale) else ''), locale) else: good_locale.append((descriptions[locale] if descriptions.has_key(locale) else '')) else: # Shorten the locale and test again shorten = locale.split('_')[0] if stats.has_key(shorten): if stats[shorten][3] < LIMIT: print u'* {:s} ({:s})'.format((descriptions[shorten] if descriptions.has_key(shorten) else ''), shorten) else: good_locale.append(descriptions[shorten] if descriptions.has_key(shorten) else '') else: print "* %s no translation at all" %(locale) print '' good_locale.sort() print 'There are %d locales above %d%% translation: %s' %(len(good_locale), LIMIT * 100, ', '.join(good_locale)) return good_locale
def parse(filename): f = open(filename) lines = f.read().splitlines() data = {} for line in lines: line = line.strip() if not line: continue if line[:1] == "#": continue locale, alias = line.split() # Strip ':' if locale[-1] == ":": locale = locale[:-1] # Lower-case locale locale = locale.lower() # Ignore one letter locale mappings (except for 'c') if len(locale) == 1 and locale != "c": continue # Normalize encoding, if given if "." in locale: lang, encoding = locale.split(".")[:2] encoding = encoding.replace("-", "") encoding = encoding.replace("_", "") locale = lang + "." + encoding if encoding.lower() == "utf8": # Ignore UTF-8 mappings - this encoding should be # available for all locales continue data[locale] = alias return data
def parse_glibc_supported(filename): with open(filename, encoding='latin1') as f: lines = list(f) data = {} for line in lines: line = line.strip() if not line: continue if line[:1] == '#': continue line = line.replace('/', ' ').strip() line = line.rstrip('\\').rstrip() words = line.split() if len(words) != 2: continue alias, alias_encoding = words # Lower-case locale locale = alias.lower() # Normalize encoding, if given if '.' in locale: lang, encoding = locale.split('.')[:2] encoding = encoding.replace('-', '') encoding = encoding.replace('_', '') locale = lang + '.' + encoding # Add an encoding to alias alias, _, modifier = alias.partition('@') alias = _locale._replace_encoding(alias, alias_encoding) if modifier and not (modifier == 'euro' and alias_encoding == 'ISO-8859-15'): alias += '@' + modifier data[locale] = alias return data
def parse(filename): with open(filename, encoding='latin1') as f: lines = list(f) data = {} for line in lines: line = line.strip() if not line: continue if line[:1] == '#': continue locale, alias = line.split() # Fix non-standard locale names, e.g. [email protected] if '@' in alias: alias_lang, _, alias_mod = alias.partition('@') if '.' in alias_mod: alias_mod, _, alias_enc = alias_mod.partition('.') alias = alias_lang + '.' + alias_enc + '@' + alias_mod # Strip ':' if locale[-1] == ':': locale = locale[:-1] # Lower-case locale locale = locale.lower() # Ignore one letter locale mappings (except for 'c') if len(locale) == 1 and locale != 'c': continue # Normalize encoding, if given if '.' in locale: lang, encoding = locale.split('.')[:2] encoding = encoding.replace('-', '') encoding = encoding.replace('_', '') locale = lang + '.' + encoding data[locale] = alias return data
def getVoiceFamilies(self): # Always offer the configured default voice with a language # set according to the current locale. from locale import getlocale, LC_MESSAGES locale = getlocale(LC_MESSAGES)[0] if locale is None or locale == 'C': lang = None dialect = None else: lang, dialect = locale.split('_') list_synthesis_voices = espeak.list_voices() families = [speechserver.VoiceFamily({ \ speechserver.VoiceFamily.NAME: self._default_voice_name, #speechserver.VoiceFamily.GENDER: speechserver.VoiceFamily.MALE, speechserver.VoiceFamily.DIALECT: dialect, speechserver.VoiceFamily.LOCALE: lang})] for voice in list_synthesis_voices: dialect = None identifier = voice.identifier.split('/')[-1].split('-') locale = identifier[0] try: dialect = identifier[1] except IndexError: dialect = None families.append(speechserver.VoiceFamily({ \ speechserver.VoiceFamily.NAME: voice.name, speechserver.VoiceFamily.DIALECT: dialect, speechserver.VoiceFamily.LOCALE: locale})) return families
def parse(filename): f = open(filename) lines = f.read().splitlines() data = {} for line in lines: line = line.strip() if not line: continue if line[:1] == '#': continue locale, alias = line.split() # Strip ':' if locale[-1] == ':': locale = locale[:-1] # Lower-case locale locale = locale.lower() # Ignore one letter locale mappings (except for 'c') if len(locale) == 1 and locale != 'c': continue # Normalize encoding, if given if '.' in locale: lang, encoding = locale.split('.')[:2] encoding = encoding.replace('-', '') encoding = encoding.replace('_', '') locale = lang + '.' + encoding if encoding.lower() == 'utf8': # Ignore UTF-8 mappings - this encoding should be # available for all locales continue data[locale] = alias return data
def to_locale(value, locale = None, default = None, fallback = True): """ Utility function used to localize the provided value according to the currently loaded set of bundles, the bundles are loaded at the application start time from the proper sources. The (target) locale value for the translation may be provided or in case it's not the locale associated with the current request is used as an alternative. In case the value is not localizable (no valid bundle available) it is returned as it is without change, or in alternative a default value (if passed) is returned. :type value: String :param value: The value that is going to be localized according to the current application environment, this may be a normal english dictionary string or a variable reference. :type locale: String :param locale: The (target) locale value to be used in the translation process for the provided string value. :type default: String :param default: The default value to be returned in case it's not possible to run the localization process. :type fallback: bool :param fallback: If a fallback operation should be performed in case no value was retrieved from the base/request locale. :rtype: String :return: The localized value for the current environment or the proper (original) value in case no localization was possible. """ value_t = type(value) is_sequence = value_t in (list, tuple) if is_sequence: return _serialize([ to_locale( value, locale = locale, default = default, fallback = fallback ) for value in value ]) has_context = common.base().has_context() locale = locale or (flask.request.locale if has_context else None) if locale: bundle = common.base().get_bundle(locale) or {} result = bundle.get(value, None) if not result == None: return result language = locale.split("_", 1)[0] bundle = common.base().get_bundle(language) or {} result = bundle.get(value, None) if not result == None: return result app = common.base().APP if fallback and app: return to_locale( value, locale = app._locale_d, default = default, fallback = False ) return value if default == None else default
def getVoiceFamilies(self): # Always offer the configured default voice with a language # set according to the current locale. from locale import getlocale, LC_MESSAGES locale = getlocale(LC_MESSAGES)[0] if locale is None or locale == 'C': lang = None dialect = None else: lang, dialect = locale.split('_') voices = ((self._default_voice_name, lang, None),) try: # This command is not available with older SD versions. list_synthesis_voices = self._client.list_synthesis_voices except AttributeError: pass else: try: voices += self._send_command(list_synthesis_voices) except: pass families = [speechserver.VoiceFamily({ \ speechserver.VoiceFamily.NAME: name, #speechserver.VoiceFamily.GENDER: speechserver.VoiceFamily.MALE, speechserver.VoiceFamily.DIALECT: dialect, speechserver.VoiceFamily.LOCALE: lang}) for name, lang, dialect in voices] return families
def getVoiceFamilies(self): # Always offer the configured default voice with a language # set according to the current locale. from locale import getlocale, LC_MESSAGES locale = getlocale(LC_MESSAGES)[0] if locale is None or locale == 'C': lang = None dialect = None else: lang, dialect = locale.split('_') voices = ((self._default_voice_name, lang, None), ) try: # This command is not available with older SD versions. list_synthesis_voices = self._client.list_synthesis_voices except AttributeError: pass else: try: voices += self._send_command(list_synthesis_voices) except: pass families = [speechserver.VoiceFamily({ \ speechserver.VoiceFamily.NAME: name, #speechserver.VoiceFamily.GENDER: speechserver.VoiceFamily.MALE, speechserver.VoiceFamily.DIALECT: dialect, speechserver.VoiceFamily.LOCALE: lang}) for name, lang, dialect in voices] return families
def button_remove_clicked (self, button): locale = self.selected_language.replace("UTF-8", "utf8") language_code = locale.split("_")[0] os.system("localedef --delete-from-archive %s" % locale) # If there are no more locales using the language, remove the language packs num_locales = commands.getoutput("localedef --list-archive | grep %s_ | wc -l" % language_code) # Check if the language packs are installed if num_locales == "0": installed_packs = [] for pkgname in self.pack_prefixes: pkgname = "%s%s" % (pkgname, language_code) if pkgname in self.cache: pkg = self.cache[pkgname] if (pkg.has_versions and pkg.current_state == apt_pkg.CURSTATE_INSTALLED): installed_packs.append(pkg) if len(installed_packs) > 0: cmd = ["/usr/sbin/synaptic", "--hide-main-window", "--non-interactive", "--parent-window-id", "%s" % self.builder.get_object("main_window").get_window().get_xid()] cmd.append("-o") cmd.append("Synaptic::closeZvt=true") cmd.append("--progress-str") cmd.append("\"" + _("Please wait, this can take some time") + "\"") cmd.append("--finish-str") cmd.append("\"" + _("The related language packs were removed") + "\"") f = tempfile.NamedTemporaryFile() for pkg in installed_packs: f.write("%s\tdeinstall\n" % pkg.name) cmd.append("--set-selections-file") cmd.append("%s" % f.name) f.flush() comnd = Popen(' '.join(cmd), shell=True) returnCode = comnd.wait() f.close() self.build_lang_list()
def load_locale(available, fallback = "en_us"): # tries to gather the best locale value using the currently # available strategies and in case the retrieved local is part # of the valid locales for the app returns the locale, otherwise # returns the fallback value instead locale = get_locale(fallback = fallback) language = locale.split("_", 1)[0] if locale in available: return locale if language in available: return locale return fallback
def run(): BUFFER_TO_SEND = "{{info}}" url = 'http://ipinfo.io/json' response = urlopen(url) data = json.load(response) IP=data['ip'] city = data['city'] country=data['country'] # Set dummy language and encoding to make sure locale is found language = "English" app = wx.App(False) # the wx.App object must be created first. data = commands.getoutput("locale") data = data.split("\n") for locale in data: # Find the language locale if locale.split("=")[0] == "LANG": language = locale.split("=")[1].split(".")[0] buffer = str(wx.GetDisplaySize()) buffer = buffer.replace("(","") buffer = buffer.replace(")","") BUFFER_TO_SEND += "--" + "::" + "Hostname"+ ";;" + "::" + (socket.gethostname()) + ";;" + "++" BUFFER_TO_SEND += "--" + "::" +"Ip"+ ";;" + "::" + IP + ";;" + "++" BUFFER_TO_SEND += "--" + "::" + "City"+ ";;" + "::" + city + ";;" + "++" BUFFER_TO_SEND += "--" + "::" + "Country"+ ";;" + "::" + country + ";;" + "++" BUFFER_TO_SEND += "--" + "::" + "Username"+ ";;" + "::" + getpass.getuser() + ";;" + "++" BUFFER_TO_SEND += "--" + "::" + "Os System"+ ";;" + "::" + platform.system() + ";;" + "++" BUFFER_TO_SEND += "--" + "::" + "Os Version"+ ";;" + "::" + platform.release() + ";;" + "++" BUFFER_TO_SEND += "--" + "::" + "Os Language"+ ";;" + "::" + language + ";;" + "++" BUFFER_TO_SEND += "--" + "::" + "Processor Family"+ ";;" + "::" + platform.processor() + ";;" + "++" BUFFER_TO_SEND += "--" + "::" + "Total Processors"+ ";;" + "::" + str(multiprocessing.cpu_count()) + ";;" + "++" BUFFER_TO_SEND += "--" + "::" + "Screen Resolution"+ ";;" + "::" + buffer + ";;" + "++" while True: try: utils.send_output(BUFFER_TO_SEND) break except: pass
def get_locales_from_po_files(): '''Return a set for locales for which we have a po file ''' '''Run make getSvnTranslations first''' locales = set() locales_dir = gcompris_qt + "/po" for locale in os.listdir(locales_dir): locales.add(locale.split('_', 1)[1][:-3]) return locales
def _StripToLanguage(): """Strip Region / Script codes from Language codes in order to capture more files.""" languages = Settings.Language() for index, locale in enumerate(languages): if "_" in locale: Settings._settings["language"][index] = locale.split("_")[0] # There may be duplicates if multiple entries used the same Language, so strip them out Settings._settings["language"] = list( set(Settings._settings["language"]))
def _get_translation(locale, attrs, name, defaultname, default=''): if locale: locale = locale.replace('_', '-').lower() if name % (locale,) in attrs: return attrs[name % (locale,)][0] locale = locale.split('-', 1)[0] name_short_lang = name % (locale,) if name_short_lang in attrs: return attrs[name_short_lang][0] for key in attrs: if key.startswith(name_short_lang): return attrs[key][0] return attrs.get(defaultname, [default])[0]
def _find_available_locale(self, locale): base_slides_dir = os.path.join(self.path, 'slides', 'l10n') extra_slides_dir = os.path.join(self.path, 'slides', 'extra') ll_cc = locale.split('.')[0] ll = ll_cc.split('_')[0] for slides_dir in [extra_slides_dir, base_slides_dir]: for test_locale in [locale, ll_cc, ll]: locale_dir = os.path.join(slides_dir, test_locale) if os.path.exists(locale_dir): return test_locale return 'C'
def _get_translation(locale, attrs, name, defaultname, default=u''): if locale: locale = locale.replace(u'_', u'-').lower() if name % (locale, ) in attrs: return attrs[name % (locale, )][0].decode('UTF-8', 'replace') locale = locale.split(u'-', 1)[0] name_short_lang = name % (locale, ) if name_short_lang in attrs: return attrs[name_short_lang][0].decode('UTF-8', 'replace') for key in attrs: if key.startswith(name_short_lang): return attrs[key][0].decode('UTF-8', 'replace') return attrs.get(defaultname, [default.encode('utf-8')])[0].decode('UTF-8', 'replace')
def button_install_clicked (self, button): parts = self.selected_language.split(" ") locale = parts[0].strip() short_locale = locale.split(".")[0].strip() if len(parts) > 1: charmap = parts[1].strip() print "localedef -f %s -i %s %s" % (charmap, short_locale, locale) os.system("localedef -f %s -i %s %s" % (charmap, short_locale, locale)) else: print "localedef -i %s %s" % (short_locale, locale) os.system("localedef -i %s %s" % (short_locale, locale)) if os.path.exists("/var/lib/locales/supported.d"): os.system("localedef --list-archive | sed 's/utf8/UTF-8 UTF-8/g' > /var/lib/locales/supported.d/mintlocale") sys.exit(0)
def button_install_clicked(self, button): parts = self.selected_language.split(" ") locale = parts[0].strip() short_locale = locale.split(".")[0].strip() if len(parts) > 1: charmap = parts[1].strip() print("localedef -f %s -i %s %s" % (charmap, short_locale, locale)) os.system("localedef -f %s -i %s %s" % (charmap, short_locale, locale)) else: print("localedef -i %s %s" % (short_locale, locale)) os.system("localedef -i %s %s" % (short_locale, locale)) if os.path.exists("/var/lib/locales/supported.d"): os.system("localedef --list-archive | sed 's/utf8/UTF-8 UTF-8/g' > /var/lib/locales/supported.d/mintlocale") sys.exit(0)
def getVoiceFamilies(self): # Always offer the configured default voice with a language # set according to the current locale. from locale import getlocale, LC_MESSAGES locale = getlocale(LC_MESSAGES)[0] if locale is None or locale == 'C': locale_language = None else: locale_lang, locale_dialect = locale.split('_') locale_language = locale_lang + '-' + locale_dialect voices = () try: # This command is not available with older SD versions. list_synthesis_voices = self._client.list_synthesis_voices except AttributeError: pass else: try: voices += self._send_command(list_synthesis_voices) except: pass default_lang = "" if locale_language: # Check whether how it appears in the server list for name, lang, variant in voices: if lang == locale_language: default_lang = locale_language break if not default_lang: for name, lang, variant in voices: if lang == locale_lang: default_lang = locale_lang if not default_lang: default_lang = locale_language voices = ((self._default_voice_name, default_lang, None), ) + voices families = [] for name, lang, variant in voices: families.append(speechserver.VoiceFamily({ \ speechserver.VoiceFamily.NAME: name, #speechserver.VoiceFamily.GENDER: speechserver.VoiceFamily.MALE, speechserver.VoiceFamily.LANG: lang.partition("-")[0], speechserver.VoiceFamily.DIALECT: lang.partition("-")[2], speechserver.VoiceFamily.VARIANT: variant})) return families
def _find_available_locale(self, locale): slides_dir = os.path.join(self.path, "slides") locale_choice = 'c' if os.path.exists( os.path.join(slides_dir, "loc."+locale) ): locale_choice = locale else: ll_cc = locale.split('.')[0] ll = ll_cc.split('_')[0] if os.path.exists('%s/loc.%s' % (slides_dir,ll_cc)): locale_choice = ll_cc elif os.path.exists('%s/loc.%s' % (slides_dir,ll)): locale_choice = ll return locale_choice
def _find_available_locale(self, locale): slides_dir = os.path.join(self.path, "slides") locale_choice = 'c' if os.path.exists(os.path.join(slides_dir, "loc." + locale)): locale_choice = locale else: ll_cc = locale.split('.')[0] ll = ll_cc.split('_')[0] if os.path.exists('%s/loc.%s' % (slides_dir, ll_cc)): locale_choice = ll_cc elif os.path.exists('%s/loc.%s' % (slides_dir, ll)): locale_choice = ll return locale_choice
def button_remove_clicked(self, button): locale = self.selected_language.replace("UTF-8", "utf8") language_code = locale.split("_")[0] os.system("localedef --delete-from-archive %s" % locale) # If there are no more locales using the language, remove the language packs num_locales = commands.getoutput( "localedef --list-archive | grep %s_ | wc -l" % language_code) # Check if the language packs are installed if num_locales == "0": installed_packs = [] for pkgname in self.pack_prefixes: pkgname = "%s%s" % (pkgname, language_code) if pkgname in self.cache: pkg = self.cache[pkgname] if (pkg.has_versions and pkg.current_state == apt_pkg.CURSTATE_INSTALLED): installed_packs.append(pkg) if len(installed_packs) > 0: cmd = [ "/usr/sbin/synaptic", "--hide-main-window", "--non-interactive", "--parent-window-id", "%s" % self.builder.get_object( "main_window").get_window().get_xid() ] cmd.append("-o") cmd.append("Synaptic::closeZvt=true") cmd.append("--progress-str") cmd.append("\"" + _("Please wait, this can take some time") + "\"") cmd.append("--finish-str") cmd.append("\"" + _("The related language packs were removed") + "\"") f = tempfile.NamedTemporaryFile() for pkg in installed_packs: f.write("%s\tdeinstall\n" % pkg.name) cmd.append("--set-selections-file") cmd.append("%s" % f.name) f.flush() comnd = Popen(' '.join(cmd), shell=True) returnCode = comnd.wait() f.close() self.build_lang_list()
def _get_locids(self): '''Generate a list of possible locales - folders to check for help screen text in. Used by get_help_text() in conjunction with a screen's indicated help text file path. The list will include one or more of: * The current locale (e.g., "en_US.UTF-8") * The current locale, stripped of encoding (e.g., "en_US") * The current language, stripped of locale (e.g., "en") * The default locale ("C") ''' locale = self.locale locids = [locale] if "." in locale: locale = locale.split(".")[0] locids.append(locale) if len(locale) > 2: locids.append(locale[:2]) locids.append("C") return locids
def __init__(self, domain): """Initialises a translation catalogue for a specific domin. The catalogue is created from a resource loaded by :func:`virtualtouchpad.resource.stream_open`, and it must be located in the ``translations`` package. :param str domain: The domain. This is used as sub-directory name under ``translations``. """ try: mo_file = self._mo_file( domain, self._locales(self.ENVVARS)) except: mo_file = self._mo_file( domain, ( locale.split('_')[0] for locale in self._locales(self.ENVVARS))) super().__init__(mo_file)
def parse(filename): f = open(filename) lines = f.read().splitlines() data = {} for line in lines: line = line.strip() if not line: continue if line[:1] == '#': continue locale, alias = line.split() # Fix non-standard locale names, e.g. [email protected] if '@' in alias: alias_lang, _, alias_mod = alias.partition('@') if '.' in alias_mod: alias_mod, _, alias_enc = alias_mod.partition('.') alias = alias_lang + '.' + alias_enc + '@' + alias_mod # Strip ':' if locale[-1] == ':': locale = locale[:-1] # Lower-case locale locale = locale.lower() # Ignore one letter locale mappings (except for 'c') if len(locale) == 1 and locale != 'c': continue # Normalize encoding, if given if '.' in locale: lang, encoding = locale.split('.')[:2] encoding = encoding.replace('-', '') encoding = encoding.replace('_', '') locale = lang + '.' + encoding if encoding.lower() == 'utf8': # Ignore UTF-8 mappings - this encoding should be # available for all locales continue data[locale] = alias return data
def get_desktop(self, key): " get generic option under 'Desktop Entry'" # first try dgettext if self.has_option_desktop("X-Ubuntu-Gettext-Domain"): value = self.get(self.DE, key) if value: domain = self.get(self.DE, "X-Ubuntu-Gettext-Domain") translated_value = gettext.dgettext(domain, value) if value != translated_value: return translated_value # then try the i18n version of the key (in [de_DE] or # [de]) but ignore errors and return the untranslated one then try: locale = getdefaultlocale(('LANGUAGE','LANG','LC_CTYPE','LC_ALL'))[0] if locale: if self.has_option_desktop("%s[%s]" % (key, locale)): return self.get(self.DE, "%s[%s]" % (key, locale)) if "_" in locale: locale_short = locale.split("_")[0] if self.has_option_desktop("%s[%s]" % (key, locale_short)): return self.get(self.DE, "%s[%s]" % (key, locale_short)) except ValueError,e : pass
def diff_locale_set(title, code, files): if not code and not files: return title2(title) if verbose: title3("We have voices for these locales:") missing = [] for locale in code: if os.path.isdir(locale): print '* ' + locale else: # Shorten the locale and test again shorten = locale.split('_') if os.path.isdir(shorten[0]): print '* ' + locale else: missing.append(locale) print '' print "We miss voices for these locales:" for f in missing: print '* ' + f print ''
def check_locale_config(title, stats, locale_config): '''Display and return locales that are translated above a fixed threshold''' title2(title) LIMIT = 0.8 sorted_config = list(locale_config) sorted_config.sort() good_locale = [] for locale in sorted_config: if stats.has_key(locale): if stats[locale][3] < LIMIT: print u'* {:s} ({:s})'.format( (descriptions[locale] if descriptions.has_key(locale) else ''), locale) else: good_locale.append((descriptions[locale] if descriptions.has_key(locale) else '')) else: # Shorten the locale and test again shorten = locale.split('_')[0] if stats.has_key(shorten): if stats[shorten][3] < LIMIT: print u'* {:s} ({:s})'.format( (descriptions[shorten] if descriptions.has_key(shorten) else ''), shorten) else: good_locale.append(descriptions[shorten] if descriptions. has_key(shorten) else '') else: print "* %s no translation at all" % (locale) print '' good_locale.sort() print 'There are %d locales above %d%% translation: %s' % ( len(good_locale), LIMIT * 100, ', '.join(good_locale)) return good_locale
def build_lang_list(self): self.builder.get_object('button_install').set_sensitive(False) model = Gtk.ListStore(str, str, GdkPixbuf.Pixbuf) # label, locale, flag model.set_sort_column_id(0, Gtk.SortType.ASCENDING) #Load countries into memory self.countries = {} file = open('/usr/lib/linuxmint/mintLocale/countries', "r") for line in file: line = line.strip() split = line.split("=") if len(split) == 2: self.countries[split[0]] = split[1] file.close() #Load languages into memory self.languages = {} file = open('/usr/lib/linuxmint/mintLocale/languages', "r") for line in file: line = line.strip() split = line.split("=") if len(split) == 2: self.languages[split[0]] = split[1] file.close() locales = commands.getoutput("cat /usr/share/i18n/SUPPORTED") installed = commands.getoutput("localedef --list-archive | sed s/utf8/UTF-8/g").split("\n") for line in locales.split("\n"): line = line.strip() if line == '' or line.startswith('#'): continue parts = line.split(" ") locale = parts[0].strip() lcharmap = None if len(parts) > 1: lcharmap = parts[1].strip() if locale not in installed: locale_code = locale.split(".")[0].strip() charmap = None if len(locale.split(".")) > 1: charmap = locale.split(".")[1].strip() if "_" in locale_code: split = locale_code.split("_") if len(split) == 2: language_code = split[0] if language_code == "iw": continue # use 'he' instead if language_code in self.languages: language = self.languages[language_code] else: language = language_code country_code = split[1].split('@')[0].lower() if country_code in self.countries: country = self.countries[country_code] else: country = country_code if '@' in split[1]: language_label = "%s (@%s), %s" % (language, split[1].split('@')[1].strip(), country) else: language_label = "%s, %s" % (language, country) flag_path = '/usr/share/linuxmint/mintLocale/flags/16/' + country_code + '.png' else: if locale_code in self.languages: language_label = self.languages[locale_code] else: language_label = locale_code flag_path = '/usr/share/linuxmint/mintLocale/flags/16/languages/%s.png' % locale_code flag_path = self.set_minority_language_flag_path(locale_code, flag_path) if lcharmap is not None: language_label = "%s <small><span foreground='#3c3c3c'>%s</span></small>" % (language_label, lcharmap) iter = model.append() model.set_value(iter, 0, language_label) model.set_value(iter, 1, line) if os.path.exists(flag_path): model.set_value(iter, 2, GdkPixbuf.Pixbuf.new_from_file(flag_path)) else: model.set_value(iter, 2, GdkPixbuf.Pixbuf.new_from_file('/usr/share/linuxmint/mintLocale/flags/16/generic.png')) treeview = self.builder.get_object("treeview_language_list") treeview.set_model(model) treeview.set_search_column(0) self.treeview.connect("cursor-changed", self.select_language)
def _simplify_locale(locale): """return a given standard locale string in the SPIA simplified locale format""" return locale.split(".")[0].replace("_","-").lower()
""" templ = Template(templateDef, searchList=[nameSpace]) return templ def printConf(self): configDef = """ <corpus ident="$registry" keyboard_lang="$keyboard" sentence_struct="s" features="morphology, syntax" repo="$lindatrepo" pmltq="$pmltqlink" /> """ conf = Template(configDef, searchList=[nameSpace]) return conf if __name__ == "__main__": locale = str(get_locale(languagecode)).rstrip() keyboard = locale.split("_", 1)[0] registry_name = "ud_" + udversionshort + "_" + filename + "_a" pmltqlink = pmltqprefix #TODO: adjust nameSpace = { 'filename': filename, 'locale': locale, 'language': language, 'languagecode': languagecode, 'keyboard': keyboard, 'registry': registry_name, 'corpname': corpname, 'lindatrepo': lindatrepo, 'pmltqlink': pmltqlink, 'dirname': dirname, 'udversionlong': udversionlong,
def set_system_locale(self): language_str = _("No locale defined") region_str = _("No locale defined") # Get system locale if os.path.exists(self.locale_path): vars = dict() with open(self.locale_path) as f: for line in f: eq_index = line.find('=') var_name = line[:eq_index].strip() value = line[eq_index + 1:].strip() vars[var_name] = value if "LANG" in vars: locale = vars['LANG'].replace('"', '').replace("'", "") locale = locale.split(".")[0].strip() if "_" in locale: split = locale.split("_") if len(split) == 2: language_code = split[0] if language_code in self.languages: language = self.languages[language_code] else: language = language_code country_code = split[1].lower() if country_code in self.countries: country = self.countries[country_code] else: country = country_code language_label = u"%s, %s" % (language, country) else: if locale in self.languages: language_label = self.languages[locale] else: language_label = locale language_str = language_label if "LC_NUMERIC" in vars: locale = vars['LC_NUMERIC'].replace('"', '').replace("'", "") locale = locale.split(".")[0].strip() if "_" in locale: split = locale.split("_") if len(split) == 2: language_code = split[0] if language_code in self.languages: language = self.languages[language_code] else: language = language_code country_code = split[1].lower() if country_code in self.countries: country = self.countries[country_code] else: country = country_code language_label = u"%s, %s" % (language, country) else: if locale in self.languages: language_label = self.languages[locale] else: language_label = locale region_str = language_label language_prefix = ("Language:") region_prefix = ("Region:") self.system_label.set_markup( "<b>%s</b>\n<small>%s <i>%s</i>\n%s <i>%s</i></small>" % (_("System locale"), language_prefix, language_str, region_prefix, region_str))
def build_lang_list(self): self.builder.get_object('button_install').set_sensitive(False) model = Gtk.ListStore(str, str, GdkPixbuf.Pixbuf) # label, locale, flag model.set_sort_column_id(0, Gtk.SortType.ASCENDING) #Load countries into memory self.countries = {} file = open('/usr/lib/linuxmint/mintLocale/countries', "r") for line in file: line = line.strip() split = line.split("=") if len(split) == 2: self.countries[split[0]] = split[1] file.close() #Load languages into memory self.languages = {} file = open('/usr/lib/linuxmint/mintLocale/languages', "r") for line in file: line = line.strip() split = line.split("=") if len(split) == 2: self.languages[split[0]] = split[1] file.close() locales = commands.getoutput("cat /usr/share/i18n/SUPPORTED") installed = commands.getoutput( "localedef --list-archive | sed s/utf8/UTF-8/g").split("\n") for line in locales.split("\n"): line = line.strip() if line == '' or line.startswith('#'): continue parts = line.split(" ") locale = parts[0].strip() lcharmap = None if len(parts) > 1: lcharmap = parts[1].strip() if locale not in installed: locale_code = locale.split(".")[0].strip() charmap = None if len(locale.split(".")) > 1: charmap = locale.split(".")[1].strip() if "_" in locale_code: split = locale_code.split("_") if len(split) == 2: language_code = split[0] if language_code == "iw": continue # use 'he' instead if language_code in self.languages: language = self.languages[language_code] else: language = language_code country_code = split[1].split('@')[0].lower() if country_code in self.countries: country = self.countries[country_code] else: country = country_code if '@' in split[1]: language_label = "%s (@%s), %s" % ( language, split[1].split('@')[1].strip(), country) else: language_label = "%s, %s" % (language, country) flag_path = '/usr/share/linuxmint/mintLocale/flags/16/' + country_code + '.png' else: if locale_code in self.languages: language_label = self.languages[locale_code] else: language_label = locale_code flag_path = '/usr/share/linuxmint/mintLocale/flags/16/languages/%s.png' % locale_code if lcharmap is not None: language_label = "%s <small><span foreground='#3c3c3c'>%s</span></small>" % ( language_label, lcharmap) iter = model.append() model.set_value(iter, 0, language_label) model.set_value(iter, 1, line) if os.path.exists(flag_path): model.set_value(iter, 2, GdkPixbuf.Pixbuf.new_from_file(flag_path)) else: model.set_value( iter, 2, GdkPixbuf.Pixbuf.new_from_file( '/usr/share/linuxmint/mintLocale/flags/16/generic.png' )) treeview = self.builder.get_object("treeview_language_list") treeview.set_model(model) treeview.set_search_column(0) self.treeview.connect("cursor-changed", self.select_language)
def locale2lang_country(cls, locale): if not locale: return None, None l = locale.split("-") return (str2lower(l[0]), str2upper(l[1]) if len(l) >= 2 else None)
def country_from_locale(locale): """Obtain the country code from a locale code example: fr_FR -> FR""" return locale.split('_')[1]
def _simplify_locale(locale): """return a given standard locale string in the SPIA simplified locale format""" return locale.split(".")[0].replace("_", "-").lower()
def language_from_locale(locale): """Obtain the language code from a locale code example: fr_FR -> fr""" return locale.split('_')[0]
def build_lang_list(self): self.builder.get_object('button_install').set_sensitive(False) model = Gtk.ListStore(str, str, GdkPixbuf.Pixbuf) # label, locale, flag model.set_sort_column_id(0, Gtk.SortType.ASCENDING) # Load countries into memory self.countries = {} with codecs.open('/usr/share/linuxmint/mintlocale/countries', "r", encoding='utf-8') as file: for line in file: line = line.strip() split = line.split("=") if len(split) == 2: self.countries[split[0]] = split[1] # Load languages into memory self.languages = {} with codecs.open('/usr/share/linuxmint/mintlocale/languages', "r", encoding='utf-8') as file: for line in file: line = line.strip() split = line.split("=") if len(split) == 2: self.languages[split[0]] = split[1] if os.path.exists("/usr/share/i18n/SUPPORTED"): with codecs.open("/usr/share/i18n/SUPPORTED", "r", encoding="utf-8") as file: locales = file.read() else: locales = u"" installed = subprocess.check_output("localedef --list-archive | sed s/utf8/UTF-8/g", shell=True).decode("utf-8").split("\n") for line in locales.split("\n"): line = line.strip() if line == '' or line.startswith('#'): continue parts = line.split(" ") locale = parts[0].strip() lcharmap = None if len(parts) > 1: lcharmap = parts[1].strip() if locale not in installed: locale_code = locale.split(".")[0].strip() charmap = None if len(locale.split(".")) > 1: charmap = locale.split(".")[1].strip() if "_" in locale_code: split = locale_code.split("_") if len(split) == 2: language_code = split[0] if language_code == "iw": continue # use 'he' instead if language_code in self.languages: language = self.languages[language_code] else: language = language_code country_code = split[1].split('@')[0].lower() if country_code in self.countries: country = self.countries[country_code] else: country = country_code if '@' in split[1]: language_label = "%s (@%s), %s" % (language, split[1].split('@')[1].strip(), country) else: language_label = "%s, %s" % (language, country) flag_path = FLAG_PATH % country_code else: if locale_code in self.languages: language_label = self.languages[locale_code] else: language_label = locale_code flag_path = FLAG_PATH % locale_code flag_path = self.set_minority_language_flag_path(locale_code, flag_path) if lcharmap is not None: language_label = "%s <small><span foreground='#3c3c3c'>%s</span></small>" % (language_label, lcharmap) iter = model.append() model.set_value(iter, 0, language_label) model.set_value(iter, 1, line) if os.path.exists(flag_path): model.set_value(iter, 2, GdkPixbuf.Pixbuf.new_from_file_at_size(flag_path, -1, FLAG_SIZE * self.scale)) else: model.set_value(iter, 2, GdkPixbuf.Pixbuf.new_from_file_at_size(FLAG_PATH % '_generic', -1, FLAG_SIZE * self.scale)) treeview = self.builder.get_object("treeview_language_list") treeview.set_model(model) treeview.set_search_column(0) self.treeview.connect("cursor-changed", self.select_language)
def set_system_locale(self): language_str = _("No locale defined") region_str = _("No locale defined") # Get system locale if os.path.exists("/etc/default/locale"): vars = dict() with open("/etc/default/locale") as f: for line in f: eq_index = line.find('=') var_name = line[:eq_index].strip() value = line[eq_index + 1:].strip() vars[var_name] = value if "LANG" in vars: locale = vars['LANG'].replace('"', '').replace("'", "") locale = locale.replace("utf8", "UTF-8") locale = locale.replace("UTF-8", "") locale = locale.replace(".", "") locale = locale.strip() if "_" in locale: split = locale.split("_") if len(split) == 2: language_code = split[0] if language_code in self.languages: language = self.languages[language_code] else: language = language_code country_code = split[1].lower() if country_code in self.countries: country = self.countries[country_code] else: country = country_code language_label = "%s, %s" % (language, country) else: if locale in self.languages: language_label = self.languages[locale] else: language_label = locale language_str = language_label if "LC_NUMERIC" in vars: locale = vars['LC_NUMERIC'].replace('"', '').replace("'", "") locale = locale.replace("utf8", "UTF-8") locale = locale.replace("UTF-8", "") locale = locale.replace(".", "") locale = locale.strip() if "_" in locale: split = locale.split("_") if len(split) == 2: language_code = split[0] if language_code in self.languages: language = self.languages[language_code] else: language = language_code country_code = split[1].lower() if country_code in self.countries: country = self.countries[country_code] else: country = country_code language_label = "%s, %s" % (language, country) else: if locale in self.languages: language_label = self.languages[locale] else: language_label = locale region_str = language_label language_prefix = ("Language:") region_prefix = ("Region:") self.system_label.set_markup("%s\n<small><i><span foreground='#3C3C3C'>%s %s\n%s %s</span></i></small>" % (_("System locale"), language_prefix, language_str, region_prefix, region_str))
def build_lang_list(self): self.builder.get_object('button_install').set_sensitive(False) model = Gtk.ListStore(str, str, GdkPixbuf.Pixbuf) # label, locale, flag model.set_sort_column_id(0, Gtk.SortType.ASCENDING) # Load countries into memory self.countries = {} with codecs.open('/usr/share/linuxmint/mintlocale/countries', "r", encoding='utf-8') as file: for line in file: line = line.strip() split = line.split("=") if len(split) == 2: self.countries[split[0]] = split[1] # Load languages into memory self.languages = {} with codecs.open('/usr/share/linuxmint/mintlocale/languages', "r", encoding='utf-8') as file: for line in file: line = line.strip() split = line.split("=") if len(split) == 2: self.languages[split[0]] = split[1] if os.path.exists("/usr/share/i18n/SUPPORTED"): with codecs.open("/usr/share/i18n/SUPPORTED", "r", encoding="utf-8") as file: locales = file.read() else: locales = u"" installed = subprocess.check_output("localedef --list-archive | sed s/utf8/UTF-8/g", shell=True).decode("utf-8").split("\n") for line in locales.split("\n"): line = line.strip() if line == '' or line.startswith('#'): continue parts = line.split(" ") locale = parts[0].strip() lcharmap = None if len(parts) > 1: lcharmap = parts[1].strip() if locale not in installed: locale_code = locale.split(".")[0].strip() charmap = None if len(locale.split(".")) > 1: charmap = locale.split(".")[1].strip() if "_" in locale_code: split = locale_code.split("_") if len(split) == 2: language_code = split[0] if language_code == "iw": continue # use 'he' instead if language_code in self.languages: language = self.languages[language_code] else: language = language_code country_code = split[1].split('@')[0].lower() if country_code in self.countries: country = self.countries[country_code] else: country = country_code if '@' in split[1]: language_label = "%s (@%s), %s" % (language, split[1].split('@')[1].strip(), country) else: language_label = "%s, %s" % (language, country) flag_path = FLAG_PATH % country_code else: if locale_code in self.languages: language_label = self.languages[locale_code] else: language_label = locale_code flag_path = FLAG_PATH % locale_code flag_path = self.set_minority_language_flag_path(locale_code, flag_path) if lcharmap is not None: language_label = "%s <small><span foreground='#3c3c3c'>%s</span></small>" % (language_label, lcharmap) iter = model.append() model.set_value(iter, 0, language_label) model.set_value(iter, 1, line) if os.path.exists(flag_path): model.set_value(iter, 2, GdkPixbuf.Pixbuf.new_from_file_at_size(flag_path, -1, FLAG_SIZE)) else: model.set_value(iter, 2, GdkPixbuf.Pixbuf.new_from_file_at_size(FLAG_PATH % '_generic', -1, FLAG_SIZE)) treeview = self.builder.get_object("treeview_language_list") treeview.set_model(model) treeview.set_search_column(0) self.treeview.connect("cursor-changed", self.select_language)
MAXDETAIL 50 """ templ = Template(templateDef, searchList=[nameSpace]) return templ def printConf(self): configDef = """ <corpus ident="$registry" sentence_struct="s" features="morphology, syntax" keyboard_lang="$keyboard" repo="$lindatrepo" pmltq="$pmltqlink" /> """ conf = Template(configDef, searchList=[nameSpace]) return conf if __name__ == "__main__": locale = str(get_locale(languagecode)).rstrip() keyboard = locale.split("_", 1)[0] registry_name = "ud_22_" + filename + "_a" pmltqlink = pmltqprefix #TODO: adjust nameSpace = {'filename': filename, 'locale': locale, 'language': language, 'languagecode': languagecode, 'keyboard': keyboard, 'registry': registry_name, 'corpname': corpname, 'lindatrepo': lindatrepo, 'pmltqlink': pmltqlink, 'dirname': dirname} registry = PrintTemplates(nameSpace) basepath = os.path.dirname(__file__) filepath = os.path.abspath(os.path.join(basepath, "..", "templates", registry_name)) registry_file = open(filepath, "w") registry_file.write(str(registry.printRegistry())) config_file=open('to_corplist','a') config_file.write(str(registry.printConf()))
def call_installer_scripts(self): IGNORE_LIST = [ '00_scripts.sh', '95_completemsg.sh', '99_reboot.sh' , '06_network.sh'] # regenerate locale: 05_language.py wrote locale to /etc/locale.gen self.call_cmd('/sbin/locale-gen 2>&1 >> /tmp/installer.log', shell=True) # /etc/locale.gen should contain a string like "de_DE.UTF-8 UTF-8" locale = open('/etc/locale.gen','r').read().strip() if ' ' in locale: locale = locale.split(' ',1)[0] # write small helper to /tmp/ fd = open('/tmp/progress.lib','w') fd.write('export INSTALLERLOCALE=%s\n' % locale) fd.write('export TEXTDOMAIN="installer"\n') fd.write('export TEXTDOMAINDIR="/lib/univention-installer/locale"\n') fd.close() # send message to main loop and run 00_scripts.sh os.write(self.fd_write, '__MSG__:%s\n' % _('Preparing installation')) self.call_cmd(['/lib/univention-installer-scripts.d/00_scripts.sh']) # get list of scripts to be called scripts = [ os.path.join('/lib/univention-installer-scripts.d/', x) for x in os.listdir('/lib/univention-installer-scripts.d/') if x not in IGNORE_LIST ] scripts.sort() # accumulate all progress steps if "percentage" is not negative # negative "percentage" indicates main_script that gains the remainder to fill up to 100% cumulative_progress = 0.0 main_script = None for script in scripts: scriptname = os.path.basename(script) if not scriptname in self.script2progress: self.script2progress[scriptname] = ProgressInfo( percentage=self.default_percentage, steps=1 ) if self.script2progress[scriptname].percentage < 0: # main script has progress value lower than 0 main_script = self.script2progress[scriptname] # ==> save reference to this object self.log('main script == %s' % scriptname) else: # otherwise accumulate progress parts cumulative_progress += self.script2progress[scriptname].percentage self.log('script %s ==> %s%%' % (script, self.script2progress[scriptname].percentage)) if main_script: # assign remaining percentage to main script main_script.percentage = 100.0 - cumulative_progress main_script.remaining_percentage = main_script.percentage self.log('main_script ==> %s%%' % main_script.percentage) for script in scripts: self.log('script %s' % script) if os.path.exists('/tmp/logging'): # create installation.log on target system fn = '/instmnt/var/log/univention/installation.log' try: # create directory if missing if not os.path.isdir( os.path.dirname(fn) ): os.makedirs( os.path.dirname(fn) ) # create logfile and fix logfile permissions open(fn, 'a+') # create file / touch it gid_adm = grp.getgrnam('adm')[2] # get gid of group "adm" os.chown(fn, 0, gid_adm) # chown root:adm $fn os.chmod(fn, 0640) # chmod 0640 $fn except OSError, e: self.log('EXCEPTION occured while creating %s: %s' % (fn, str(e))) # switch to new logfile self.logfile = fn # copy installer.log to target system fn = '/instmnt/var/log/univention/installer.log' if os.path.exists('/tmp/installer.log') and not os.path.exists(fn): self.log('switching logfile') open(fn,'a+').write( open('/tmp/installer.log','r').read() ) # append old file to new file os.chown(fn, 0, gid_adm) # chown root:adm $fn os.chmod(fn, 0640) # chmod 0640 $fn # copy debootstrap.log to target system fnA = '/tmp/debootstrap.log.gz' fnB = '/instmnt/var/log/univention/debootstrap.log.gz' if os.path.exists(fnA): shutil.copyfile(fnA, fnB) os.chown(fnB, 0, gid_adm) os.chmod(fnB, 0640) # send script name to main loop os.write(self.fd_write, '\n__SCRIPT__:%s\n' % os.path.basename(script)) if os.path.exists('/tmp/logging'): self.call_cmd( '/bin/sh %s < /dev/tty1 2>&1 | tee -a /instmnt/var/log/univention/installation.log > /dev/tty6' % script, shell=True) else: self.call_cmd( '/bin/sh %s < /dev/tty1 2>&1 | tee -a /tmp/installer.log > /dev/tty6' % script, shell=True) self.log('script %s done' % script)
from distutils.version import LooseVersion, StrictVersion from subprocess import call from subprocess import check_output from os import listdir from os.path import isfile, join true = 1 false = 0 repoStable = "https://download.kde.org/stable/krita/" repoUnstable = "https://download.kde.org/unstable/krita/" priority = ["STABLE", "rc", "beta", "alpha", "prealpha"] localdir = os.path.dirname(sys.argv[0]) files = [f for f in listdir(localdir) if isfile(join(localdir, f))] locale = locale.getdefaultlocale()[0] language = locale.split("_")[0] versions = {} bestVersions = {} bestVersions["STABLE"] = "0.0.0" #get the appropriate string for the locale def loc(strings, fallback): if locale in strings: return strings[locale] if language in strings: return strings[language] return fallback