How do I specify values in a properties file so they can be retrieved using ResourceBundle#getStringArray?

A Properties object can hold Objects, not just Strings. That tends to be forgotten because they’re overwhelmingly used to load .properties files, and so often will only contain Strings. The documentation indicates that calling bundle.getStringArray(key) is equivalent to calling (String[]) bundle.getObject(key). That’s the problem: the value isn’t a String[], it’s a String. I’d suggest storing … Read more

String concatenation in EL for dynamic ResourceBundle key

If you’re already on Servlet 3.1 / EL 3.0 (Tomcat 8, WildFly 8, GlassFish 4, etc), make use of new EL 3.0 += operator: <h:outputText value=”#{msg[‘entry’ += managedBean.entryIndex]}” /> If you’re only on Servlet 3.0 / EL 2.2 (Tomcat 7, JBoss AS 6/7, GlassFish 3, etc), make use of new EL 2.2 ability to directly … Read more

How to load a resource bundle from a file resource in Java?

As long as you name your resource bundle files correctly (with a .properties extension), then this works: File file = new File(“C:\\temp”); URL[] urls = {file.toURI().toURL()}; ClassLoader loader = new URLClassLoader(urls); ResourceBundle rb = ResourceBundle.getBundle(“myResource”, Locale.getDefault(), loader); where “c:\temp” is the external folder (NOT on the classpath) holding the property files, and “myResource” relates to … Read more

internationalization in JSF with ResourceBundle entries which are loaded from database

Internationalization/localization should preferably be entirely done in the view side. The model shouldn’t be aware of this. In JSF, the <resource-bundle> entry in faces-config.xml and the <f:loadBundle> in XHTML can also point to a fullworthy ResourceBundle class instead of basename of .properties files. In Java SE 6 there’s a new ResourceBundle.Control API available which allows … Read more