The ResourceBundle class is used to internationalize the messages. In other words, we can say that it provides a mechanism to globalize the messages.
The hardcoded message is not considered good in terms of programming, because it differs from one country to another. So we use the ResourceBundle class to globalize the massages. The ResourceBundle class loads these informations from the properties file that contains the messages.
Conventionally, the name of the properties file should be filename_languagecode_country code for example MyMessage_en_US.properties.
There are many methods in the ResourceBundle class. Let's see the commonly used methods of the ResourceBundle class.
Let's see the simple example of ResourceBundle class. In this example, we are creating three files:
greeting=Hello, how are you?
greeting=Halo, apa kabar?
import java.util.Locale; import java.util.ResourceBundle; public class InternationalizationDemo { public static void main(String[] args) { ResourceBundle bundle = ResourceBundle.getBundle("MessageBundle", Locale.US); System.out.println("Message in "+Locale.US +":"+bundle.getString("greeting")); //changing the default locale to indonasian Locale.setDefault(new Locale("in", "ID")); bundle = ResourceBundle.getBundle("MessageBundle"); System.out.println("Message in "+Locale.getDefault()+":"+bundle.getString("greeting")); } }