Home | History | Annotate | Download | only in i18n
      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 (at] genix.com>, 2004-12-10.
      7 
      8 """
      9 import locale
     10 
     11 # Location of the alias file

     12 LOCALE_ALIAS = '/usr/share/X11/locale/locale.alias'
     13 
     14 def 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         # Strip ':'

     27         if locale[-1] == ':':
     28             locale = locale[:-1]
     29         # Lower-case locale

     30         locale = locale.lower()
     31         # Ignore one letter locale mappings (except for 'c')

     32         if len(locale) == 1 and locale != 'c':
     33             continue
     34         # Normalize encoding, if given

     35         if '.' in locale:
     36             lang, encoding = locale.split('.')[:2]
     37             encoding = encoding.replace('-', '')
     38             encoding = encoding.replace('_', '')
     39             locale = lang + '.' + encoding
     40             if encoding.lower() == 'utf8':
     41                 # Ignore UTF-8 mappings - this encoding should be

     42                 # available for all locales

     43                 continue
     44         data[locale] = alias
     45     return data
     46 
     47 def pprint(data):
     48 
     49     items = data.items()
     50     items.sort()
     51     for k,v in items:
     52         print '    %-40s%r,' % ('%r:' % k, v)
     53 
     54 def print_differences(data, olddata):
     55 
     56     items = olddata.items()
     57     items.sort()
     58     for k, v in items:
     59         if not data.has_key(k):
     60             print '#    removed %r' % k
     61         elif olddata[k] != data[k]:
     62             print '#    updated %r -> %r to %r' % \
     63                   (k, olddata[k], data[k])
     64         # Additions are not mentioned

     65 
     66 if __name__ == '__main__':
     67     data = locale.locale_alias.copy()
     68     data.update(parse(LOCALE_ALIAS))
     69     print_differences(data, locale.locale_alias)
     70     print
     71     print 'locale_alias = {'
     72     pprint(data)
     73     print '}'
     74