Setting Java Locale Settings

How do I set the default locale in the JVM?

From the Oracle Reference:

The default locale of your application is determined in three ways.
First, unless you have explicitly changed the default, the
Locale.getDefault() method returns the locale that was initially determined
by the Java Virtual Machine (JVM) when it first loaded. That is, the
JVM determines the default locale from the host environment. The host
environment's locale is determined by the host operating system and
the user preferences established on that system.

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.

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

Setting java locale settings

I believe java gleans this from the environment variables in which it was launched, so you'll need to make sure your LANG and LC_* environment variables are set appropriately.

The locale manpage has full info on said environment variables.

How do I change the default locale settings in Java to make them consistent?

Nice, i was interested about the same thing but so far I have only a partial solution:

user.timezone=UTC
user.country=CA
user.variant=
user.language=en
file.encoding=utf8

You can use Canada, because it one of the countries using the ISO 8601 format.

Java 7 default locale

This is as designed. Java 7 has changed the way Locale.getDefault() works. A defect has been entered with Oracle, but they basically said this is As Designed.

To sum up, you must modify the Display Language of the OS. Modifying the Region Format only is no longer sufficient.

Read the bug report here: Locale.getDefault() returns wrong Locale for Java SE 7

How can Locale.setDefault(new Locale (en, US)) be changed at runtime?

The Locale.setDefault is a global thing. If you have two users that need to use differnt locales that won't work out.

You should probably put the locale in the HttpServletRequest.getSession().

After we did the global setDefault it even switched the language and logging of our application server.

Locale.getDefault() returns en always

In Linux/Unix/Mac, the settings LC_ALL and LANG can control the default locale for Java programs. In Windows, the locales are set from the Control Panel, under Regional and Language Options.

When the JVM starts in a *nix environment, it will do this:

  • Scan the environment for LC_ALL
  • If LC_ALL doesn't exist, scan the environment for LANG
  • If the JVM setting user.language is set, use that in place of the environment variables.
  • If nothing is set, default to en_US (I believe this is the final failure case)

In your environment, you have LC_ALL set to C, which is just the C locale. It's basically a traditional fallback to the days when locales weren't used.

You can change LC_ALL in your case, and restart your JVM, and you should get a new value for java.util.Locale.getDefault().

Example:

import java.util.Locale;

public class LocaleTest {
public static void main(String[] args) {
System.out.println(Locale.getDefault());
}
}

Here's running:

> LC_ALL=en_UK java LocaleTest
en_UK

> LC_ALL=ja_JP java LocaleTest
ja_JP

Also note that if you're running Java 1.7.0-b147, there is a bug with the JRE not recognizing environment settings for locale, and will always use the default system locale.

Bug report here: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7073906



Related Topics



Leave a reply



Submit