I encountered this problem with message resource property files in Struts. The problem is: I have an entry in property file where in it takes few arguements {0}, {1} etc.
some.property.key = Hello {0}, Following is your account’s detail. \
Please note that your registration no. {1} \
is going to expire on {2}, kindly login into your \
account {3} and update it. Now when I put the value for the arguments {0}, {1} etc using
MessageFormat.format(body, argList)
, it does’nt fill arguments {1} and above. It only fills value for argument {0}. I removed the apostrophe (‘) from account’s and then it worked. Hence there must be some problem with the apostrophe in java properties files. So I googled a little and finally got this solution. The solution is: Simply use two apostrophe (”) instead of one. So I changed the code to:
some.property.key = Hello {0}, Following is your account”s detail. \
Please note that your registration no. {1} \
is going to expire on {2}, kindly login into your \
account {3} and update it. And it worked :)
Hi Viral,
Thanks for this post — it helped clear up this issue for me.
What I’m wondering, though, is what really triggered this problem? At first, apostrophes in properties were working fine, and they suddenly stopped working for new properties… while still working for the old ones. Very strange!
If you have any ideas as to the root cause, please let me know. Thanks again!
Yeah, just what I needed. Thanks…
I found the root cause of this problem.
While it is well documented in java.text.MessageFormat that apostrophes need to be escaped by using a double apostrophe, the real issue lies in a Spring class:
org.springframework.context.support.AbstractMessageSource (version 2.5.6 I think?)
In the method,
protected String getMessageInternal(String code, Object[] args, Locale locale)
I noticed that a decision to use java.text.MessageFormat is made based on the fact if the translated string has replacement parameters or not.
IMHO I think this is bad because now I have a need for double apostrophes only if the translated string in question has replacement parameters. If there are no parameters in the translated string, I don’t have to use the double apostrophe escaping mechanism. So my properties file looks like this.
sample.key1 = Translation for you. It’s very short
sample.key2 = Translation for {0}. It”s very short
@Trace – Thanks for the insight. :)
You”ve made my day!
thank you, that what i looked for
Thank you very much Trace. This was bugging me for quite a while and I am so relieved to learn why this is happening.
Nice post. This will give solution for many programmers. Thanks.