java tutorial - ResourceBundle class in Java - java programming - learn java - java basics - java for beginners



  • The ResourceBundle class is utilized to internationalize the messages. At the end of the day, we can state that it gives a system to globalize the messages.
  • The hardcoded message isn't viewed as great as far as programming, since it contrasts starting with one nation then onto the next.
  • So we utilize the ResourceBundle class to globalize the back rubs.
  • The ResourceBundle class stacks these informations from the properties record that contains the messages.
  • Expectedly, the name of the properties document ought to be filename_languagecode_country code for instance MyMessage_en_US.properties.

Methods of ResourceBundle class

  • There are many methods used ResourceBundle class.
    • public static ResourceBundle getBundle(String basename) returns the instance of the ResourceBundle class for the default locale.
    • public static ResourceBundle getBundle(String basename, Locale locale) returns the instance of the ResourceBundle class for the specified locale.
    • public String getString(String key) returns the value for the corresponding key from this resource bundle.

Sample Code

  • Below example creating three files:
    • MessageBundle_en_US.properties file contains the localize message for US country.
    • MessageBundle_in_ID.properties file contains the localize message for Indonaisa country.
    • InternationalizationDemo.java file that loads these properties file in a bundle and prints the messages.

MessageBundle_en_US.properties

Tutorials = Welcome to wikitechy tutorials!!!
click below button to copy the code. By - java tutorial - team

MessageBundle_in_ID.properties

Tutorials = selamat datang di wikitechy tutorial !!!
click below button to copy the code. By - java tutorial - team

InternationalizationDemo.java

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("Tutorials"));  
  
  //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("Tutorials"));  
  
 }  
}  
click below button to copy the code. By - java tutorial - team

Output

Message in en_US : welcome to wikitechy tutorials!!!
Message in in_ID    : selamat datang di wikitechy tutorial !!!

Related Searches to ResourceBundle class in Java