2013-03-26 30 views
1

如何設置在OS X Mountain Lion中運行哪些應用程序的默認區域設置(LANG,LC_ALL = en_US.UTF-8)?如何在OSX Mountain Lion中設置應用程序包的默認區域設置

我想運行一個Python程序打包到.app與py2app。當通過在其上的雙擊,代碼段中運行該應用

import locale 
print locale.getlocale() 

返回(無,無)

然而,通過執行

opensesame.app/Contents/MacOS/opensesame 

所述的getLocale運行從終端應用程序時( )功能正確返回(en_US,UTF-8)
(由於終端具有正確的環境設置)

我到目前爲止因爲發現應用程序在雙擊時運行在不同於它們從終端運行時的環境中,所以現在我試圖設置應用程序的默認編碼變量。

我試圖找到我的建議: How can you get the system default language/locale, in a py2app packaged Python app on Mac OS X? 跟着上的指示:http://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPInternational/Articles/ChoosingLocalizations.html

我已經在應用程序中添加這兩種CFBundleLocalizationsLSEnvironment鍵的Info.plist(貼在下面)捆綁,但getlocale()函數保持回調(無,無)

任何人都可以告訴我爲OS X應用程序正確設置這些變量,以便Python選取這些變量嗎?

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>CFBundleDevelopmentRegion</key> 
    <string>English</string> 
    <key>CFBundleDisplayName</key> 
    <string>opensesame</string> 
    <key>CFBundleExecutable</key> 
    <string>opensesame</string> 
    <key>CFBundleIconFile</key> 
    <string>opensesame.icns</string> 
    <key>CFBundleIdentifier</key> 
    <string>org.pythonmac.unspecified.opensesame</string> 
    <key>CFBundleInfoDictionaryVersion</key> 
    <string>6.0</string> 
    <key>CFBundleName</key> 
    <string>opensesame</string> 
    <key>CFBundlePackageType</key> 
    <string>APPL</string> 
    <key>CFBundleShortVersionString</key> 
    <string>0.0.0</string> 
    <key>CFBundleSignature</key> 
    <string>????</string> 
    <key>CFBundleVersion</key> 
    <string>0.0.0</string> 
    <key>CFBundleLocalizations</key> 
    <string>en_US.UTF-8</string> 
    <key>LSHasLocalizedDisplayName</key> 
    <false/> 
    <key>NSAppleScriptEnabled</key> 
    <false/> 
    <key>NSHumanReadableCopyright</key> 
    <string>Copyright not specified</string> 
    <key>NSMainNibFile</key> 
    <string>MainMenu</string> 
    <key>NSPrincipalClass</key> 
    <string>NSApplication</string> 
    <key>LSEnvironment</key> 
    <dict> 
     <key>LANG</key> 
     <string>en_US.UTF-8</string> 
     <key>LC_ALL</key> 
     <string>en_US.UTF-8</string> 
    </dict> 
    <key>PyMainFileNames</key> 
    <array> 
     <string>__boot__</string> 
    </array> 
    <key>PyOptions</key> 
    <dict> 
     <key>alias</key> 
     <false/> 
     <key>argv_emulation</key> 
     <false/> 
     <key>emulate_shell_environment</key> 
     <false/> 
     <key>no_chdir</key> 
     <false/> 
     <key>prefer_ppc</key> 
     <false/> 
     <key>site_packages</key> 
     <false/> 
     <key>use_pythonpath</key> 
     <false/> 
    </dict> 
    <key>PyResourcePackages</key> 
    <array/> 
    <key>PyRuntimeLocations</key> 
    <array> 
     <string>@executable_path/../Frameworks/Python.framework/Versions/2.7/Python</string> 
    </array> 
    <key>PythonInfoDict</key> 
    <dict> 
     <key>PythonExecutable</key> 
     <string>@executable_path/../Frameworks/Python.framework/Versions/2.7/Python</string> 
     <key>PythonLongVersion</key> 
     <string>2.7.3 (default, Mar 19 2013, 18:26:22) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.27)]</string> 
     <key>PythonShortVersion</key> 
     <string>2.7</string> 
     <key>py2app</key> 
     <dict> 
      <key>alias</key> 
      <false/> 
      <key>template</key> 
      <string>app</string> 
      <key>version</key> 
      <string>0.7.3</string> 
     </dict> 
    </dict> 
</dict> 
</plist> 
+0

你使用哪個版本的py2app? LSEnvironment中的其他環境變量是否在os.environ中結束?最近版本的py2app在環境中覆蓋LC_ALL,以確保py3k正確初始化並且IIRC我不會在之後恢復以前的LC_ALL值。 – 2013-03-26 15:07:28

+0

我已經使用了0.7.3版本,我相信它是最新版本。 – 2013-03-26 15:18:13

回答

0

隨着py2app的0.7分支(這將是0.7.4不久的某個時候),我可以部分地重現該問題的尖端,在locale.getlocale()返回(無,無),而不管其LC_的*變量在環境中設置。

當我打電話locale.getlocale()我得到那個年代由LC_ALL或LANG環境變量(通過在信息的LSEnvironment鍵設置指定的區域設置信息之前調用locale.setlocale(locale.LC_AL,「」)。當雙擊應用程序時爲plist)。

+0

感謝您的建議!然而,調用 'locale.setlocale(locale.LC_ALL,「」)' 給了我錯誤 **錯誤:不支持的區域設置** – 2013-03-27 11:05:33

+0

這很奇怪。環境中存在哪些與語言環境相關的變量? – 2013-03-27 13:46:42

+0

在我的機器上(OSX 10.8),我總是在環境中獲得LC_CTYPE,值爲en_US.UTF-8。這是py2app在引導應用程序時使用的值,由於某些原因重置不起作用。 – 2013-03-27 14:07:48

相關問題