[r21539]GrampsLocale: Restore setlocale, add GrampsLocale.encoding

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

svn: r21542
This commit is contained in:
John Ralls 2013-03-03 23:59:39 +00:00
parent debf8b601a
commit 1d2b143be9
3 changed files with 19 additions and 11 deletions

View File

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

View File

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

View File

@ -102,6 +102,9 @@ class GrampsLocale(object):
return super(GrampsLocale, cls).__new__(cls) return super(GrampsLocale, cls).__new__(cls)
def __init_from_environment(self, lang=None, language=None): 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: if not lang:
lang = ' ' lang = ' '
try: try:
@ -184,6 +187,20 @@ class GrampsLocale(object):
maclocale.mac_setup_localization(self, lang, language) maclocale.mac_setup_localization(self, lang, language)
else: else:
self.__init_from_environment(lang, language) 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 #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* #doesn't, so set $LANG to have a UTF-8 locale. NB: This does *not*