Created
September 15, 2011 12:18
-
-
Save supernifty/1219096 to your computer and use it in GitHub Desktop.
Python localization
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gettext | |
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment