Menu

#434 Numeric parameters render as null when propertyt comes from a custom ResourceBundle

undecided
open
nobody
None
5
2015-10-10
2015-08-06
Gavin Jones
No

If one has a 'custom' (or delegate) ResourceBundle - that is, one that extends java.util.ResourceBundle, numeric parameters to property strings come as null.

This may apply to all non-string parameters.

A workaround is to 'stringify' parameters before calling the bundle.

eg.) Some ResourceBundle that is not a true PropertyResourceBundle (can't extend it):

public class CustomBundle extends ResourceBundle {

ResourceBundle rb;

public CustomBundle(ResourceBundle propertyBundle) {
    this.rb = propertyBundle;
}

protected Object handleGetObject(String key) {
    return rb.getString(key);
}

public Enumeration<String> getKeys() {
    return this.rb.getKeys();
}

}

And the wrapped ResourceBundle (a true property file) has a property like:

property.key=This property has {0} parameters.

if we provide FreeMarker with one of these bundles and an Integer object named "param", the following throws a NullPointerExeption:

${bundleObj("property.key", param)}

The following prevents the exception, but the parameter comes out as null:

${bundleObj("property.key", param.string)}

The following is a workaround - stringify any such parameter first on the template:

<#assign stringCount = "${integerCount}">
${bundleObj("property.key", stringCount)}

ResourceBundle instances obtained from ResourceBundle.getBundle() are not affected.

Discussion

  • Dániel Dékány

    • Group: 2.3.22 --> undecided
     
  • Garvit Bansal

    Garvit Bansal - 2015-10-10

    Just want to clarify as I am newbie in this.
    I need to create template to stringify parameter other than string

     
  • Garvit Bansal

    Garvit Bansal - 2015-10-10

    Created class customResourceBuilder which stringify all parameter passed to it.

     

Log in to post a comment.