localization.py (622B)
1 import gettext 2 import locale 3 import logging 4 import os 5 6 def init_locale(loc=None): 7 locale.setlocale(locale.LC_ALL, '') # use user's preferred locale 8 # take first two characters of country code 9 if not loc: 10 loc = locale.getlocale() 11 else: 12 loc = [loc] 13 filename = os.path.dirname(__file__) + "/res/messages_%s.mo" % loc[0][0:2] 14 15 try: 16 logging.debug("Opening message file %s for locale %s", filename, loc[0]) 17 trans = gettext.GNUTranslations(open(filename, "rb")) 18 except IOError: 19 logging.debug("Locale not found. Using default messages") 20 trans = gettext.NullTranslations() 21 22 trans.install() 23