How to get a value form the resource bundle file in ADF

Hi
So many times we need to show message to the users. The problem is that we do not want to pass the message text as hard coded. We sometimes need to read this message from the resource bundle.
In this discussion , I will show how to achieve this task within both the ViewController and Model project.

1- How to achieve this in the ViewController Project.

The following method accepts a String parameter which is the KEY and return the text for this key in the resource bundle.
String resourceBundle is variable represents the class name of the resource bundle file.
In order to now the resource bundle for the project , Just go to the project property,Resource Bundle




/** * added by tarek fathy
 * oct 02 2012
 * use this method to return a value from the ViewControllerBundle file
 * just pass the key
 * @param identifier
 * @return String
 * this method will work only if the recource bundle is org.dhaman.es.view.ViewControllerBundle
 */
 public static String getStringFromResourceBundle(String identifier){
 String resourceBundle= "org.dhaman.es.view.ViewControllerBundle";
 FacesContext facesContext = FacesContext.getCurrentInstance();
 Locale locale = facesContext.getViewRoot().getLocale();
 ResourceBundle bundle = ResourceBundle.getBundle(resourceBundle, locale);
 return bundle.getString(identifier);
 }

the above method helps you to return a string value from the resourde bundle
example: 

FacesContext context = FacesContext.getCurrentInstance(); context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, JSFUtils.getStringFromResourceBundle("INVALID_LOGIN_MSG"),
null));


2- How to achieve this in the Model Project.

You can write the following method in the EntityImpl or ViewImple or ApplicationModuleImp class.




public String getLocalizedValue(String key) {
ResourceBundleDef resourceDef = this.getResourceBundleDef();
Locale locale = this.getDBTransaction().getSession().getLocale();
String retVal =
StringManager.getLocalizedStringFromResourceDef(resourceDef, key,null, locale, null,false);
return retVal;

Just make sure that the resource bundle exist.
you can easily call this method using groovy


adf.object.getLocalizedValue("DEFULT_DEPT_NAME")  




Comments

Popular posts from this blog

Another user has changed the row with primary key oracle.jbo.Key[12 ].

Working With File Throgh WebUtill

Reading From File