[Pythonmac-SIG] Setting up locale on Mac - how should this been done?

Barry Scott barry at barrys-emacs.org
Wed Oct 7 10:34:36 CEST 2009


On 4 Oct 2009, at 20:30, Ronald Oussoren wrote:

>
> On 4 Oct, 2009, at 19:53, Barry Scott wrote:
>
>> I have a wxPython application  (PySVN WorkBench) that is a bundle.
>>
>> When the app runs the environment does not contain any of the usual
>> variables that would be used to into the locale, LANG, LC_ALL etc.
>>
>> What I can see in the environment is __CF_USER_TEXT_ENCODING.
>> I have failed to find details of how to interpret the value of this  
>> environment
>> variable which leads me to believe I should not do processing it  
>> directly.
>>
>> The trick that works on windows does not work. Windows init:
>>
>> 	locale.setlocale( locale.LC_ALL, '' )
>>
>> $ python2.6
>> Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39)
>> [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
>> Type "help", "copyright", "credits" or "license" for more  
>> information.
>> >>> import locale
>> >>> locale.getdefaultlocale()
>> (None, 'mac-roman')
>> >>> locale.getlocale()
>> (None, None)
>> >>> locale.setlocale( locale.LC_ALL, '' )
>> 'C'
>> >>> locale.getlocale()
>> (None, None)
>> >>>
>>
>> The default locale of (None, 'mac-roman') is useless as it
>> does not say which locale the user has selected in
>> System Preferences, Language and Text.
>>
>> How should I init the locale to the users choice?
>
> That depends on the version of OSX you're running.
>
> On my system (10.6.1):
>
> >>> locale.setlocale(locale.LC_ALL, '')
> 'en_US.UTF-8'
> >>> locale.getlocale()
> ('en_US', 'UTF8')
> >>>

Look in your environment. You will see one of LANG, LANGUAGE, LC_ALL  
defined that setlocale is picking up on.
As I said when OS X runs python in an application bundle none of these  
environment variables are defined.

>
> This should also work in 10.5, but probably not 10.4.

>
> The value of "__CF_USER_TEXT_ENCODING" is undocumented, you  
> shouldn't rely on that value. The only reliable way to get at the  
> locale settings from System Preferences is by using mac-specific  
> API's from CoreFoundation or Cocoa.

Agreed. But it is info I can get from with in python.

What I'm surprised at is that setlocale on OS X does not call those  
Mac API's if the environment variables are not defined.
Especially given that python on windows does call windows APIs to find  
the language and set the locale.

>
> BTW. The usual approach for localizing an OSX app is to have  
> language-specific subbundles in the application bundle, for  
> examples, "Resources/English.lproj" for the english localization and  
> "Resources/nl.lproj" for the Dutch localization. Apple's APIs have  
> specific support for that structure, although I don't know if  
> wxWidgets exposes that.

I'll ask the wxPython folks how to do this.

Barry



More information about the Pythonmac-SIG mailing list