Localizing Messages - Java Internationalization

Java examples for Internationalization:Locale

Description

Localizing Messages

Demo Code

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

public class Main {
  public static void m() {
    String baseName = "MyResources";
    try {//from ww  w  .  jav  a  2 s . co  m
      // Get the resource bundle for the default locale
      ResourceBundle rb = ResourceBundle.getBundle(baseName);

      String key = "hello";
      String s = rb.getString(key); // Hello
      key = "bye";
      s = rb.getString(key); // Goodbye

      // Get the resource bundle for a specific locale
      rb = ResourceBundle.getBundle(baseName, Locale.FRENCH);

      key = "hello";
      s = rb.getString(key); // Bonjour
      key = "bye";
      s = rb.getString(key); // Au Revoir
    } catch (MissingResourceException e) {
      // The resource bundle cannot be found or
      // the key does not exist in the resource bundle
    }
  }
}

Related Tutorials