Java Tutorial/I18N/ListResourceBundle

Материал из Java эксперт
Перейти к: навигация, поиск

List Resource bundle

import java.util.Enumeration;
import java.util.ResourceBundle;
import java.util.MissingResourceException;
public class SimpleResourceBundleExample {
  public static void main(String [] argv) {
    try {
      ResourceBundle rb = ResourceBundle.getBundle("SimpleResourceBundle");
      System.out.println(rb.getString("AMMessage"));
      System.out.println(rb.getString("PMMessage"));
    } catch (MissingResourceException mre) {
      mre.printStackTrace();
    }
  }
}
import java.awt.*;
import java.util.*;
public class SampleResourceBundle extends ListResourceBundle {
  public Object [][] getContents() {
    return contents;
  }
  static final Object [][] contents = {
      {"okButton", new Button("OK")},
      {"negativeInteger", new Integer(-1)},
      {"textString", "Thank you for reading our book"}
  };
}

import java.util.Enumeration;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
public class SimpleResourceBundle extends ResourceBundle {
  private String keys = "AMMessage PMMessage";
  public Object handleGetObject(String key) {
    if (key.equals("AMMessage")) return "Good morning";
    if (key.equals("PMMessage")) return "Good evening";
    return null;
  }
  public Enumeration getKeys() {
    StringTokenizer keyTokenizer = new StringTokenizer(keys);
    return keyTokenizer;
  }
}

public class SimpleResourceBundle_it extends SimpleResourceBundle {
  public Object handleGetObject(String key) {
    if (key.equals("AMMessage")) return "Buona mattina";
    if (key.equals("PMMessage")) return "Buona sera";
    return null;
  }
}





Text Bundle

import java.util.ListResourceBundle;
class TextBundle extends ListResourceBundle {
  public Object[][] getContents() {
    return contents;
  }
  static final Object[][] contents = { { "dog", "dog" }, { "cat", "cat" }, { "horse", "horse" },
      { "cow", "cow" }, { "elephant", "elephant" } };
}
class TextBundle_es extends ListResourceBundle {
  public Object[][] getContents() {
    return contents;
  }
  static final Object[][] contents = { { "dog", "perro" }, { "cat", "gato" },
      { "horse", "caballo" }, { "cow", "vaca" }, { "elephant", "elefante" } };
}