/Tools/i18n/makelocalealias.py

http://unladen-swallow.googlecode.com/ · Python · 73 lines · 49 code · 8 blank · 16 comment · 13 complexity · 9b64e56bead60b56c142a2434249808a MD5 · raw file

  1. #!/usr/bin/env python
  2. """
  3. Convert the X11 locale.alias file into a mapping dictionary suitable
  4. for locale.py.
  5. Written by Marc-Andre Lemburg <mal@genix.com>, 2004-12-10.
  6. """
  7. import locale
  8. # Location of the alias file
  9. LOCALE_ALIAS = '/usr/lib/X11/locale/locale.alias'
  10. def parse(filename):
  11. f = open(filename)
  12. lines = f.read().splitlines()
  13. data = {}
  14. for line in lines:
  15. line = line.strip()
  16. if not line:
  17. continue
  18. if line[:1] == '#':
  19. continue
  20. locale, alias = line.split()
  21. # Strip ':'
  22. if locale[-1] == ':':
  23. locale = locale[:-1]
  24. # Lower-case locale
  25. locale = locale.lower()
  26. # Ignore one letter locale mappings (except for 'c')
  27. if len(locale) == 1 and locale != 'c':
  28. continue
  29. # Normalize encoding, if given
  30. if '.' in locale:
  31. lang, encoding = locale.split('.')[:2]
  32. encoding = encoding.replace('-', '')
  33. encoding = encoding.replace('_', '')
  34. locale = lang + '.' + encoding
  35. if encoding.lower() == 'utf8':
  36. # Ignore UTF-8 mappings - this encoding should be
  37. # available for all locales
  38. continue
  39. data[locale] = alias
  40. return data
  41. def pprint(data):
  42. items = data.items()
  43. items.sort()
  44. for k,v in items:
  45. print ' %-40s%r,' % ('%r:' % k, v)
  46. def print_differences(data, olddata):
  47. items = olddata.items()
  48. items.sort()
  49. for k, v in items:
  50. if not data.has_key(k):
  51. print '# removed %r' % k
  52. elif olddata[k] != data[k]:
  53. print '# updated %r -> %r to %r' % \
  54. (k, olddata[k], data[k])
  55. # Additions are not mentioned
  56. if __name__ == '__main__':
  57. data = locale.locale_alias.copy()
  58. data.update(parse(LOCALE_ALIAS))
  59. print_differences(data, locale.locale_alias)
  60. print
  61. print 'locale_alias = {'
  62. pprint(data)
  63. print '}'