0

I have a property file with the following key and value:

elsi.log.status.1              = Keine Änderungen

But the character Ä is not properly displayed on my webpage. The output is �

But if i use the faces-config and then directly display a message from the xhtml the message is displayed same as in the property file

This is the method used to get values from the propertyfile in java. When I debug the value is allready wrong here (bundle.getString(key) returns Keine �nderungen)

    public static String getString(String key) {
    try {
        Locale locale = CurrentEnvironment.getLocale();
        ResourceBundle bundle = ResourceBundle.getBundle(BUNDLE_NAME, locale);
        if (bundle != null) {
            return bundle.getString(key);
        }
    } catch (MissingResourceException e) {
        return '!' + key + '!';
    }
    return '!' + key + '!';
}

Direct output with xhtml works

<h:outputText value="#{messages.elsi_copyright}" />

I also noticed that replacing the chars in the propertyfile with hexcodes helped but i want to know if it is possible to do this otherwise.

Thanks for your help

0riginal
  • 137
  • 1
  • 11

2 Answers2

5

The problem is that ResourceBundle.getBundle() uses ISO Latin-1 encoding for reading the bundles and hence can't interpret UTF-8 files (which would be the case when inserting non-Latin-1 characters like ä etc.).

Currently I can think of 2 solutions:

  1. Replace every special character with an encoded form, e.g. by using unicode point in the form \u00E4 for ä etc.
  2. since Java 5 ResourceBundle provides a means to read UTF-8 files, although the internal caching and fallback mechanism won't work in that case and you'd have to do that yourself.

Update: instead of an example for no. 2, please have a look here: How to use UTF-8 in resource properties with ResourceBundle

There are a lot of good resources that should help you deal with bundles containing umlauts etc. in a way that fits your needs.

Community
  • 1
  • 1
Thomas
  • 87,414
  • 12
  • 119
  • 157
  • @0riginal I'll have a look, but we also had that problem and are using the 2 solutions I mentioned. The main problem seems to be that something (e.g. your editor) causes the file to be encoded with UTF-8 if a non-Latin-1 (non-ASCII ?) character is contained. – Thomas Apr 14 '15 at 14:01
  • A poorly implemented text editor might save the replacement in UTF-8 form; the text file data will then look like this: 0x66 0xEF 0xBF 0xBD 0x72, which will be displayed in ISO-8859-1 as "f�r" - [wikipedia](http://en.wikipedia.org/wiki/Specials_%28Unicode_block%29) – 0riginal Apr 14 '15 at 14:05
  • @0riginal yes, Eclipse would do that and that's what we use as well. – Thomas Apr 14 '15 at 14:06
  • I looked in the eclipse preferences and I saw that it is possible to change encoding for different types and .properties ar set to ISO-8859-1 – 0riginal Apr 14 '15 at 14:08
  • I ended up converting my properties to UTF-8 and then used the UTF8Control. This works perfectly but you have to rewrite some characters in the new UTF-8 files. – 0riginal Apr 15 '15 at 09:00
0

Property files are hard-encoded in ISO-8859-1. If your page has another encoding (say, UTF-8), you will encouter encoding problems.

Fortunately, Java ResourceBundles provide an alternative way : using XML files. You don't have to change anything in your code, just use the XML format as described in the javadoc of the java.util.Properties file. Those files can be encoded in any encoding, provided you specify it in the XML header.

Its doctype is

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

Example :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="com.compant.key1">value1</entry>
<entry key="com.company.key2">value2</entry>
...
</properties>
Olivier Croisier
  • 6,139
  • 25
  • 34
  • my webpage is iso-8859-1 and it works with jsf but not with java Unfortunately I'm not able to change the properties to xml :/ – 0riginal Apr 14 '15 at 13:59