- Create list resource bundle file for default and supported locale. And read label value from xml file.
- This approach gives a facility to change label or message value, without compilation and redeployment of application.
- To do this first crate a xml file containing key and value pair, so that you can use label according to their key.
- This method also give an option to use single file for different application, which are used as a library.
- Here you can write code to read xml file and assign key value pair to an object of resource bundle class.
public class MyResources extends ListResourceBundle {
public MyResources() {
super();
}
private static final Object[][] contents =
{ { "", "" }, { "", "" }, { "", "" } };
protected Object[][] getContents() {
try {
File file = new File("C:\\Users\\admin\\Desktop\\locale\\Resource.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("label");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element)nNode;
contents[temp][0] = getTagValue("key", eElement);
contents[temp][1] = getTagValue("value", eElement);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return contents;
}
private static String getTagValue(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node)nlList.item(0);
return nValue.getNodeValue();
}
}
Ok... I tried this, but can you tell me how do I configure my app to use my custom bundle instead of the default one?
ReplyDeletehello ,
DeleteFirst follow following link and try to add resource using java resource file. if that works fine then modify resource file and use XML for that.
http://docs.oracle.com/cd/E15051_01/web.1111/b31973/af_global.htm
regards
Rohit