def __init__(self, localespec): """ Constructor for the LocaleInfo class. :param localespec: locale string such as "sr_RS.UTF-8@latin" (glibc-like format) or "sr_Latn_RS" (CLDR-format) :type localespec: str """ # get the basic values parsed_values = parse_langcode(localespec) if parsed_values: # successfully parsed -> a glibc-like locale, try to get some more # information by querying langtable # language must be specified language = parsed_values["language"] territory = parsed_values.get("territory", "") script = parsed_values.get("modifier", "") locales = langtable.list_locales(languageId=language, territoryId=territory, scriptId=script) else: # not in the glibc-like format, use langtable to get it locales = langtable.list_locales(languageId=localespec) if not locales: msg = "'%s' is not a valid locale" % localespec raise InvalidLocaleSpec(msg) # locales are sorted from the most probable to the least probable self._locale = locales[0] parsed_values = parse_langcode(self._locale) # language must be specified self._language = parsed_values["language"] # try to get the other values self._territory = parsed_values.get("territory", "") self._encoding = parsed_values.get("codeset", "") self._script = parsed_values.get("modifier", "") # TODO: never appeared so far, needs to be parsed and stored? self._variant = None
def get_territory_locales(territory): """Function returning list of locales for the given territory. The list is sorted from the most probable locale to the least probable one (based on langtable's ranking. :param territory: territory to return locales for :type territory: str :return: list of locales :rtype: list of strings """ return langtable.list_locales(territoryId=territory)
def get_language_locales(lang): """Function returning all locales available for the given language. :param lang: language to get available locales for :type lang: str :return: a list of available locales :rtype: list of strings :raise InvalidLocaleSpec: if an invalid locale is given (see is_valid_langcode) """ raise_on_invalid_locale(lang) return langtable.list_locales(languageId=lang)
def main(argv=None): if argv is None: argv = sys.argv parser = argparse.ArgumentParser("Check for duplicated accelerators") parser.add_argument("-t", "--translate", action='store_true', help="Check translated strings") parser.add_argument("-p", "--podir", action='store', type=str, metavar='PODIR', help='Directory containing po files', default='./po') parser.add_argument("glade_files", nargs="+", metavar="GLADE-FILE", help='The glade file to check') args = parser.parse_args(args=argv) # First check the untranslated strings in each file for glade_file in args.glade_files: check_glade(glade_file) # Now loop over all of the translations if args.translate: import langtable with open(os.path.join(args.podir, 'LINGUAS')) as linguas: for line in linguas.readlines(): if re.match(r'^#', line): continue for lang in line.strip().split(" "): # Reset the locale to C before parsing the po file because # polib has erroneous uses of lower(). # See https://bitbucket.org/izi/polib/issue/54/pofile-parsing-crashes-in-turkish-locale locale.setlocale(locale.LC_ALL, 'C') po_map = PODict(os.path.join(args.podir, lang + ".po")) # Set the locale so that we can use lower() on accelerator keys. # If the language is of the form xx_XX, use that as the # locale name. Otherwise use the first locale that # langtable returns for the language. If that doesn't work, # just use C and hope for the best. if '_' in lang: locale.setlocale(locale.LC_ALL, lang) else: locale_list = langtable.list_locales(languageId=lang) if locale_list: try: locale.setlocale(locale.LC_ALL, locale_list[0]) except locale.Error: print("No such locale %s, using C" % locale_list[0]) locale.setlocale(locale.LC_ALL, 'C') else: locale.setlocale(locale.LC_ALL, 'C') for glade_file in args.glade_files: check_glade(glade_file, po_map)
def main(argv=None): if argv is None: argv = sys.argv parser = argparse.ArgumentParser("Check for duplicated accelerators") parser.add_argument("-t", "--translate", action='store_true', help="Check translated strings") parser.add_argument("-p", "--podir", action='store', type=str, metavar='PODIR', help='Directory containing po files', default='./po') parser.add_argument("glade_files", nargs="+", metavar="GLADE-FILE", help='The glade file to check') args = parser.parse_args(args=argv) # First check the untranslated strings in each file for glade_file in args.glade_files: check_glade(glade_file) # Now loop over all of the translations if args.translate: import langtable podicts = translate_all(args.podir) for (lang, po_map) in ((key, podicts[key]) for key in podicts.keys()): # Set the locale so that we can use lower() on accelerator keys. # If the language is of the form xx_XX, use that as the # locale name. Otherwise use the first locale that # langtable returns for the language. If that doesn't work, # just use C and hope for the best. if '_' in lang: locale.setlocale(locale.LC_ALL, lang) else: locale_list = langtable.list_locales(languageId=lang) if locale_list: try: locale.setlocale(locale.LC_ALL, locale_list[0]) except locale.Error: print("No such locale %s, using C" % locale_list[0]) locale.setlocale(locale.LC_ALL, 'C') else: locale.setlocale(locale.LC_ALL, 'C') for glade_file in args.glade_files: check_glade(glade_file, po_map)
def get_territory_locales(territory): """ Function returning list of locales for the given territory. The list is sorted from the most probable locale to the least probable one (based on langtable's ranking. :param territory: territory to return locales for :type territory: str :return: list of locales :rtype: list of strings """ return langtable.list_locales(territoryId=territory)
def get_language_locales(lang): """ Function returning all locales available for the given language. :param lang: language to get available locales for :type lang: str :return: a list of available locales :rtype: list of strings :raise InvalidLocaleSpec: if an invalid locale is given (see LANGCODE_RE) """ parts = parse_langcode(lang) if "language" not in parts: raise InvalidLocaleSpec("'%s' is not a valid language" % lang) return langtable.list_locales(languageId=parts["language"], territoryId=parts.get("territory", ""), scriptId=parts.get("script", ""))
'iu', 'ja', 'jv', 'kl', 'kn', 'kr', 'ks', 'kk', 'km', 'ki', 'rw', 'ky', 'kv', 'kg', 'ko', 'ku', 'kj', 'la', 'lb', 'lg', 'li', 'ln', 'lo', 'lt', 'lu', 'lv', 'gv', 'mk', 'mg', 'ms', 'ml', 'mt', 'mi', 'mr', 'mh', 'mn', 'na', 'nv', 'nd', 'ne', 'ng', 'nb', 'nn', 'no', 'ii', 'nr', 'oc', 'oj', 'cu', 'om', 'or', 'os', 'pa', 'pi', 'fa', 'pl', 'ps', 'pt', 'qu', 'rm', 'rn', 'ro', 'ru', 'sa', 'sc', 'sd', 'se', 'sm', 'sg', 'sr', 'gd', 'sn', 'si', 'sk', 'sl', 'so', 'st', 'es', 'su', 'sw', 'ss', 'sv', 'ta', 'te', 'tg', 'th', 'ti', 'bo', 'tk', 'tl', 'tn', 'to', 'tr', 'ts', 'tt', 'tw', 'ty', 'ug', 'uk', 'ur', 'uz', 've', 'vi', 'vo', 'wa', 'cy', 'wo', 'fy', 'xh', 'yi', 'yo', 'za', 'zu' ] language_code_to_locale = {} locale_set = set() for language_code in language_codes: locales = langtable.list_locales(languageId=language_code) if len(locales) > 0: language_code_to_locale[language_code] = locales[0] for locale in locales: locale_set.add(locale) else: long_language_code = language_code + '_' + language_code.upper() locales = langtable.list_locales(languageId=long_language_code) if len(locales) > 0: language_code_to_locale[language_code] = locales[0] for locale in locales: locale_set.add(locale) else: if GnomeDesktop.language_has_translations(language_code): print("Can't convert country code '{}' to locale".format( language_code))