• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/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
12LOCALE_ALIAS = '/usr/share/X11/locale/locale.alias'
13
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()
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
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
46            if encoding.lower() == 'utf8':
47                # Ignore UTF-8 mappings - this encoding should be
48                # available for all locales
49                continue
50        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
71
72if __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 '}'
80