1

Is there a single command I can use to get the language to use for messages or do I have to check LC_MESSAGES, LC_ALL, LANG etc. in some specific order?

1 Answers1

1

You can get the locale information with:

$ locale
LANG=en_us.UTF-8
LANGUAGE=
LC_CTYPE="en_us.UTF-8"
LC_NUMERIC="en_us.UTF-8"
LC_TIME="en_us.UTF-8"
LC_COLLATE="en_us.UTF-8"
LC_MONETARY="en_us.UTF-8"
LC_MESSAGES="en_us.UTF-8"
LC_PAPER="en_us.UTF-8"
LC_NAME="en_us.UTF-8"
LC_ADDRESS="en_us.UTF-8"
LC_TELEPHONE="en_us.UTF-8"
LC_MEASUREMENT="en_us.UTF-8"
LC_IDENTIFICATION="en_us.UTF-8"
LC_ALL=

The relevant variable for your concern would then be $LC_MESSAGES:

   LC_MESSAGES
           Formats of informative and diagnostic messages and
           interactive responses.

In a sctipt you could source that output to have those environment variables available:

$ source <(locale)
chaos
  • 48,171
  • OK, thanks. Now I realize that LC_MESSAGES gets a value even if a user has only defined for instance LC_ALL. – August Karlstrom Apr 29 '15 at 06:17
  • @August Karlstrom: It doesn't; try echo $LC_MESSAGES. But things work as if it did. – lcd047 Apr 29 '15 at 06:37
  • @lcd047 I see, but does the locale command always output LC_MESSAGES? In that case I can get the language with locale | grep LC_MESSAGES | awk -F'=' '{ print substr($2, 1, 2) }'. – August Karlstrom Apr 29 '15 at 06:43
  • 1
    The easier way to do it is mentioned in the answer above: source <(locale). This will actually define LC_MESSAGE and friends in the environment. But yes, you can also get it from the output of locale, perhaps like this: locale | sed -n '/^LC_MESSAGES/{ s/^.*=//; s/"//g; p }'. – lcd047 Apr 29 '15 at 06:57
  • @lcd047 I already have LC_MESSAGES defined in the environment (exported from ~/.profile). The compound command should (of course) be locale | awk -F'=' '$1 == "LC_MESSAGES" { print substr($2, 1, 2) }' which I think is easier to understand than your version with sed. – August Karlstrom Apr 30 '15 at 11:12
  • 1
    @August Karlstrom: Possible. I've been playing the UNIX game for ~25 years, I suppose I'm deeply stuck in the old-school ways. Sorry about that. shrug – lcd047 Apr 30 '15 at 13:10