Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <a href="http://docs.oracle.com/javase/7/docs/api/java/util/ResourceBundle.html" rel="noreferrer"><code>ResourceBundle#getBundle()</code></a> uses under the covers <a href="http://docs.oracle.com/javase/7/docs/api/java/util/PropertyResourceBundle.html" rel="noreferrer"><code>PropertyResourceBundle</code></a> when a <code>.properties</code> file is specified. This in turn uses by default <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load(java.io.InputStream)" rel="noreferrer"><code>Properties#load(InputStream)</code></a> to load those properties files. As per <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load(java.io.InputStream)" rel="noreferrer">the javadoc</a>, they are by default read as ISO-8859-1. </p> <blockquote> <p><code>public void load(InputStream inStream) throws IOException</code></p> <p>Reads a property list (key and element pairs) from the input byte stream. The input stream is in a simple line-oriented format as specified in load(Reader) <strong>and is assumed to use the ISO 8859-1 character encoding</strong>; that is each byte is one Latin1 character. Characters not in Latin1, and certain special characters, are represented in keys and elements using Unicode escapes as defined in section 3.3 of The Java™ Language Specification.</p> </blockquote> <p>So, you'd need to save them as ISO-8859-1. If you have any characters beyond ISO-8859-1 range and you can't use <code>\uXXXX</code> off top of head and you're thus forced to save the file as UTF-8, then you'd need to use the <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/native2ascii.html" rel="noreferrer"><em>native2ascii</em></a> tool to convert an UTF-8 saved properties file to an ISO-8859-1 saved properties file wherein all uncovered characters are converted into <code>\uXXXX</code> format. The below example converts a UTF-8 encoded properties file <code>text_utf8.properties</code> to a valid ISO-8859-1 encoded properties file <code>text.properties</code>.</p> <pre>native2ascii -encoding UTF-8 text_utf8.properties text.properties</pre> <p>When using a sane IDE such as Eclipse, this is already automatically done when you create a <code>.properties</code> file in a Java based project and use Eclipse's own editor. Eclipse will transparently convert the characters beyond ISO-8859-1 range to <code>\uXXXX</code> format. See also below screenshots (note the "Properties" and "Source" tabs on bottom, click for large):</p> <p><a href="https://i.stack.imgur.com/iQWWG.png" rel="noreferrer"><img src="https://i.stack.imgur.com/iQWWGm.png" alt="&quot;Properties&quot; tab"></a> <a href="https://i.stack.imgur.com/kjOCb.png" rel="noreferrer"><img src="https://i.stack.imgur.com/kjOCbm.png" alt="&quot;Source&quot; tab"></a></p> <p>Alternatively, you could also create a custom <a href="http://docs.oracle.com/javase/7/docs/api/java/util/ResourceBundle.Control.html" rel="noreferrer"><code>ResourceBundle.Control</code></a> implementation wherein you explicitly read the properties files as UTF-8 using <a href="http://docs.oracle.com/javase/7/docs/api/java/io/InputStreamReader.html#InputStreamReader(java.io.InputStream,%20java.lang.String)" rel="noreferrer"><code>InputStreamReader</code></a>, so that you can just save them as UTF-8 without the need to hassle with <code>native2ascii</code>. Here's a kickoff example:</p> <pre><code>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; } } </code></pre> <p>This can be used as follows:</p> <pre><code>ResourceBundle bundle = ResourceBundle.getBundle("com.example.i18n.text", new UTF8Control()); </code></pre> <h3>See also:</h3> <ul> <li><a href="http://balusc.blogspot.com/2009/05/unicode-how-to-get-characters-right.html" rel="noreferrer">Unicode - How to get the characters right?</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload