public
Created

Python localization

  • Download Gist
gistfile1.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
import locale
import logging
 
def init_localization():
'''prepare l10n'''
locale.setlocale(locale.LC_ALL, '') # use user's preferred locale
# take first two characters of country code
loc = locale.getlocale()
filename = "res/messages_%s.mo" % locale.getlocale()[0][0:2]
 
try:
logging.debug( "Opening message file %s for locale %s", filename, loc[0] )
trans = gettext.GNUTranslations(open( filename, "rb" ) )
except IOError:
logging.debug( "Locale not found. Using default messages" )
trans = gettext.NullTranslations()
 
trans.install()
 
if __name__ == '__main__':
init_localization()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.