Locale settings

Bob van der Poel bvdpoel at kootenay.com
Mon Apr 21 20:22:07 CEST 2003


I've got a minor problem with locale settings and tkinter. My program 
has done a test to determine the current locale setting for the user:

loc = locale.getdefaultlocale()
if loc[1]:
   userEncoding = loc[1]
     try:
        ''.encode(userEncoding, 'replace')
     except:
        userEncoding = ''

if userEncoding == '':
   userEncoding='ascii'

Now, for the most part this works. However, one user of my program has 
just upgraded to Linux Mandrake 9.1 and (I think) he has set his 
language to "de". In any event, the getdefaultlocal() seems to return 
'de' which isn't a valid python encoding (??) and the above code then 
converts to his 'ascii'.

Strings entered into entry boxes, etc. are converted to a useable 
encoding with:

if type(s) != type(''):
   s=s.encode(userEncoding, 'replace')

But, with the 'ascii' encoding accented characters are converted to '?' 
(which is as expected). Now, on my system, with language set to 
'english' I get a locate setting of ISO8859-1. And this does display 
accented characters properly.

My question, after all this, is what to tell my German user as to the 
correct way to set his locale so that these characters display. Or, if 
my code is not correct, a proper manner to do all this. BTW, this is all 
done to convert the unicode strings sometimes returned by tk-entryboxes 
to normal, 8bit strings, which can be sorted, etc. ... avoiding the dreaded:
	
	UnicodeError: ASCII decoding error: ordinal not in range(128)

Thanks.


-- 
Bob van der Poel ** Wynndel, British Columbia, CANADA **
EMAIL: bvdpoel at kootenay.com
WWW:   http://www.kootenay.com/~bvdpoel






More information about the Python-list mailing list