blob: d75892b73b809451198b68c22382646661b4054b [file] [log] [blame]
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +00001#!/usr/bin/env python
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"""
9import locale
10
11# Location of the alias file
Antoine Pitroufc531532010-04-11 22:32:39 +000012LOCALE_ALIAS = '/usr/share/X11/locale/locale.alias'
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000013
14def parse(filename):
15
16 f = open(filename)
17 lines = f.read().splitlines()
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 Storchakad551b282013-12-26 21:20:46 +020026 # 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é Lemburgbb4f1bd2004-12-10 21:58:14 +000032 # 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é Lemburgb4cebd42004-12-13 19:56:01 +000046 if encoding.lower() == 'utf8':
47 # Ignore UTF-8 mappings - this encoding should be
48 # available for all locales
49 continue
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000050 data[locale] = alias
51 return data
52
53def pprint(data):
54
55 items = data.items()
56 items.sort()
57 for k,v in items:
58 print ' %-40s%r,' % ('%r:' % k, v)
59
60def print_differences(data, olddata):
61
62 items = olddata.items()
63 items.sort()
64 for k, v in items:
65 if not data.has_key(k):
66 print '# removed %r' % k
67 elif olddata[k] != data[k]:
68 print '# updated %r -> %r to %r' % \
69 (k, olddata[k], data[k])
70 # Additions are not mentioned
Tim Peters5a9fb3c2005-01-07 16:01:32 +000071
Marc-André Lemburgbb4f1bd2004-12-10 21:58:14 +000072if __name__ == '__main__':
73 data = locale.locale_alias.copy()
74 data.update(parse(LOCALE_ALIAS))
75 print_differences(data, locale.locale_alias)
76 print
77 print 'locale_alias = {'
78 pprint(data)
79 print '}'