javajsfutf-8xpagesfacescontext

Sending UTF-8 message to Xpages


I have an XPages application for Swedish users.

When I am sending a message to the facescontext via

msg = propStrings.getProperty("gen_CustDateDecided") ;
FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.addMessage("msgBox", new javax.faces.application.FacesMessage(msg));

The text for the that is broadcasted resides on a properties file, with UTF-8 encoding.

When loading the properties file I make sure it is read in UTF-format:

private Properties getPropertiesFromFile(String fileName) {
        Properties prop = new Properties();
        try {
            InputStream is = FacesContextEx.getCurrentInstance().getExternalContext().getResourceAsStream(fileName);
            BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            prop.load(r);
        } catch (Exception e) {
            XspOpenLogUtil.logEvent(null, null, fileName, Level.WARNING, null);
        }
        return prop;
    }

The text that appears on the messages control on the Xpage: Senaste beslutsdatum i kundkommittén

The text in the properties file: gen_CustDateDecided=Senaste beslutsdatum i kundkommittén

What have I done wrong?


Solution

  • Property files are not encoded as UTF-8. They are ISO 8859-1. You need to encode them accordingly. Easiest is to just use a few lines in a standalone Java class to save properties. Takes care of encoding.

    More details here: How to use UTF-8 in resource properties with ResourceBundle