Monday

ResourceBundle example - read a property file based on the locale inside the class folder

package read.properties.file;

import java.util.Locale;
import java.util.ResourceBundle;

public class ResourcesBundleExample {

       public static void main(String[] args) {

              ResourceBundle bundle = ResourceBundle.getBundle("config");

              // read config_tr_TR.properties
              Locale.setDefault(new Locale("tr", "TR"));
              bundle = ResourceBundle.getBundle("config");
              System.out.println("Current Locale: " + Locale.getDefault());
              System.out.println("my name is: " + bundle.getString("who_are_you"));
             
              // read config_en_US.properties
              Locale.setDefault(new Locale("en", "US"));
              bundle = ResourceBundle.getBundle("config");
              System.out.println("Current Locale: " + Locale.getDefault());
              System.out.println("my name is: " + bundle.getString("who_are_you"));

       }

}

config_tr_TR.properties
who_are_you = eda_TR


config_en_US.properties
who_are_you = eda_EN
outputs:
Current Locale: tr_TR
my name is: eda_TR
Current Locale: en_US
my name is: eda_EN




No comments:

Post a Comment