100

Apostrophe doesn't get translated properly when placed in a resource bundle.

key = {0}'s brush is {1} centimeters tall

(e.g. Sam's brush is 4 centimeters tall)

The apostrophe gets missed if I format the above key from a java.util.ResourceBundle What could be the problem here?

user339108
  • 12,613
  • 33
  • 81
  • 112
  • 5
    +1 I ran into this problem myself and wrote a small blog post about it. Maybe it can provide additional information for people running into this "feature": http://www.mscharhag.com/2013/10/single-quote-escaping-in-java-resource.html – micha Oct 04 '13 at 17:30

8 Answers8

141

You should escape the single quote as

key = {0}''s brush is {1} centimeters tall
Raghuram
  • 51,854
  • 11
  • 110
  • 122
37

I strongly belive that the problem is not the ressource bundle but the MessageFormater you use to print the message:

From MessageFormater java doc:

Within a String, '' (two single quotes ) represents a single quote. A QuotedString can contain arbitrary characters except single quotes; the surrounding single quotes are removed. An UnquotedString can contain arbitrary characters except single quotes and left curly brackets. Thus, a string that should result in the formatted message '{0}' can be written as '''{'0}'' or '''{0}'''.

So you need to write:

{0}''s brush is {1} centimeters tall
Ralph
  • 118,862
  • 56
  • 287
  • 383
8

Adding to @Ralph's answer: You will realize that this is a MessageFormat thing when you have a text like

text1=It's too late

versus

text2={0}''s too late

text1 would probably not run through a MessageFormater (e.g. spring has different code paths if arguments are passed or not), whereas text2 would. So if you used two single quotes in text1, they may / will display as such. So you'll need to check if any arguments get formatted in or not and use one or two single quotes accordingly.

sorrymissjackson
  • 2,395
  • 1
  • 19
  • 18
6

Look at the javadoc here

Within a String, "''" represents a single quote. A QuotedString can contain arbitrary characters except single quotes; the surrounding single quotes are removed. An UnquotedString can contain arbitrary characters except single quotes and left curly brackets. Thus, a string that should result in the formatted message "'{0}'" can be written as "'''{'0}''" or "'''{0}'''".

alfonx
  • 6,936
  • 2
  • 49
  • 58
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
  • I have issue when use double quote with a message without {} or no args. When that MessageFormat will not process and return raw message. You need to turn on option **alwaysUseMessageFormat** to MessageFormat always processes. – duyetpt Jun 14 '22 at 04:21
5

If you are completely stuck, as I was (none of the above worked), you can replace the apostrophe sign with its Unicode: \u0027. Remember you are always allowed to use UTF symbol in your properties file.

Nestor Milyaev
  • 5,845
  • 2
  • 35
  • 51
5

You need to double single quote i.e. {0}''s brush is {1} centimeters tall

Samuel Parsonage
  • 3,077
  • 1
  • 18
  • 21
2

Consider using Properties Editor plugin (for Eclipse)

http://propedit.sourceforge.jp/index_en.html

Danubian Sailor
  • 1
  • 38
  • 145
  • 223
1

For everyone that has Android problems in the string.xml, use \'\' instead of single quote.

Uriel Frankel
  • 14,304
  • 8
  • 47
  • 69