Why is the Swedish text from the resource pack displayed as gibberish?

Possible duplicate:
How to use UTF-8 in resource properties with ResourceBundle

I want to enable the internationalization of a Java Swing application. I use the package file to keep all tags in it.

As a test, I tried to set the Swedish title to JButton . Therefore, in the package file, I wrote:

 nextStepButton=nästa 

And in Java code, I wrote:

 nextStepButton.setText(bundle.getString("nextStepButton")); 

But button header characters are not displayed correctly at runtime:
alt text

I am using a Tahoma font that supports Unicode. When I set the button name manually through the code, it looks fine:

 nextStepButton.setText("nästa"); 

Any idea why this fails in the bundle file?

--------------------------------------------> Edit: header encoding:
I tried to encode the text coming from the package file using the code:

 nextStepButton.setText(new String(bundle.getString("nextStepButton").getBytes("UTF-8"))); 

And still the result:
alt text

+6
java swing internationalization mojibake resourcebundle
source share
3 answers

According to javadoc, property files are read using ISO-8859-1.

.. The input / output stream is encoded in ISO 8859-1 character encoding. Characters that cannot be directly represented in this encoding can be written using Unicode escapes; Only one u character is allowed in an escape sequence. The native2ascii tool can be used to convert property files to and from other character encodings.

In addition to using native2ascii to convert UTF-8 property files to ISO-8859-1 property files, you can also use a custom ResourceBundle.Control so that you can control the loading of property files and use UTF-8. Here is an example run:

 public class UTF8Control extends Control { public ResourceBundle newBundle (String baseName, Locale locale, String format, ClassLoader loader, boolean reload) throws IllegalAccessException, InstantiationException, IOException { // The below is a copy of the default implementation. String bundleName = toBundleName(baseName, locale); String resourceName = toResourceName(bundleName, "properties"); ResourceBundle bundle = null; InputStream stream = null; if (reload) { URL url = loader.getResource(resourceName); if (url != null) { URLConnection connection = url.openConnection(); if (connection != null) { connection.setUseCaches(false); stream = connection.getInputStream(); } } } else { stream = loader.getResourceAsStream(resourceName); } if (stream != null) { try { // Only this line is changed to make it to read properties files as UTF-8. bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8")); } finally { stream.close(); } } return bundle; } } 

Use it as follows:

 ResourceBundle bundle = ResourceBundle.getBundle("com.example.i18n.text", new UTF8Control()); 

This way, you don’t have to bother with the native2ascii tool, and you get more convenient files of supported properties.

See also:

+8
source share

Take a look at the Java Internationalization FAQ . If you put non-ASCII characters in your .properties file, you must convert it using the native2ascii tool. Then everything should work.

+6
source share

The problem is that the resource package properties file is encoded in UTF-8, but your application loads it using Latin-1.

If you take “LATIN SMALL A WITH DIAERESIS” (E4 in Latin-1 or 0000E4 as a Unicode codeword) and represent it as UTF-8, you will get C3 A4. If you then process them as latin-1 bytes, you get "LATIN CAPITAL LETTER A WITH TILDE" and the square symbol "CURRENCY SIGN" ... that's how the characters appear in the screenshot of the button!

(By the way, here is the neologism for distortion that you get as a result of using the wrong character encoding ... mojibake . Your friends using it in a conversation.)

+3
source share

All Articles