Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 409
user339108 Avatar asked Dec 15 '10 11:12

user339108


4 Answers

You should escape the single quote as

key = {0}''s brush is {1} centimeters tall
like image 96
Raghuram Avatar answered Nov 11 '22 08:11

Raghuram


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
like image 28
Ralph Avatar answered Nov 11 '22 08:11

Ralph


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.

like image 7
sorrymissjackson Avatar answered Nov 11 '22 10:11

sorrymissjackson


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}'''".

like image 6
Aravind Yarram Avatar answered Nov 11 '22 10:11

Aravind Yarram