5

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).

  • You might look at this question 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 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
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).

  • 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 8: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 8: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
3

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.

  • Then what do I have to do in order to change LANG or LC_ALL for shell-command ? – Philipp Imhof Oct 29 at 10:19
  • I tried (unless (getenv "LANG") (setenv "LANG" "en_US.UTF-8")) in my .emacs file and it works. – Philipp Imhof Oct 29 at 10:27
0

You may also try something like

(set-locale-environment "en_AU")

in your .emacs ini file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.