6

I'm running OS X Yosemite and Emacs 24.3 from Homebrew.

The locale setting for my user is:

LANG="en_AU.UTF-8"
LC_COLLATE="en_AU.UTF-8"
LC_CTYPE="en_AU.UTF-8"
LC_MESSAGES="en_AU.UTF-8"
LC_MONETARY="en_AU.UTF-8"
LC_NUMERIC="en_AU.UTF-8"
LC_TIME="en_AU.UTF-8"
LC_ALL=

Yet, when I launch Emacs.app via the launcher (or the OS X UI generally), and run locale in ansi-term, I get:

LANG=
LC_COLLATE="C"
LC_CTYPE="C"
LC_MESSAGES="C"
LC_MONETARY="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_ALL=

How can I ensure that I get my user's locale when launching Emacs.app via the OS X UI?

(Note: Launching Emacs.app from a shell does pickup the correct locale, but that is not the solution I'm looking for).

Andrew Swann
  • 3,436
  • 2
  • 15
  • 43
pwalsh
  • 245
  • 2
  • 6
  • You might look at [this question](http://apple.stackexchange.com/questions/64916/defining-environment-variables-with-launchd-launchctl) over at the Apple sister site, or you can search there for “environment variables” to get more information. – Harald Hanche-Olsen Apr 21 '15 at 14:00
  • 1
    Actually, I did look over there, to refresh my memory, and came away more confused than I was. The solution to this kind of problem seems to vary, depending on which OS version you're on. But in any case, I think your question fits better on http://apple.stackexchange.com/ than here. – Harald Hanche-Olsen Apr 21 '15 at 14:48
  • I understand your point about it being an OS X issue, but I def. disagree that the question should be on apple.x and not here. The accepted answer will be highly relevant to emacs users on OS X, and the context here fits better. – pwalsh Apr 21 '15 at 16:11
  • Okay. But note that I posted my comment before the answer appeared. Clearly, the existence of this answer changes things. – Harald Hanche-Olsen Apr 21 '15 at 17:59
  • In the upcoming Emacs 25 release, the `LANG` variable should be set according to the selected locale. – Lindydancer Feb 19 '16 at 15:04

3 Answers3

4

A nice package that helps with setting up the environment is exec-path-from-shell (at MELPA or https://github.com/purcell/exec-path-from-shell). As the name suggests it sets the PATH variable to a more useful value, but it is also able to set any number of environment variables. I use it like this:

(when (and (memq window-system '(mac ns))
           (require 'exec-path-from-shell nil t))
  ;; (setq exec-path-from-shell-debug t)
  (exec-path-from-shell-initialize)
  (exec-path-from-shell-copy-envs '("LANG" "GPG_AGENT_INFO" "SSH_AUTH_SOCK"))
  (message "Initialized PATH and other variables from SHELL."))

With these lines I get the same locale environment as in my plain terminal sessions. A more simple variant of the above, that will throw errors if the library is not available is:

(require 'exec-path-from-shell)
(exec-path-from-shell-initialize)
(exec-path-from-shell-copy-envs '("LANG" "GPG_AGENT_INFO" "SSH_AUTH_SOCK")))

In order to check if an environment variable has found its way inside Emacs, call the function getenv via M-x.

EDIT: As has been mentioned in the comments, you should double check that your environment variables, that should be imported by Emacs, are really available in a default shell. This can be check via env (in the shell or terminal). If the variable is missing in the env output, it has to be exported from your shells init script (e.g. ~/.bash_login or ~/.bashrc).

Stefan Nobis
  • 156
  • 4
  • I prematurely accepted this answer. When I add this to my init.el, it does not work for $LANG, (nor any other LC vars). It does work, for example, with $SSH_AUTH_SOCK – pwalsh Apr 22 '15 at 08:25
  • The above code only works, if exec-path-from-shell could be loaded. Did you check that this library is availabe (e.g. put a `(message "exec-path lib loaded")` inside the when). You should also check from within emacs for LANG. Try `M-x` `getenv` `RET` `LANG`. – Stefan Nobis Apr 22 '15 at 08:43
  • Thanks for the (message) tip. Yes, I get the message, and I knew also that it was loaded as some other custom environment variables that I set are available. But, LANG is still empty (verified using `M-x` `getenv` `RET` `LANG`) and running `locale` in an `ansi-term` buffer. – pwalsh Apr 22 '15 at 10:28
  • Is LANG really set in you default environment? Check `SHELL` via `getenv` from Emacs to see the shell that is used to expand environment variables. Also try to open the default MacOS Terminal app (with the default shell) and verify that LANG is set in this context. What does `env` called in your default SHELL say? – Stefan Nobis Apr 22 '15 at 10:38
  • Sure, LANG is set in my default environment. it is `en_AU.UTF-8` as indicated in first post. I can verify this via `locale`, as noted above, and also via `echo $LANG` - both in terminal.app. `SHELL` is /bin/bash, verified using `getenv` and terminal of course – pwalsh Apr 22 '15 at 10:53
  • I just double checked that a plain Emacs.app without init.el on my system has not LANG environment variable set. If I use a stripped down init.el with only extending the load-path, `(require exec-path-from-shell)` and `(exec-path-from-shell-copy-envs '("LANG"))`, then LANG is set inside of Emacs. You could better see what exec-path-from-shell does by enabling its debug outputs via `(setq exec-path-from-shell-debug t)` (after that execute the `exec-path-from-shell-copy-envs` line and have a look at the \*Messages\* buffer). – Stefan Nobis Apr 22 '15 at 10:55
  • @pwalsh Can you do in your terminal (not inside of emacs) `env | grep LANG` if it doesn't appear then it might not be set. I have the following set on my .bashrc `export LANG=en_US.UTF-8 export LC="$LANG" export LC_ALL="$LANG"` I don't use exec-path-from-shell. As far as I understand ansi-term like a normal terminal, loads from .bashrc and .bash_profile. – xmonk Apr 22 '15 at 11:01
  • @xmonk ok that is great, **now** I have it working: Stefan's instructions are correction, but, unless I **explicitly** set the LANG/LC variables myself in .bashrc/.bash_profile, then those settings will not be picked up. – pwalsh Apr 22 '15 at 11:04
  • @StefanNobis I'd like to accept your answer, as it is technically correct. The problem is that the step that xmonk has added is required: the LANG and LC vars that **are** set and visible in Terminal, are **not** accessible to Emacs - rather, they **must** be explicitly defined in .bashrc or similar. – pwalsh Apr 22 '15 at 11:10
4

Terminal.app and iTerm 2 set LANG based on the region selected in System Preferences (defaults read -g AppleLocale) by default unless the "Set locale environment variables on startup" setting is unchecked in Terminal or the "Set locale variables automatically" setting is unchecked in iTerm 2.

This changes LANG in Emacs:

(setenv "LANG" "en_US.UTF-8")

You can also add

export LANG=en_US.UTF-8

to a shell configuration file, but that alone won't for example affect shell-command or shell-command-on-region or shell scripts you run from Emacs.

nisetama
  • 241
  • 2
  • 4
1

You may also try something like

(set-locale-environment "en_AU")

in your .emacs ini file