Original Address :
Http://tutorials.jenkov.com/java-internationalization/resourcebundle.html
The Java.util.ResourceBundle class is used to store text and localize related data. This article describes the ResourceBundle and its subclasses.
Class hierarchies for ResourceBundle
The ResourceBundle class has two subclasses, propertyResourceBundle and ListResourceBundle. The following are the structure diagrams for these classes.
propertyResourceBundle stores localized text in a Java property file.
Create ResourceBundle
You can create an instance of the ResourceBundle class as follows.
Locale locale = new locale ("en", "US"); ResourceBundle labels = resourcebundle.getbundle ("i18n. Mybundle ", locale); System.out.println (labels.getstring ("Label1"));
you first need to create a locale instance. The locale instance is then passed to the Resourcebundle.getbundle () method, and the name of the resource bundle to be loaded is also passed. Finally, the localized values can be accessed by means of ResourceBundle's GetString (), GetObject (), and so on.
Instead of creating an ResourceBundle instance, you created an instance of the ResourceBundle subclass. The creation of instances is done through factory methods. First the ResourceBundle class will look for ListResourceBundle, and then it will look for propertyResourceBundle. The lookup is done according to the ListResourceBundle class name to match the name of the resource Boundle (the first parameter of the Getbundle () method). If it is not found, it is matched according to the propertyResourceBundle resource bundle.
Property files as ResourceBundle
You can use a standard property file to store localized text. These properties can be loaded through the ResourceBundle class.
Locale locale = new locale ("en", "US"); ResourceBundle labels = resourcebundle.getbundle ("i18n. Mybundle ", locale); System.out.println (labels.getstring ("Label1"));
in the example above, you need to put Mybundle in the package i18n.
The name of the resource bundle is similar to the class name. such as i18n. Mybundle means that the property file in the package i18n mybundle.properties.
The following is the contents of the properties file.
Label1 = Label 1 is done!label2 = label 2 is through!
The above documents are composed of a set of key,value pairs. Key is to the left of the = number, and value is on the right. Value is the content you want to localize.
Place different languages in different property files
To provide strings in different languages, you need to create a property file for each language, with the format of the file name Xxxx_ language encoding. For example:
MyBundle.propertiesMyBundle_da.propertiesMyBundle_de.propertiesMyBundle_fr.properties
These files should be placed in the same package.
Files that do not have a language suffix (for example: mybundle.properties) are the default property files.
A property file with a language encoding suffix contains the same key but value is identified in a different language. such as the Danish property file:
Label1 = label 1 er klar!label2 = label 2 er igennem!
use class as ResourceBundle
You can also use a set of classes to get resources, use classes, and use other types of resources, in addition to string values.
Like the properties file, you can create a set of classes with the bundle base name and language suffix.
For example:
i18n. Myclassbundle
i18n. Myclassbundle_da
i18n. Myclassbundle_en
i18n. Myclassbundle_de
The following is an example of a default bundle class file implementation.
Import Java.util.listresourcebundle;public class Myclassbundle extends ListResourceBundle { @Override Protected object[][] getcontents () { return contents; } Private object[][] Contents = { {"Price" , new Double (10.00)}, {"Currency", "EUR"}, };}
The following is the implementation of the Danish resource bundle.
public class Myclassbundle_da extends ListResourceBundle { @Override protected object[][] getcontents () { return contents; } Private object[][] Contents = { {"Price" , New Double (75.00)}, {"Currency", "DKK"}, };}
Note the contents array, which is a two-dimensional array that contains key,value. Price,currency is a key, and the right side of key is a localized value. Get the ListResourceBundle instance in the same way you get the propertyResourceBundle instance, here's an example of getting the default ResourceBundle and Danish ResourceBundle.
Locale locale = new locale ("de", "de"); No bundle for German, defaultresourcebundle bundle = Resourcebundle.getbundle ("i18n. Myclassbundle ", locale); System.out.println ("Price :" + bundle.getobject ("price")); System.out.println ("Currency:" + bundle.getobject ("currency"); locale = new locale ("Da", "DK"); bundle = Resourcebundle.getbundle ("i18n. Myclassbundle ", locale); System.out.println ("Price :" + bundle.getobject ("price")); System.out.println ("Currency:" + bundle.getobject ("currency")); the output is: Price : 10.0currency:eurprice : 75.0currency:dkk
get the value from ResourceBundle
After you get the ResourceBundle instance, you can obtain the localized values by using the following methods.
GetObject (String key);
GetString (String key);
Getstringarray (String key);
All keys can also be obtained through the keyset () method. set<string> keys = Bundle.keyset ();
Other ResourceBundle methods can be obtained by viewing the document.
Use of the ResourceBundle class in Java