#!/usr/bin/env python3"""Convert the X11 locale.alias file into a mapping dictionary suitablefor locale.py.Written by Marc-Andre Lemburg <mal@genix.com>, 2004年12月10日."""import localeimport sys_locale = locale# Location of the X11 alias file.LOCALE_ALIAS = '/usr/share/X11/locale/locale.alias'# Location of the glibc SUPPORTED locales file.SUPPORTED = '/usr/share/i18n/SUPPORTED'def parse(filename):with open(filename, encoding='latin1') as f:lines = list(f)# Remove mojibake in /usr/share/X11/locale/locale.alias.# b'\xef\xbf\xbd' == '\ufffd'.encode('utf-8')lines = [line for line in lines if '\xef\xbf\xbd' not in line]data = {}for line in lines:line = line.strip()if not line:continueif line[:1] == '#':continuelocale, alias = line.split()# Fix non-standard locale names, e.g. ks_IN@devanagari.UTF-8if '@' 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 localelocale = locale.lower()# Ignore one letter locale mappings (except for 'c')if len(locale) == 1 and locale != 'c':continue# Normalize encoding, if givenif '.' in locale:lang, encoding = locale.split('.')[:2]encoding = encoding.replace('-', '')encoding = encoding.replace('_', '')locale = lang + '.' + encodingdata[locale] = aliasreturn datadef 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:continueif line[:1] == '#':continueline = line.replace('/', ' ').strip()line = line.rstrip('\\').rstrip()words = line.split()if len(words) != 2:continuealias, alias_encoding = words# Lower-case localelocale = alias.lower()# Normalize encoding, if givenif '.' in locale:lang, encoding = locale.split('.')[:2]encoding = encoding.replace('-', '')encoding = encoding.replace('_', '')locale = lang + '.' + encoding# Add an encoding to aliasalias, _, modifier = alias.partition('@')alias = _locale._replace_encoding(alias, alias_encoding)if modifier and not (modifier == 'euro' and alias_encoding == 'ISO-8859-15'):alias += '@' + modifierdata[locale] = aliasreturn datadef pprint(data):items = sorted(data.items())for k, v in items:print(' %-40s%a,' % ('%a:' % k, v))def print_differences(data, olddata):items = sorted(olddata.items())for k, v in items:if k not in data:print('# removed %a' % k)elif olddata[k] != data[k]:print('# updated %a -> %a to %a' % \(k, olddata[k], data[k]))# Additions are not mentioneddef optimize(data):locale_alias = locale.locale_aliaslocale.locale_alias = data.copy()for k, v in data.items():del locale.locale_alias[k]if locale.normalize(k) != v:locale.locale_alias[k] = vnewdata = locale.locale_aliaserrors = check(data)locale.locale_alias = locale_aliasif errors:sys.exit(1)return newdatadef check(data):# Check that all alias definitions from the X11 file# are actually mapped to the correct alias locales.errors = 0for k, v in data.items():if locale.normalize(k) != v:print('ERROR: %a -> %a != %a' % (k, locale.normalize(k), v),file=sys.stderr)errors += 1return errorsif __name__ == '__main__':import argparseparser = argparse.ArgumentParser()parser.add_argument('--locale-alias', default=LOCALE_ALIAS,help='location of the X11 alias file ''(default: %a)' % LOCALE_ALIAS)parser.add_argument('--glibc-supported', default=SUPPORTED,help='location of the glibc SUPPORTED locales file ''(default: %a)' % SUPPORTED)args = parser.parse_args()data = locale.locale_alias.copy()data.update(parse_glibc_supported(args.glibc_supported))data.update(parse(args.locale_alias))while True:# Repeat optimization while the size is decreased.n = len(data)data = optimize(data)if len(data) == n:breakprint_differences(data, locale.locale_alias)print()print('locale_alias = {')pprint(data)print('}')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。