views:

320

answers:

3

Is there a way to detect the character encoding set in the terminal which is calling my Java program? In Windows I can call the "chcp" tool and parse the output.

But what about in Linux or Mac?

+2  A: 

Check the locale(1) man page

You can change this by setting the LANG environment variable

$ export LANG=en_US.iso88591

$ locale
LANG=en_US.iso88591
LC_CTYPE="en_US.iso88591"
LC_NUMERIC="en_US.iso88591"
LC_TIME="en_US.iso88591"
LC_COLLATE="en_US.iso88591"
LC_MONETARY="en_US.iso88591"
LC_MESSAGES="en_US.iso88591"
LC_PAPER="en_US.iso88591"
LC_NAME="en_US.iso88591"
LC_ADDRESS="en_US.iso88591"
LC_TELEPHONE="en_US.iso88591"
LC_MEASUREMENT="en_US.iso88591"

LC_IDENTIFICATION="en_US.iso88591"

stacker
Better than nothing...the problem is that locale gives the same output even after changing the terminal encoding... it simply gives the default console encoding for new consoles...
Epaga
A: 

Every time you start new shell session it gets all parameters from a config file. So you have to change them to make shell get desired codepage on start. For example in FreeBSD this config file is /etc/login.conf.

Adding the following lines to /etc/profile or ~/.bash_profile is also helpful:
LANG=ru_RU.CP1251; export LANG
LC_ALL=ru_RU.CP1251; export LC_ALL
MM_CHARSET=CP1251; export MM_CHARSET

The latter will set russian locale.

Yasir Arsanukaev
+1  A: 

There is no defined communication path from the terminal app to processes running inside a terminal window (short of those defined by the terminal being emulated, such as window size changes). This would be true on any other Unix-like OS as well. Environment variables can only be inherited when a process starts. Looking at the values of these environment variables is all you can do.

Do you really expect people to be changing the encoding while a terminal window is open? I can't imagine this is a common use case at all.

Nicholas Riley