Search code examples
javastruts2resourcebundle

Multiple application properties in struts2 and need to change property file in java based on somecondition?


I am using

struts.custom.i18n.resources=file1,file2

file1(ApplicationResources1.properties)

enter.user = User name

file2(ApplicationResources2.properties)

enter.user = User name1

based on some condition I need to switch over the file from file1 to file2 in action.? I have used the following code...Both files are get loaded but i need to refer any of these files based on condition...

Struts.xml
<constant name="struts.custom.i18n.resources"
    value="ApplicationResources2,ApplicationResources1" />

Test.java

if(condition){
    LocalizedTextUtil.clearDefaultResourceBundles();
LocalizedTextUtil.addDefaultResourceBundle("ApplicationResources1.properties");
LocalizedTextUtil.setReloadBundles(true); 
}else{
    LocalizedTextUtil.clearDefaultResourceBundles();
LocalizedTextUtil.addDefaultResourceBundle("ApplicationResources2.properties");
LocalizedTextUtil.setReloadBundles(true); 
}

Solution

  • If you properties files names are ApplicationResources1.properties and ApplicationResources2.properties then you need to use that names in addDefaultResourceBundle method of LocalizedTextUtil.

    if(condition) {
      LocalizedTextUtil.clearDefaultResourceBundles();
      LocalizedTextUtil.addDefaultResourceBundle("ApplicationResources1");
      LocalizedTextUtil.setReloadBundles(true); 
    }else {
      LocalizedTextUtil.clearDefaultResourceBundles();
      LocalizedTextUtil.addDefaultResourceBundle("ApplicationResources2");
      LocalizedTextUtil.setReloadBundles(true); 
    }