Java properties files localization & character encoding issue
I am working on a Java web application which is now in the final stages of development and one of the remaining things to be done is the localization. We are using properties files for every supported locale. The issue I have spotted is that some unicode characters do no appear correctly in the web browser. The web pages have UTF-8 encoding specified in the charset meta tag and the browser has correctly detected it (In Firefox View->Character Encoding the correct one seems to be selected). I believe the issue comes from the fact that while the application displays text as UTF-8, the properties files are saved in ISO*xxxx encoding, which happens to be some eclipse default setting. I have found a similar question here: Java properties UTF-8 encoding in Eclipse which advices me to install the Resource Bundle Plug-in. I installed and used the plug-in to edit the corresponding properties, but I still have the issue. Is there a quick solution (I mean a solution that will not cause too much changes in the application, since it is in almost finished stage) that will overcome the problem I am experiencing. Maybe I should mention that I am developing and observing the problem under Ubuntu Linux OS using Firefox 7. Thanks in advance. Edit: I did not mention an important matter. My user interface is written in GWT and the properties are exposed by an interface which has annotations on the getters that GWT uses to internally create an implementation of that interface and link to the corresponding property. So I guess I do not have much control on how properties are actually being read, or at least I do not know how to do it in GWT.
Perhaps try having some of the properties echoed out to the console or a log file when they’re retrieved. See what’s actually being obtained from the properties file.
I have added some clarifications for my case. I am using GWT which automatically resolves the properties and it seems I’ve no control on how are the properties files actually read.
Properties File — encoding
This tutorial explains step by step to read and write a properties file in spring boot with encoding default and UTF-8 example with example.
Sometimes, We want to read the properties file that contains non-English characters.
For example, the properties file contains UTF-8 encoding characters. The default encoding for properties file reading is ISO-8859-1 .
Spring framework loads the properties file in default encoding.
In this tutorial, you will learn how to read and write the content of a property file with a specified encoding in Java.
How to configure encoding for Spring boot to read
In this example, you will learn how to Read keys and their values from a property file spring boot application.
Let’s have a properties file — application_fr.properties which contains non-English characters.
There are many ways properties file read in spring core and boot applications.
create a Bean object returning PropertiesFactoryBean using the @Bean annotation
set encoding using setFileEncoding method
configure to read properties file from ClassPathResource setLocation method
Next, you can access the properties file mapped to member variables using @Value annotation with the below syntax
Another way, using @PropertySource annotation with value contains the name of file and encoding to `UTF-8
@PropertySource(value = “classpath:/application_properties.properties”, encoding=“UTF-8”)
read individual properties with the getProperty method with encoding
How to read from property file containing utf 8 character in java
Reading properties file is an easy way to do it in java.
You can apply encoding for file input stream as seen in the below example
catch (FileNotFoundException fie) < fie.printStackTrace(); >catch (IOException e) < e.printStackTrace(); >System.out.println(properties.getProperty("hostname")); Set keys = properties.stringPropertyNames(); for (String key : keys) < System.out.println(key + " - " + properties.getProperty(key)); >> >
Conclusion
You learned how to configure properties files to read UTF-8 character encoding in spring framework and boot as well as Java.