GrampsLocale: Restore setlocale, add GrampsLocale.encoding

encoding to replace translation().info()['encoding'] which isn't 
the encoding we're looking for. 

svn: r21539
This commit is contained in:
John Ralls 2013-03-03 21:55:02 +00:00
parent 46d7c95f86
commit 649bdf7bb7
3 changed files with 19 additions and 9 deletions

View File

@ -42,11 +42,7 @@ if sys.version_info[0] < 3:
else:
#locale returns unicode in python 3
to_uni = lambda x, y: x
try:
codeset = glocale.get_translation().info()["charset"]
except KeyError:
codeset = "UTF-8"
codeset = glocale.encoding
try:
month_to_int = {

View File

@ -284,10 +284,7 @@ def fix_encoding(value, errors='strict'):
try:
return cuni(value)
except:
try:
info = glocale.get_translation().info()["charset"]
except:
codeset = "UTF-8"
codeset = glocale.encoding
if sys.version_info[0] < 3:
return unicode(value, codeset, errors)
else:

View File

@ -102,6 +102,9 @@ class GrampsLocale(object):
return super(GrampsLocale, cls).__new__(cls)
def __init_from_environment(self, lang=None, language=None):
#First, globally set the locale to what's in the environment:
locale.setlocale(locale.LC_ALL, '')
if not lang:
lang = ' '
try:
@ -184,6 +187,20 @@ class GrampsLocale(object):
maclocale.mac_setup_localization(self, lang, language)
else:
self.__init_from_environment(lang, language)
#A variety of useful functions use the current locale for
#formatting. Pending global replacement of those functions with ICU
#equivalents, we need to use setlocale to our chosen default. This
#unfortunately doesn't work in Windows because it uses different
#values until VS2012 (which only works on Win8), so while we can set
#translations and date formats with lang, we can't affect currency or
#numeric format. Those are fixed by the user's system settings.
if not win():
locale.setlocale(locale.LC_COLLATE, self.collation)
locale.setlocale(locale.LC_TIME, self.calendar)
locale.setlocale(locale.LC_MONETARY, self.currency)
#Next, we need to know what is the encoding from the native environment:
self.encoding = locale.getlocale()[1]
#GtkBuilder depends on reading Glade files as UTF-8 and crashes if it
#doesn't, so set $LANG to have a UTF-8 locale. NB: This does *not*