How to remove others ??? when the message is not found in the kit

In JSF 2.0, if the message is not found in the message set, then by default the key is surrounded ??? . This is a very useful hint during development. However, in my particular case, I really would like those ??? were not present. I prefer that only the key is displayed.

eg. when i do

 #{msg.hello} 

and the key "hello" does not exist, the page displays

??? Hello???

but I would like to show the bare key

Hello

The message bundle is uploaded to the JSF page as follows:

 <f:loadBundle basename="resources.text" var="msg" /> 

The <f:loadBundle> does not seem to have an attribute to control the way values ​​are received from this package. Should I overwrite a class or how to intercept the way I get messages from a package?

I found a very interesting article on this subject: Context-sensitive resource entries in JavaServer Faces applications - going beyond local languages, regions, and variations . However, in my case, I just want to omit ??? . I think this solution is rather complicated. How can I achieve this?

+7
jsf resourcebundle
Jun 23 '11 at 8:23
source share
2 answers

basename can point to a full ResourceBundle class. For example.

 <f:loadBundle basename="resources.Text" var="msg" /> 

from

 package resources; public class Text extends ResourceBundle { public Text() { setParent(getBundle("resources.text", FacesContext.getCurrentInstance().getViewRoot().getLocale())); } @Override public Enumeration<String> getKeys() { return parent.getKeys(); } @Override protected Object handleGetObject(String key) { return parent.getObject(key); } } 

You can override message processing in handleGetObject . JSF by default (by specification) calls getObject() , catches MissingResourceException and returns "???" + key + "???" "???" + key + "???" when capturing. You can do it differently.

 @Override protected Object handleGetObject(String key) { try { return parent.getObject(key); } catch (MissingResourceException e) { return key; } } 
+7
Jun 23 2018-11-11T00:
source share

You can also create a simple bean that takes care of string manipulation. This approach is much better if you do not need to remove the default environment everywhere, but only in certain places. The second function is much safer to use, since it also takes into account the case when the translation starts and ends ??? ,

 @ApplicationScoped @Named public class LocaleUtils { public String getMessage(String s) { return clearMessage(s); } public Object getMessage(ResourceBundle propertyResourceBundle, String key) { try { return propertyResourceBundle.getObject(key); } catch (MissingResourceException e) { return clearMessage(key); } } private static String clearMessage(String s) { String clearMessage = s; String prefix = "???", suffix = "???"; if (s != null && s.startsWith(prefix) && s.endsWith(suffix)) { s = s.substring(prefix.length()); clearMessage = s.substring(0, s.length() - suffix.length()); } return clearMessage; } } 

Using:

 <h:outputText value="#{localeUtils.getMessage(msg['hello'])}"/> <h:outputText value="#{localeUtils.getMessage(msg, 'hello')}"/> 
0
Jun 10 '19 at 10:23
source share



All Articles