Setting environment variables

Issue #109 closed
Ronald Oussoren
repo owner created an issue

See http://stackoverflow.com/questions/15639218/how-to-set-default-locale-for-app-bundles-in-osx-mountain-lion

1) It should be possible to set LANG/LC_* variables through Info.plist, but that currently doesn't work.

2) It may be useful to have a py2app option for this, although explicitly updating info.plist isn't too bad.

See also http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/LaunchServicesKeys.html

Comments (5)

  1. Ronald Oussoren reporter

    Duh, setting LC_CTYPE won't work because py2app sets LC_CTYPE to "en_US.UTF-8" during Py_Initialize and that means os.environment will contain that value instead of the one mentioned in the environment.

  2. Ronald Oussoren reporter

    You can use the key LSEnvironment in the 'Info.plist' file for this.

    Adding this can be done using the setup.py file, for example:

    ... # other setup.py code
    
    setup(
       options=dict(
         py2app=dict(
           plist=dict(
             LSEnvironment=dict(
                LANG="en_US.UTF-8"
             )
           )
         )
       )
    )
    

    You cannot override LC_CTYPE with this because py2app overrides that when initialising Python.

    BTW. A better way is to actually get the locale from the system using an Apple API, for example using PyObjC:

    from Foundation import NSUserDefaults
    
    defaults = NSUserDefaults.standardUserDefaults()
    globalDomain = defaults.persistentDomainForName_("NSGlobalDomain")
    languages = globalDomain.objectForKey_("AppleLanguages")
    

    On my system this returns:

    >>> print(languages)
    (
        "en-NL",
        "nl-NL"
    )
    

    See also "https://developer.apple.com/library/content/qa/qa1391/_index.html".

    The advantage of getting the language information using the Apple API is that you can pick the first language from the list that you have a translation for.

  3. Log in to comment