24

Messages in my terminal are displayed using Russian language by default, which is my native one.

For just a moment I want them to be in English (eg. to paste in forums), then switch back to the default language.

How can I do the switch and switch back using bash?

25

There are several environment variables available for changing language settings. You can view your current locale settings by executing the locale command. To change all locale settings to English, use LANG=C. This C locale is always available without installing additional language packs. (In order to temporarily change to non-English locales, see @mklement0's post.)

Examples:

Executing a command with the default language settings and print the current locale settings:

$ /nonexistent
bash: /nonexistent: Bestand of map bestaat niet
$ locale
LANG=nl_NL.UTF-8
LANGUAGE=
LC_CTYPE="nl_NL.UTF-8"
LC_NUMERIC="nl_NL.UTF-8"
LC_TIME="nl_NL.UTF-8"
LC_COLLATE="nl_NL.UTF-8"
LC_MONETARY="nl_NL.UTF-8"
LC_MESSAGES="nl_NL.UTF-8"
LC_PAPER="nl_NL.UTF-8"
LC_NAME="nl_NL.UTF-8"
LC_ADDRESS="nl_NL.UTF-8"
LC_TELEPHONE="nl_NL.UTF-8"
LC_MEASUREMENT="nl_NL.UTF-8"
LC_IDENTIFICATION="nl_NL.UTF-8"
LC_ALL=

Temporarily override the language for one program and show that it is really temporary:

$ LANG=C ls /nonexistent
ls: cannot access /nonexistent: No such file or directory
$ ls /nonexistent
ls: kan geen toegang krijgen tot /nonexistent: Bestand of map bestaat niet

Change the locale for all commands executed in the current shell and include proofs again:

$ LANG=C
$ ls /nonexistent
ls: cannot access /nonexistent: No such file or directory
$ locale
LANG=C
LANGUAGE=
LC_CTYPE="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_COLLATE="C"
LC_MONETARY="C"
LC_MESSAGES="C"
LC_PAPER="C"
LC_NAME="C"
LC_ADDRESS="C"
LC_TELEPHONE="C"
LC_MEASUREMENT="C"
LC_IDENTIFICATION="C"
LC_ALL=
  • 6
    For other users having issues getting this to work - setting LANG or LANG_ALL isn't working for me, yet LANGUAGE is. See Why is overriding the LANG environment variable not changing the language for me? – gertvdijk Jun 23 '13 at 16:07
  • 1
    @gertvdijk: Thanks for that; the reason this answer works even without setting LANGUAGE is an exception: GNU gettext gives precedence to the LANGUAGE value except if the (effective) LC_MESSAGES value (typically set indirectly via LANG or LC_ALL) is either C or (its synonym) POSIX. Also note that LANGUAGE happens to be unset in this answer, whereas it is set by default, and if it is set, you must override it to switch to a specific locale's language (as opposed to "C" / "POSIX"), which is what you found. – mklement0 Nov 1 '16 at 5:45
  • Your answer works great when switching to the "C" locale (with US English messages, as requsted), but won't work for other locales unless LANGUAGE is explicitly unset or overridden. Given the question's generic title, it is likely that people will find this answer looking to switch to a non-English language also, so please consider adding this information to your answer. – mklement0 Nov 1 '16 at 5:52
  • @mklement0 Given the context of the question (posting English error messages), I think that the current post is sufficient. You can add another answer to explain the details around LANGUAGE if you want :) – Lekensteyn Nov 2 '16 at 0:23
  • I've added my own answer, as you suggested. If you agree with my assessment that future readers may come here looking to switch to any language (as several people have already actively indicated), please add a link to my answer to your question. Aside from that, I suggest changing the value of LANGUAGE in your sample output to nl, which is the actual default value when your locale is nl_NL.UTF-8. – mklement0 Nov 2 '16 at 2:20
12

Lekensteyn's helpful answer works great if you want to switch to US English on demand, as the OP requested, but if you want to switch to a different language on demand, more work is needed.

Before starting, you must install message tables with sudo apt-get install language-pack-<lang-tag>, where <lang-tag> is a simple RTF 5646 language subtag, such as es for Spanish.

Background info

GNU gettext-based utilities give precedence to the nonstandard LANGUAGE environment variable[1] over POSIX-defined locale environment variables LC_ALL, LC_MESSAGES, and LANG (in that order).

Given that LANGUAGE is set by default on Ubuntu systems[2], namely to a substring of the LANG value that reflects either a simple language tag (e.g., es for Spanish) or a language-region tag (e.g., de_DE for the Germany variant of German), you must unset or override LANGUAGE in order for a different language's messages to take effect.[3]

Option 1: Set LANGUAGE

Example: Switch to Spanish (es) messages ad-hoc:

$ LANGUAGE=es ls NoSuchFile
ls: no se puede acceder a NoSuchFile: No existe el archivo o el directorio

Note: A simple language tag such as es is sufficient, but you may add a region identifier (e.g., es_AR for Argentina), and even a charset suffix (e.g., es_AR.UTF-8).
However, localized messages may only exist at the language level, and the fallback is to use messages that match the language part (es, in this case).

Option 2: Unset LANGUAGE and set LC_ALL

This alternative solution undefines LANGUAGE first, and then uses POSIX locale environment variable LC_ALL to implicitly set LC_MESSAGES[4]:

$ LANGUAGE= LC_ALL=es_ES.UTF-8 ls NoSuchFile
ls: no se puede acceder a NoSuchFile: No existe el archivo o el directorio

This solution has the advantage of setting all localization aspects to the specified locale (such as LC_TIME for date/time formats) and by (implicitly) setting LC_MESSAGES also informs non-GNU programs of the desired language.

Note how LC_ALL requires the exact, full locale name, including charset suffix, to be effective (es_ES.UTF-8) (unlike LANGUAGE, for which a simple language tag is sufficient (like es)). The same applies to setting LC_MESSSAGES and LANG. Specifying an invalid / non-installed locale name causes fallback to the POSIX locale and therefore US English.


Footnotes

[1] The reasons that Lekensteyn's answer works even without unsetting / overriding LANGUAGE is an exception: if the (effective) LC_MESSAGES value (typically set indirectly via LANG or LC_ALL) is either C or (its synonym) POSIX, that value is respected, irrespective of the value of LANGUAGE, if any. Conversely, if the (effective) LC_MESSAGES value is any other, specific locale, LANGUAGE takes precedence.

[2] This applies to Ubuntu proper, but not necessarily to other flavors; Lekensteyn states that Kubuntu does not set LANGUAGE.
Arguably, LANGUAGE should not be set by default, given that in its absence the LC_MESSAGES value implied by the LANG value (which determines the current locale), is respected.

[3] You can also use this approach to switch to [US] English by assigning either LANGUAGE=C or LANGUAGE=POSIX (as an alternative to, LANG=C / LANG=POSIX), though I'm unclear whether that is actively recognized or simply a fallback mechanism, given that these values don't start with a language tag; perhaps the better choice would be en_US.

[4] There's an edge case where this approach doesn't work: Trying to invoke an executable with a path - whether relative or absolute - does NOT switch to the specified language, whereas a mere filename does:
LANGUAGE= LC_ALL=es_ES.UTF-8 /path/to/no_such_utility does not work (outputs a message in the current locale), whereas
LANGUAGE= LC_ALL=es_ES.UTF-8 no_such_utility does (outputs a Spanish error message).
If anyone knows why and whether there's a good reason for this, do let us know.

  • Tip of the hat to @wjandrea for his help with structuring this post. – mklement0 Nov 3 '16 at 19:57

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.