I'm using a resourcebundle to read a properties file based on locale. (Lang_en_US.properties, ..)
The resourcebundle is read as iso-8859-1 (standard?).
ResourceBundle rb= ResourceBundle.getBundle("Lang", locale);
The resourcebundle is then used throughout the Spring/JSF web-application to generate the front-end text.
<h:outputText value="#{msg['message.example']}" />
But I believe this is irrelevant, as debugging shows that the text is already gibberish right after rb getMessage is called.
// returns gibberish:
log.trace(rb.getMessage("l_SampleText"));
I believe you are correct in assuming the resourcebundle is read in as iso-8859-1.
Javadoc of Properties class
(source)
Are you sure your properties file is saved under the iso-8859-1 format?
I believe Notepad++ provides the functionality to at least check the encoding, if not convert it.
Since you are using UTF-8 characters that might not be encoded properly in ISO-8859-1, you have 2 options
Use nativetoascii tool to escape the characters in the bundle so that all the chars are read properly
Use Spring's MessageSources's bundle support which supports files in UTF-8 encoding
Related
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
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:
Replace every special character with an encoded form, e.g. by using unicode point in the form \u00E4 for ä etc.
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.
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>
I was working with an app that loads a .properties file with java.util.Properties like this:
Properties _properties = new Properties();
_properties.load(new FileInputStream("app.properties"));
The properties file (initially) was this:
app=myApp
dbLogin=myDbLogin
version=0.9.8.10
server=1
freq=10000
stateGap=360000
The strange thing was that when I called _properties.getProperty("app"), it always returned null, however I could load all of the other properties without any issues. I solved the problem by adding a comment to the top of the properties file, then everything worked fine.
My question is: Why does Java do this? I can't seem to find any documentation about this, and it seems counter-intuitive.
Thanks to #KonstantinV.Salikhov and #pms for their help in hunting this down; I decided to post the answer that was discovered to save people hunting through the comments.
The problem was that my file was the wrong encoding, as mentioned here: http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html
The load(Reader) / store(Writer, String) methods load and store properties from and to a character based stream in a simple line-oriented format specified below. The load(InputStream) / store(OutputStream, String) methods work the same way as the load(Reader)/store(Writer, String) pair, except the input/output stream is encoded in ISO 8859-1 character encoding.
(Emphasis mine).
I changed the encoding of the properties file to ISO-8859-1 and everything worked.
Java does not handle the BOM correctly – you can see it in the properties as key. It is possible to save the file UTF-8 but without BOM. In vim for instance
:set nobomb
See vim wiki
First time use FreeMarker on JAVA project and stack on configure the chinese character.
I tried a lot of examples to fix the code like below, but it still not able to make it.
// Free-marker configuration object
Configuration conf = new Configuration();
conf.setTemplateLoader(new ClassTemplateLoader(getClass(), "/"));
conf.setLocale(Locale.CHINA);
conf.setDefaultEncoding("UTF-8");
// Load template from source folder
Template template = conf.getTemplate(templatePath);
template.setEncoding("UTF-8");
// Get Free-Marker output value
Writer output = new StringWriter();
template.process(input, output);
// Map Email Full Content
EmailNotification email = new EmailNotification();
email.setSubject(subject);
.......
Saw some example request to make changes on the freemarker.properties but i have no this file. I just import the .jar file and use it.
Kindly advise what should i do to make it display chinese character.
What exactly is the problem?
Anyway, cfg.setDefaultEncoding("UTF-8"); should be enough, assuming your template files are indeed in UTF-8. But, another place where you have to ensure proper encoding is when you convert the the template output back to "binary" from UNICODE text. So FreeMarker sends its output into a Writer, so everything is UNICODE so far, but then you will have an OutputStreamWriter or something like that, and that has to use charset (UTF-8 probably) that can encode Chinese characters.
You need to change your file encoding of your .ftl template files by saving over them in your IDE or notepad, and changing the encoding in the save dialog.
There should be an Encoding dropdown at the bottom of the save dialog.
I'm trying to read Hebrew values from a .properties file and I get gibberish. I've tried a couple of ways, including changing the file's encoding (Cp1255, ISO-8859-8, UTF-8), adding a -file.encoding to the arguments and nothing helped.
This issue was raised during our migration to Weblogic from IAS (OC4J container), I noticed the javascript messages (which are read from a .properties file) appear as ???? ???, which does not happen on the OC4J. However, this only applies to data read from .properties files, everything else is shown fine.
I've been googling for a couple of days now and I haven't been able to come up with a solution.
EDIT:
What I tried at home
ResourceBundle rb = ResourceBundle.getBundle("test");
System.out.println(rb.getString("test"));
This is what test.properties looks like:
test שלום
Output is: ùìåí
Since you have an instance of ResourceBundle and you can get the ISO-8859-1 encoded String with:
String strISO = rb.getString("test");
You can then convert this to UTF-8 and print it by using:
System.out.println(new String(strISO.getBytes("ISO-8859-1"), "UTF-8"));
Files for ResourceBundle should be in ISO 8859-1 encoding or in \uXXXX format, just as for Properties. See more at How to use UTF-8 in resource properties with ResourceBundle
I want to write "Arabic" in the message resource bundle (properties) file but when I try to save it I get this error:
"Save couldn't be completed
Some characters cannot be mapped using "ISO-85591-1" character encoding. Either change encoding or remove the character ..."
Can anyone guide please?
I want to write:
global.username = اسم المستخدم
How should I write the Arabic of "username" in properties file? So, that internationalization works..
BR
SC
http://sourceforge.net/projects/eclipse-rbe/
You can use the above plugin for eclipse IDE to make the Unicode conversion for you.
As described in the class reference for "Properties"
The load(Reader) / store(Writer, String) methods load and store properties from and to
a character based stream in a simple line-oriented format specified below.
The load(InputStream) / store(OutputStream, String) methods work the same way as the
load(Reader)/store(Writer, String) pair, except 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 a single 'u' character is allowed
in an escape sequence. The native2ascii tool can be used to convert property files to
and from other character encodings.
Properties-based resource bundles must be encoded in ISO-8859-1 to use the default loading mechanism, but I have successfully used this code to allow the properties files to be encoded in UTF-8:
private static class ResourceControl extends ResourceBundle.Control {
#Override
public ResourceBundle newBundle(String baseName, Locale locale,
String format, ClassLoader loader, boolean reload)
throws IllegalAccessException, InstantiationException,
IOException {
String bundlename = toBundleName(baseName, locale);
String resName = toResourceName(bundlename, "properties");
InputStream stream = loader.getResourceAsStream(resName);
return new PropertyResourceBundle(new InputStreamReader(stream,
"UTF-8"));
}
}
Then of course you have to change the encoding of the file itself to UTF-8 in your IDE, and can use it like this:
ResourceBundle bundle = ResourceBundle.getBundle(
"package.Bundle", new ResourceControl());
new String(ret.getBytes("ISO-8859-1"), "UTF-8"); worked for me.
property file saved in ISO-8859-1 Encodiing.
If you are using Eclipse, you can choose "Window-->Preferences" and then filter on "content types". Then you should be able to set the default encoding. There's a screen shot showing this at the top of this post.
This is mainly an editor configuration issue. If you're working in Windows, you can edit the text in an editor that supports UTF-8. Notepad or Eclipse built-in editor should be more than enough, provided you've saved file as UTF-8. In Linux, I've used gedit and emacs successfully. In Notepad, you can do this by clicking 'Save As' button and choosing 'UTF-8' encoding. Other editors should have similar feature. Some editors might require font change in order to display letters correctly, but it seems that you don't have this issue.
Having said that, there are other steps to consider when performing i18n for arabic. You can find some useful links below. Make sure to use native2ascii on properties file before using it otherwise it might not work. I spent a lot of time until I figured this one out.
Tomcat webapps
Using nativ2ascii with properties files
Besides native2ascii tool mentioned in other answers there is a java Open Source library that can provide conversion functionality to be used in code
Library MgntUtils has a Utility that converts Strings in any language (including special characters and emojis to unicode sequence and vise versa:
result = "Hello World";
result = StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence(result);
System.out.println(result);
result = StringUnicodeEncoderDecoder.decodeUnicodeSequenceToString(result);
System.out.println(result);
The output of this code is:
\u0048\u0065\u006c\u006c\u006f\u0020\u0057\u006f\u0072\u006c\u0064
Hello World
The library can be found at Maven Central or at Github It comes as maven artifact and with sources and javadoc
Here is javadoc for the class StringUnicodeEncoderDecoder