1

如何设置在 OS X Mountain Lion 中运行应用程序的默认语言环境(LANG,LC_ALL = en_US.UTF-8)?

我正在尝试使用 py2app 运行打包到 .app 的 Python 程序。通过双击运行应用程序时,代码段

import locale
print locale.getlocale()

返回(无,无)

但是,当通过执行从终端运行应用程序时

opensesame.app/Contents/MacOS/opensesame

getlocale() 函数正确返回 (en_US,UTF-8)
(因为终端具有正确的环境设置)

我发现双击时应用程序在与从终端运行时不同的环境中运行,所以现在我正在尝试为应用程序设置默认编码变量。

我已经尝试了我发现的建议: 如何在 Mac OS X 上的 py2app 打包 Python 应用程序中获取系统默认语言/区域设置? 并按照以下说明操作: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>
4

1 回答 1

0

使用 py2app 的 0.7 分支的尖端(不久将是 0.7.4),我可以部分重现该问​​题,因为 locale.getlocale() 无论环境中设置了哪些 LC_* 变量,都会返回 (None, None) .

当我在调用 locale.getlocale() 之前调用 locale.setlocale(locale.LC_AL, "") 时,我确实获得了由 LC_ALL 或 LANG 环境变量指示的语言环境信息(通过 Info.plist 中的 LSEnvironment 键设置,当 double-点击应用程序)。

于 2013-03-26T15:35:00.297 回答