JDK-4397903 : default locale is always en_Us, ignoring user.language setting
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:i18n
  • Affected Version: 1.3.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2000-12-13
  • Updated: 2001-01-10
  • Resolved: 2001-01-10
Related Reports
Duplicate :  
Relates :  
Description

Name: yyT116575			Date: 12/13/2000


java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0)
Java HotSpot(TM) Client VM (build 1.3.0, mixed mode)


When I run any Java program, no matter what I set the system properties
user.language and user.region to, the default locale is still en_US. Here is
an example program:

import java.util.*;

public class LocaleBug {
    public static void main(String[] args) {
        for (Iterator iter = System.getProperties().keySet().iterator();iter.hasNext(); ) {
            String key = (String) iter.next();
            if (key.startsWith("user.") || key.startsWith("java.vm.")) {
                System.out.println(key + " -> " + System.getProperty(key));
            }
        }
        System.out.println("locale is " + Locale.getDefault());
    }
}

Here is what happens when I run it with the 3 VMs on my system:

[john@localhost ~/projects/filler/locale]$ /opt/jbuilder4/jdk1.3/bin/java
-Duser.language=fr -Duser.region=FR LocaleBug
java.vm.version -> 1.3.0
java.vm.vendor -> IBM Corporation
java.vm.name -> Classic VM
java.vm.specification.name -> Java Virtual Machine Specification
user.dir -> /home/john/projects/filler/locale
java.vm.specification.vendor -> Sun Microsystems Inc.
user.home -> /home/john
user.timezone -> Pacific/Honolulu
user.name -> john
java.vm.specification.version -> 1.0
user.language -> fr
java.vm.info -> J2RE 1.3.0 IBM build cx130-20000815 (JIT enabled: jitc)
user.region -> FR
locale is fr_FR

[john@localhost ~/projects/filler/locale]$ /opt/IBMJava2-13/jre/bin/java
-Duser.language=fr -Duser.region=FR LocaleBug
java.vm.version -> 1.3.0
java.vm.vendor -> IBM Corporation
java.vm.name -> Classic VM
java.vm.specification.name -> Java Virtual Machine Specification
user.dir -> /home/john/projects/filler/locale
java.vm.specification.vendor -> Sun Microsystems Inc.
user.home -> /home/john
user.timezone -> Pacific/Honolulu
user.name -> john
java.vm.specification.version -> 1.0
user.language -> fr
java.vm.info -> J2RE 1.3.0 IBM build cx130-20000623 (JIT enabled: jitc)
user.region -> FR
locale is fr_FR

[john@localhost ~/projects/filler/locale]$ /usr/java/jdk1.3/bin/java
-Duser.language=fr -Duser.region=FR LocaleBug
java.vm.version -> 1.3.0
java.vm.vendor -> Sun Microsystems Inc.
java.vm.name -> Java HotSpot(TM) Client VM
java.vm.specification.name -> Java Virtual Machine Specification
user.dir -> /home/john/projects/filler/locale
java.vm.specification.vendor -> Sun Microsystems Inc.
user.home -> /home/john
user.timezone ->
user.name -> john
java.vm.specification.version -> 1.0
user.language -> fr
java.vm.info -> mixed mode
user.region -> FR
locale is en_US

As you can see, the Sun VM knows the correct language and region, and the locale
is wrong. I am guessing that the static initialiser for locale is run before the
values of the system variables are set????? The code in Locale.java is:

    static {
        String language =
            (String) AccessController.doPrivileged(
                        new GetPropertyAction("user.language","EN"));
        String country =
            (String) AccessController.doPrivileged(
                        new GetPropertyAction("user.region",""));

        /* The user.region property may be of the form country, country_variant,
         * or _variant.  Since the Locale constructor takes the country value as
         * an unparsed literal, and we don't want to change that behavior, we
         * must premunge it here into country and variant. - liu 7/24/98 */
        String variant = "";
        int i = country.indexOf('_');
        if (i >= 0) {
            variant = country.substring(i+1);
            country = country.substring(0, i);
        }
        defaultLocale = new Locale(language, country, variant);
    }

which suggests that what I think should work is correct, it just doesn't.
(Review ID: 113552) 
======================================================================

Comments
WORK AROUND Name: yyT116575 Date: 12/13/2000 Put this code at the start of your main method. // This code copied from the Locale static initialiser, because it // doesn't seem to be executed in Sun's JDK1.3 on Linux. String language = System.getProperty("user.language","en"); String country = System.getProperty("user.region",""); // language can be en_US if set from $LANG on Linux int i = language.indexOf('_'); if (i >= 0) { if (country.equals("")) { country = language.substring(i+1); } language = language.substring(0, i); } String variant = ""; i = country.indexOf('_'); if (i >= 0) { variant = country.substring(i+1); country = country.substring(0, i); } Locale defaultLocale = new Locale(language, country, variant); Locale.setDefault(defaultLocale); System.out.println("default locale is " + Locale.getDefault()); ======================================================================
11-06-2004