user3035305
user3035305

Reputation: 295

Spanish character not supported in linux server

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

Upvotes: 0

Views: 2078

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54583

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.

Upvotes: 2

Related Questions