Site Search:

Build a resource bundle for each locale and load a resource bundle in an application

Back>

resource bundle
resource bundle

When we write a ResourceBundle code like the followin:
ResourceBundle rb = ResourceBundle.getBundle("Helloworld", new Locale("en", "CA"));
rb.getString("hello");

We ask the JVM to pick the best available resource bundle for a given key "hello". The JVM will start searching all the resource bundles defined in <bundle name>*.java and <bundle name>*.properties.

If JVM can not find a match in the bundle you specified, namely Helloworld_en_CA.java or Helloworld_en_CA.properties, it will keep looking other places.

The following java code will return the default Locale:
Locale locale = Locale.getDefault();

on my machine, it is en_US.

So for resource bundle helloworld with the locale new Locale("fr", "CA") and the default locale en_US, JVM's searching order will be:


  1. The requested Locale: Helloworld_fr_CA.java then Helloworld_fr_CA.properties
  2. The requested Locale with region dropped: Helloworld_fr.java then Helloworld_fr.properties
  3. The default Locale: Helloworld_en_US.java then Helloworld_en_US.properties
  4. The default Locale with region dropped: Helloworld_en.java then Helloworld_en.properties
  5. The default bundle without locale: Helloworld.java then Helloworld.properties
  6. still missing, throw MissingResourceException

OCPJP>ls Helloworld_*
Helloworld_en.properties Helloworld_fr.properties
Helloworld_en_US.java Helloworld_fr_CA.java
OCPJP>
OCPJP>cat Helloworld_en.properties 
#locale with region dropped
hello=Hello-en
world=world-en
OCPJP>cat Helloworld_fr.properties 
!Locale with region dropped
 hello=         Bonjour-fr
world : le\
           monde-fr
OCPJP>cat Helloworld_en_US.java 
import java.util.*;
import java.time.*;
public class Helloworld_en_US extends ListResourceBundle {
  public static void main(String[] args) {
    Locale locale = new Locale.Builder().setLanguage("en").setRegion("US").build();
    System.out.println(Locale.getDefault());
    ResourceBundle rb = ResourceBundle.getBundle("Helloworld", locale);
    String helloworld = rb.getString("hello") + " " + rb.getString("one") + " " + rb.getString("world") + " at " + rb.getObject("time"); 
    System.out.println(helloworld);
  }

  protected Object[][] getContents() {
    return new Object[][] {
      {"time", LocalDateTime.now()},
      {"hello", "Helloxx"}
    };
  }
}
OCPJP>cat Helloworld_fr_CA.java 
import java.util.*;
import java.time.*;
public class Helloworld_fr_CA extends ListResourceBundle {
  public static void main(String[] args) {
    Locale locale = new Locale.Builder().setLanguage("fr").setRegion("CA").build();
    System.out.println(Locale.getDefault());
    ResourceBundle rb = ResourceBundle.getBundle("Helloworld", locale);
    String helloworld = rb.getString("hello") + " " + rb.getString("one") + " " + rb.getString("world") + " at " + rb.getObject("time"); 
    System.out.println(helloworld);
  }

  protected Object[][] getContents() {
    return new Object[][] {
      {"time", LocalDateTime.now()},
      {"hello", "Bonjour-fr-CA"}
    };
  }
}
OCPJP>javac Helloworld_*.java
OCPJP>java Helloworld_en_US
en_US
Helloxx 1 world-en at 2018-02-15T18:54:03.720
OCPJP>java Helloworld_fr_CA
en_US
Bonjour-fr-CA 1 lemonde-fr at 2018-02-15T18:54:13.727
OCPJP>
OCPJP>cat Helloworld.properties 
#default bundle without locale

one 1


Notice the *.properties file used some advance syntaxes, check previous post for them.