Details
-
Improvement
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
None
-
None
-
None
Description
The Page getMessage() method search message key in a resource bundle named after the Page class name (getClass().getName())
This is done loading a MessageMap instance in the getMessages() method.
It would be handy to have a default page resource bundle where keys are looked up if they are not found on the page specific bundle.
I was thinking of something like click-page (like click-control is default for controls).
This way you can:
1 - share default application wide keys
2 - choose to have a single bundle to maintain and translate
A discussion about implementation is on the development newsgroup:
http://news.gmane.org/find-root.php?group=gmane.comp.web.click.devel&article=498
The following is my implementation (that patches Click 0.18).
_____________________________ MessageMap.java ____________________________
package mypackage;
import java.util.*;
public class MessagesMap extends net.sf.click.util.MessagesMap {
private net.sf.click.util.MessagesMap parent;
public MessagesMap(String baseName, Locale locale,
net.sf.click.util.MessagesMap parent)
public MessagesMap(String baseName, Locale locale)
{ this(baseName, locale, null); } public Object get(Object key) {
if(parent == null)
else
{ return parent.get(key); }}
// ****** not sure about the following methods: is it right to consider the presence of the parent MessageMap to determine the result of the methods?
public boolean containsKey(Object key)
{ return super.containsKey(key) || (parent != null && parent.containsKey(key)); }public boolean containsValue(Object value)
{ return super.containsValue(value) || (parent != null && parent.containsValue(value)); } public Set keySet() {
Set keySet = super.keySet();
if(parent != null)
return keySet;
}
public Collection values() {
Collection values = super.values();
if(parent != null)
return values;
}
public Set entrySet() {
Set entrySet = super.entrySet();
if(parent != null)
return entrySet;
}
}
______________________ Page.java _____________________________
package mypackage;
import java.text.MessageFormat;
import java.util.Locale;
import java.util.Map;
import it.graphite.click.util.MessagesMap;
public class Page extends net.sf.click.Page {
public Map getMessages() {
if (messages == null) {
if (getContext() != null)
else
{ String msg = "Context not set cannot initialize messages"; throw new IllegalStateException(msg); } }
return messages;
}
// *********** also useful and missing in the Click 0.18 *********
public String getMessage(String name, Object arg) {
Object[] args = new Object[]
;
return getMessage(name, args);
}
public String getMessage(String name, Object[] args) {
if (args == null)
String value = getMessage(name);
return MessageFormat.format(value, args);
}
}