Apostrophe creating problem in properties file in java/struts

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 :)

View Comments

  • 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!

  • 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

  • Thank you very much Trace. This was bugging me for quite a while and I am so relieved to learn why this is happening.

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

4 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

4 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

4 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

4 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

4 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

5 years ago