JDK-6463986 : (rb) Unexpected behavior of ResourceBundle fallback mechanism
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util:i18n
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-08-25
  • Updated: 2011-02-16
  • Resolved: 2006-08-25
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_07"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-b03)
Java HotSpot(TM) Client VM (build 1.5.0_07-b03, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
The fallback mechanism of ResourceBundle depends on default locale (Locale.getDefault()) in an unexpected way.

Assume we have .properties localization files for different languages, with the root language, which is as a matter of fact English. For instance, we have root property file i18n.properties with English stuff, and then i18n_cs.properties with Czech stuff. If we call ResourceBundle.getBundle for czech locale, we get stuff from i18n_cs if we call it for english locale, or any other locale, we should get the fallback stuff from the root i18n file, regardless what Locale.getDefault says. That is, if the developer decided that the root locale is English, it should be English even on systems that have set locale to e.g. czech.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
see example code. you also need to create the two property files in the same dir as the .java file:
- i18n.properties ----
key=Key
- i18n_cs.properties ----
key=Klic


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
the test case should return "Key", because we are asking for bundle that is not supported (English), so we should fall back to the superordinate root bundle
ACTUAL -
the test case actually returns "Klic", i.e. the fallback mechanism falls back to bundle of the system's default locale, not to the superordinate bundle of the requested English bundle.

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
package org.ksmsa.utils.i18n.test;

import java.util.Locale;
import java.util.ResourceBundle;

public class ResourceBundleTest {

	public static void main(String[] args) {
		Locale.setDefault(new Locale("cs"));
		ResourceBundle bundle = ResourceBundle.getBundle(
				"org.ksmsa.utils.i18n.test.i18n", Locale.ENGLISH);
		System.out.println(bundle.getString("key"));
	}

}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
for each root .properties resource bundle, create its copy named according to its language. In our case, for each i18n.properties create a copy named i18n_en.properties

Comments
EVALUATION Unfortunately, it's the spec to fall back the default Locale if a resource bundle for the specified Locale is not found. However, we enhanced ResourceBundle in JDK 6 so that the getBundle behavior can be controlled. Please refer to the JDK 6 API documentation for ResourceBundle. Closing this CR as a duplicate of 4303146.
25-08-2006