The ResourceBundle class is used to store locale-specific resources that can be loaded by a program to tailor the program’s appearance to the particular locale in which it is being run.
The ResourceBundle
class in Java is a part of the java.util
package and is used for internationalization (i18n) and localization (l10n) of Java applications. It provides a way to externalize strings and other resources from your Java code, allowing you to create versions of your application in different languages and for different regions without modifying the source code.
A ResourceBundle
contains key-value pairs, where the keys are strings that uniquely identify a particular resource, and the values are the localized versions of those resources. These resources can include text messages, images, or any other data that needs to be presented to the user.
There are two types of ResourceBundle
classes:
- PropertyResourceBundle: This class is based on a properties file (a simple text file with key-value pairs).
- ListResourceBundle: This class is based on a two-dimensional array of key-value pairs.
Here’s a simple example using PropertyResourceBundle
:
import java.util.ResourceBundle;
public class Example {
public static void main(String[] args) {
ResourceBundle bundle = ResourceBundle.getBundle(“MyResources”);
System.out.println(“Message in English: “ + bundle.getString(“hello”));
}
}
In this example, the MyResources.properties
file should be in the classpath and contain:
hello=Hello, World!
The ResourceBundle
mechanism helps in creating applications that can be easily adapted for different languages and regions without changing the source code.