The Java Program: Main.java

  1 import java.text.MessageFormat;
  2 import java.util.Locale;
  3 import java.util.ResourceBundle;
  4 
  5 public class Main {
  6 
  7    final static Locale[] locArray = {
  8       Locale.ENGLISH,Locale.FRENCH,Locale.GERMAN,Locale.KOREAN,new Locale("sv","")};
  9 
 10    public static void main (String[] args) {
 11 
 12       for (int i = 0; i < locArray.length; i++) {
 13 
 14          final Locale loc = locArray[i];
 15          System.out.println ("Using locale: " +loc);
 16 
 17          // Create a resource bundle object and load the bundle.
 18          final ResourceBundle myResources = ResourceBundle.getBundle("MyResources", loc);
 19 
 20          // Create blank message format and set locale
 21          final MessageFormat mf = new MessageFormat ("");
 22          mf.setLocale(loc);  // must come before "applyPattern"
 23          
 24          // Get the pattern from resource bundle and use in the message foramt
 25          mf.applyPattern (myResources.getString("pattern"));
 26 
 27          // Assign the first 4 objects from the bundle to an array.
 28          final Object[] arguments = {   
 29             new Integer(7),
 30             new Long(System.currentTimeMillis()),
 31             myResources.getString("color"), 
 32             myResources.getString("animal"), 
 33          }; 
 34 
 35          // Print the pattern
 36          System.out.println (mf.toPattern());
 37 
 38          // Format the objects in the arguments array to a string.
 39          final String result = mf.format (arguments);
 40 
 41          // Print the formatted string.
 42          System.out.println (result);
 43          System.out.println ();
 44       }
 45    }
 46 }