Set locale for Python on Openshift
Environment
- Python 2.6
Issue
- Here's my Django views function. I want the locale to be set to UTF-8. How to I make local.getdefaultlocale() return a UTF-8 local?
from django.http import HttpResponse
import os, sys
import locale as loc
def locale(request):
s = ""
if 'LANG' in os.environ:
s += '<p>LANG=%s' % os.environ['LANG']
else:
s += '<p>LANG is not set'
if 'LC_ALL' in os.environ:
s += '<p>LC_ALL=%s' % os.environ['LC_ALL']
else:
s += '<p>LC_ALL is not set'
s += "<p>sys.getfilesystemencoding()=%s" % sys.getfilesystemencoding()
s += "<p>locale.getdefaultlocale()=%s" % str(loc.getdefaultlocale())
return HttpResponse(s)
- This returns the following information:
LANG is not set
LC_ALL is not set
sys.getfilesystemencoding()=ANSI_X3.4-1968
locale.getdefaultlocale()=(None, None)
Resolution
- Export LANG in pre_start_python hook. rerun the 3 prior commands given.
# echo $LANG
# python -c "import locale; print locale.getdefaultlocale()"
# python -c "import os; print os.environ['LANG']"
- The application is seeing something different this would be an indication that the application is changing the LANG context as part of the application code.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.
Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.
