31 August, 2012

ADF : Get Key from Resource Bundle

Resource bundles contain locale-specific objects.
In this way, you can write program code that is largely independent of the user's locale isolating most, if not all, of the locale-specific information in resource bundles.

This allows you to write programs that can:
    a- be easily localized, or translated, into different languages
    b- handle multiple locales at once
    c- be easily modified later to support even more locales


You can use the below code snippet to get key value from Resource Bundle

   public static FacesContext getFacesContext() {  
     return FacesContext.getCurrentInstance();  
   }  
   
   public static ResourceBundle getResourceBundle(String resourceBundlePath) {  
   
     return ResourceBundle.getBundle(resourceBundlePath, getFacesContext().getViewRoot().getLocale());  
   }  
   
   public static Object getResourceBundleKey(String resourceBundlePath, String key) {  
     ResourceBundle bundle = getResourceBundle(resourceBundlePath);  
     return getResourceBundleKey(bundle, key);  
   }  
   
   private static Object getResourceBundleKey(ResourceBundle resourceBundle, String key) {  
     Object bundleKeyValue;  
     try {  
       bundleKeyValue = resourceBundle.getString(key);  
     } catch (MissingResourceException mrExp) {  
       bundleKeyValue = key + " not found in resource bundle";  
     }  
     return bundleKeyValue;  
   }  

Import the following classes

 import java.util.MissingResourceException;  
 import java.util.ResourceBundle;  
   
 import javax.faces.context.FacesContext;  

Note resourceBundlePath should be the complete package name of your resource Bundle
For example demo.mah.view.ViewControllerBundle

Thanks
Recommended Post Slide Out For Blogger