The Java Internationalization API
by Carol A. Jones



Example 1:

base + "_" + language1 + "_" + country1 + "_" + variant1
base + "_" + language1 + "_" + country1
base + "_" + language1
base

Example 2:
(a)
message = "Could not write to the " + filename + " file";

(b)
message = "No se encontrs el archivo " + filename;


Example 3:

(a) 
Object[] arguments = {new Date(System.currentTimeMillis()), "myfile.java"};
String result = MessageFormat.format("{1} was modifed at {0,time} 
                on {0,date}",arguments);


(b)
myfile.java was modified at 12:30 PM on Jul 23, 1997


Listing One
import java.util.*;

public class MyStrings_de_DE extends ListResourceBundle {
   public Object[][] getContents() {
    return contents;
   }
   static final Object[][] contents = {
      // LOCALIZE THIS
      {"FontName",  "Schriftart"},
      {"Size",      "Grv_e"}, 
      {"Bold",      "Fett"},
      {"Italic",    "Kursiv"},
      {"Color",     "Farbe"},
      {"Red",       "Rot"},
      {"Green",     "Grun"},
      {"Blue",      "Bla|"},
      {"Sample",    "Vorschau"}
      // END OF MATERIAL TO LOCALIZE
   };
}


Listing Two
// This sample illustrates use of the Collator class
import java.text.*;
import java.util.*;

class StrengthDemo {

  static Collator collator;
  static int result;
  static String s1 = "pjche";
  static String s2 = "pichi";

  public static void main(String argv[]) {
            
    System.out.println("United States");
    collator = Collator.getInstance(new Locale("en","US"));
    collator.setStrength(Collator.PRIMARY);
    doSorts();
    collator.setStrength(Collator.SECONDARY);
    doSorts();
    collator.setStrength(Collator.TERTIARY);
    doSorts();

    System.out.println();

    System.out.println("France");
    collator = Collator.getInstance(new Locale("fr","FR"));
    collator.setStrength(Collator.PRIMARY);
    doSorts();
    collator.setStrength(Collator.SECONDARY);
    doSorts();
    collator.setStrength(Collator.TERTIARY);
    doSorts();
  }
  public static void doSorts()
  {
    if (collator.getStrength() == Collator.PRIMARY)
      System.out.println("Primary: ");
    else if (collator.getStrength() == Collator.SECONDARY)
      System.out.println("Secondary: ");
    else if (collator.getStrength() == Collator.TERTIARY)
      System.out.println("Tertiary: ");
    result = collator.compare(s1, s2);
    if (result == 0)
      System.out.println(s1 + " equals " + s2);
    else if (result < 0)
      System.out.println(s1 + " is before " + s2);
    else if (result > 0)
      System.out.println(s1 + " is after " + s2);
  }
}
Output
------
United States
Primary:
pjche equals pichi
Secondary:
pjche is after pichi
Tertiary:
pjche is after pichi

France
Primary:
pjche equals pichi
Secondary:
pjche is before pichi
Tertiary:
pjche is before pichi


Listing Three
//--------------------------------------------------------------------------
// Method to sort a vector of strings. This sort uses CollationKeys, where 
// are about 10 times faster than using Collator.compare. This is true even 
// including the overhead of setting up the extra arrays, etc.
// It is a simple bubble sort, which is faster than other algorithms
// for very small vectors and for vectors that are almost in order already
// This sample illustrates use of CollationKey class

import java.text.*;
import java.util.*;

class SortDemo {
    static Vector v = new Vector();
  public static void main(String args[]) {
      v.addElement("Dan");
        v.addElement("Alice");
        v.addElement("Liza");
        v.addElement("Edward");
        v.addElement("Zane");
        v = sort(v);
        for (int i=0; i<v.size(); i++)
          System.out.println(v.elementAt(i));
  }
    public static Vector sort (Vector unsorted) {
      CollationKey temp;
        int i,j;
        int size = unsorted.size();
        Vector sorted = new Vector(size);

        Collator collator = Collator.getInstance(Locale.getDefault());
        CollationKey[] keys = new CollationKey[size];
        for (i=0; i<size; i++)
          keys[i] = collator.getCollationKey((String)(unsorted.elementAt(i)));
        for (i=0; i<size; i++)
            for (j=i; j<size; j++)
               if( keys[i].compareTo( keys[j] ) > 0 ) {
                    temp = keys[j];
                    keys[j] = keys[i];
                    keys[i] = temp;
                }    
        for (i=0; i<size; i++)
            sorted.addElement(keys[i].getSourceString());
        return sorted;
    }
}
Output
------
Alice
Dan
Edward
Liza
Zane


Listing Four
// This sample illustrates use of the BreakIterator class
import java.text.*;
import java.util.*;

class BreakDemo {
    static String string = 
        "He said \"How tall are you?\" and I said I'm 5\'5\" tall, etc.";
    static BreakIterator boundary;
    public static void main(String args[]) {

    //print each word
    boundary = BreakIterator.getWordInstance(Locale.getDefault());
    boundary.setText(string);
    printBreaks("word");

    //print each sentence
    boundary = BreakIterator.getSentenceInstance(Locale.getDefault());
    boundary.setText(string);
    printBreaks("sentence");
 }
 static void printBreaks(String type) {
    int start = boundary.first();
    int end = boundary.next();
    while (end != BreakIterator.DONE) {
        String part = string.substring(start,end);
        if (!part.equals(" "))
          System.out.println(part + " is a " + type);
        start = end;
        end = boundary.next();
    }
 }
}  
Output
------
He is a word
said is a word
" is a word
How is a word
tall is a word
are is a word
you is a word
? is a word
" is a word
and is a word
I is a word
said is a word
I'm is a word
5'5 is a word
" is a word
tall is a word
, is a word
etc. is a word
He said "How tall are you?"  is a sentence
and I said I'm 5' is a sentence
5" tall, etc. is a sentence



5


