Java - general

locate with ResourceBundles

java.util.ResourceBundle
In many applications all texts shall be available for different languages. These texts let themselves evacuate in so-called ResourceBundles. This is showed here in form of two simple properties-files. With it, the two letters define after the „_“ the language (de=german, en=english, fr=french, …).
In addition, the land can also be indicated (DE=Germany, CH=Switzerland, …). In this case, the property-file would be called e.g. texts_de_DE.properties.

Property-file for the german texts (texts_de.properties):

name=Name
street=Straße
city=Ort




Property-file for the english texts (texts_en.properties):

name=name
street=street
city=city


Then the use occurs so:

String baseName = "texts";
try {
   ResourceBundle rb = ResourceBundle.getBundle(baseName);  
   System.out.println(rb.getString("name"));    // "Name" oder "name" 
   System.out.println(rb.getString("street"));  // "Straße" oder "street"
   System.out.println(rb.getString("city"));    // "Ort" oder "city"
} catch (MissingResourceException e) {
   // The Property-file could not be found 
   // or the Key does not exist.
}

The example on the top delivers the at the operating system set language.

But you can also force the language:

rb = ResourceBundle.getBundle(baseName, Locale.GERMAN);   // For German

Eigene Werkzeuge
Werkzeuge

gratis Counter by GOWEB
seit 9.10.2007