问题描述
我需要获取系统区域设置才能执行许多操作,最终我想使用gettext转换我的应用程序.我将同时在Linux和OSX上分发它,但是在OSX Snow Leopard上遇到了问题:
I need to get the system locale to do a number of things, ultimately I want to translate my app using gettext. I am going to distribute it on both Linux and OSX, but I ran into problems on OSX Snow Leopard:
$ python
Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
'sv_SE.UTF-8'
>>> locale.getlocale()
('sv_SE', 'UTF8')
$ python
Python 2.6.1 (r261:67515, Jul 7 2009, 23:51:51)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, '')
'C'
>>> locale.getlocale()
(None, None)
两个系统都使用瑞典语.在Linux上,环境变量LANG已设置为"sv_SE.UTF-8".如果我将该变量传递给OSX上的python(改为LANG="sv_SE.UTF-8" python
),则可以很好地检测到语言环境.但是locale.getlocale()
是否不应该能够获取操作系统使用的任何语言?我根本不想强迫用户设置LANG
,LC_ALL
或任何环境变量.
Both systems are using Swedish languages. On Linux, the environment variable LANG is already set to "sv_SE.UTF-8". If I pass that variable to python on OSX (LANG="sv_SE.UTF-8" python
instead), locale is detected nicely. But shouldn't locale.getlocale()
be able to fetch whatever language the operating system has? I don't want to force users to set LANG
, LC_ALL
or any environment variable at all.
locale
命令的输出:
$ locale
LANG=
LC_COLLATE="C"
LC_CTYPE="C"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL=
推荐答案
在OSX上奇怪(Smow Leopard 10.6.1)
Odd on OSX (Smow Leopard 10.6.1) I get
$ python
Python 2.6.1 (r261:67515, Jul 7 2009, 23:51:51)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.getlocale()
(None, None)
>>> locale.setlocale(locale.LC_ALL, '')
'en_GB.UTF-8'
>>> locale.getlocale()
('en_GB', 'UTF8')
我刚刚在 apple python邮件列表中找到了这个
I just found this on the apple python mailing list
基本上,这取决于运行时环境中设置的内容(LANG,LANGUAGE和LC_ALL中的一个),我的Shell环境中有LANG = en_GB.UTF-8
Basically it depends on what is set in your environment at run time (one of LANG, LANGUAGE, LC_ALL) I had LANG=en_GB.UTF-8 in my shell environment
这篇关于OSX上的locale.getlocale()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!