0

I have Java application where user enters "spanish characters" from the UI and that needs to be displayed on some other page. Application is fine in WINDOWS environment, but when deployed in UNIX/Linux server, spanish characters in not displayed correctly.

Here is my Unix locale setting (using locale command)

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

my unix box character encoder (using java I checked Charset.defaultCharset()) is ISO 8859 1 And my java code to display this data is

spanishName = new String(spanishName.getBytes("8859_1"), "UTF-8");

now it is printing N?? L???d?

If I am printing this variable directly then NT= LTß±dTr.

But the original data is Néó Léáñdér

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
user3035305
  • 285
  • 1
  • 9
  • 17
  • You're assuming ISO Latin every time. Maybe you don't require converting to Unicode when deployed in your *nix server. – Mena May 29 '15 at 09:37
  • Sorry, this is all a little vague... Obviously you cannot visualize such characters correctly when using ISO-8859-1 or ASCII, since they are not contained and have to get missinterpreted. The question here is _why_ your environment has that setting. Todays linux system consequently use UTF-8 encoding unless something else is specified. so your locale settings should hold something like `en_US.UTF-8`... – arkascha May 29 '15 at 09:37
  • I think, we are using old UNIX box and changing these settings is out of my scope. I need correct data in my code. Please tell me if we can handle or override it in our java code. and just for my information, how can we change the _locale settings_ in unix box? – user3035305 May 29 '15 at 09:47

1 Answers1

2

UTF-8 encoding will not display in POSIX "C" locale. Your server might support other locales. To see this, run

locale -a

and look for settings which end in a variety of "utf8" or "utf-8" (both uppercase and lowercase). If you find one, set your locale environment variables to that.

Supposing that you find en_US.UTF-8. Then (assuming your shell is ksh or bash), you could execute these commands before running your Java application:

unset LANGUAGE
LANG=en_US.UTF-8; export LANG
LC_CTYPE=en_US.UTF-8; export LC_CTYPE
LC_ALL=en_US.UTF-8; export LC_ALL

In csh, that would be

unsetenv LANGUAGE
setenv LANG en_US.UTF-8
setenv LC_CTYPE en_US.UTF-8
setenv LC_ALL en_US.UTF-8

That would set the system locale. There are additional ways to set the locale, as noted in

That is, on the command-line (quoting from Oracle's documentation):

Second, on some Java runtime implementations, the application user can override the host's default locale by providing this information on the command line by setting the user.language, user.country, and user.variant system properties.

and it gives as example

java -Duser.language=fr -Duser.country=CA Default

and finally,

Third, your application can call the setDefault(Locale aLocale) method. The setDefault(Locale aLocale) method lets your application set a systemwide resource. After you set the default locale with this method, subsequent calls to Locale.getDefault() will return the newly set locale.

Note that Java uses different names from the system locales, and not all are supported on each platform (see table); you will have to find what works for you.

Community
  • 1
  • 1
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • How can I set my locale environment to that (I don't know UNIX) ?...Can we achieve it without changing the environment? – user3035305 May 29 '15 at 10:00
  • Thank you for the information. I don't think I have permisssion to change it. Anyway I have to do it in java. can we handle it in Java code? – user3035305 May 29 '15 at 10:16