| Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| Marc-André Lemburg | bb4f1bd | 2004-12-10 21:58:14 +0000 | [diff] [blame] | 2 | """ |
| 3 | Convert the X11 locale.alias file into a mapping dictionary suitable |
| 4 | for locale.py. |
| 5 | |
| 6 | Written by Marc-Andre Lemburg <mal@genix.com>, 2004-12-10. |
| 7 | |
| 8 | """ |
| 9 | import locale |
| 10 | |
| 11 | # Location of the alias file |
| Antoine Pitrou | 0c70d2d | 2010-04-11 22:35:34 +0000 | [diff] [blame] | 12 | LOCALE_ALIAS = '/usr/share/X11/locale/locale.alias' |
| Marc-André Lemburg | bb4f1bd | 2004-12-10 21:58:14 +0000 | [diff] [blame] | 13 | |
| 14 | def parse(filename): |
| 15 | |
| Serhiy Storchaka | 55c6cc4 | 2013-12-23 18:56:08 +0200 | [diff] [blame] | 16 | with open(filename, encoding='latin1') as f: |
| 17 | lines = list(f) |
| Marc-André Lemburg | bb4f1bd | 2004-12-10 21:58:14 +0000 | [diff] [blame] | 18 | data = {} |
| 19 | for line in lines: |
| 20 | line = line.strip() |
| 21 | if not line: |
| 22 | continue |
| 23 | if line[:1] == '#': |
| 24 | continue |
| 25 | locale, alias = line.split() |
| Serhiy Storchaka | 5eb0153 | 2013-12-26 21:20:59 +0200 | [diff] [blame] | 26 | # Fix non-standard locale names, e.g. ks_IN@devanagari.UTF-8 |
| 27 | if '@' in alias: |
| 28 | alias_lang, _, alias_mod = alias.partition('@') |
| 29 | if '.' in alias_mod: |
| 30 | alias_mod, _, alias_enc = alias_mod.partition('.') |
| 31 | alias = alias_lang + '.' + alias_enc + '@' + alias_mod |
| Marc-André Lemburg | bb4f1bd | 2004-12-10 21:58:14 +0000 | [diff] [blame] | 32 | # Strip ':' |
| 33 | if locale[-1] == ':': |
| 34 | locale = locale[:-1] |
| 35 | # Lower-case locale |
| 36 | locale = locale.lower() |
| 37 | # Ignore one letter locale mappings (except for 'c') |
| 38 | if len(locale) == 1 and locale != 'c': |
| 39 | continue |
| 40 | # Normalize encoding, if given |
| 41 | if '.' in locale: |
| 42 | lang, encoding = locale.split('.')[:2] |
| 43 | encoding = encoding.replace('-', '') |
| 44 | encoding = encoding.replace('_', '') |
| 45 | locale = lang + '.' + encoding |
| Marc-André Lemburg | b4cebd4 | 2004-12-13 19:56:01 +0000 | [diff] [blame] | 46 | if encoding.lower() == 'utf8': |
| 47 | # Ignore UTF-8 mappings - this encoding should be |
| 48 | # available for all locales |
| 49 | continue |
| Marc-André Lemburg | bb4f1bd | 2004-12-10 21:58:14 +0000 | [diff] [blame] | 50 | data[locale] = alias |
| 51 | return data |
| 52 | |
| 53 | def pprint(data): |
| Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 54 | items = sorted(data.items()) |
| 55 | for k, v in items: |
| Serhiy Storchaka | 55c6cc4 | 2013-12-23 18:56:08 +0200 | [diff] [blame] | 56 | print(' %-40s%a,' % ('%a:' % k, v)) |
| Marc-André Lemburg | bb4f1bd | 2004-12-10 21:58:14 +0000 | [diff] [blame] | 57 | |
| 58 | def print_differences(data, olddata): |
| Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 59 | items = sorted(olddata.items()) |
| Marc-André Lemburg | bb4f1bd | 2004-12-10 21:58:14 +0000 | [diff] [blame] | 60 | for k, v in items: |
| Georg Brandl | bf82e37 | 2008-05-16 17:02:34 +0000 | [diff] [blame] | 61 | if k not in data: |
| Serhiy Storchaka | 55c6cc4 | 2013-12-23 18:56:08 +0200 | [diff] [blame] | 62 | print('# removed %a' % k) |
| Marc-André Lemburg | bb4f1bd | 2004-12-10 21:58:14 +0000 | [diff] [blame] | 63 | elif olddata[k] != data[k]: |
| Serhiy Storchaka | 55c6cc4 | 2013-12-23 18:56:08 +0200 | [diff] [blame] | 64 | print('# updated %a -> %a to %a' % \ |
| Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 65 | (k, olddata[k], data[k])) |
| Marc-André Lemburg | bb4f1bd | 2004-12-10 21:58:14 +0000 | [diff] [blame] | 66 | # Additions are not mentioned |
| Tim Peters | 5a9fb3c | 2005-01-07 16:01:32 +0000 | [diff] [blame] | 67 | |
| Marc-André Lemburg | bb4f1bd | 2004-12-10 21:58:14 +0000 | [diff] [blame] | 68 | if __name__ == '__main__': |
| 69 | data = locale.locale_alias.copy() |
| 70 | data.update(parse(LOCALE_ALIAS)) |
| 71 | print_differences(data, locale.locale_alias) |
| Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 72 | print() |
| 73 | print('locale_alias = {') |
| Marc-André Lemburg | bb4f1bd | 2004-12-10 21:58:14 +0000 | [diff] [blame] | 74 | pprint(data) |
| Collin Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 75 | print('}') |