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

JavaResourcebundle

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

You should escape the single quote as

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

Solution 2 - Java

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

Solution 3 - Java

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.

Solution 4 - Java

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

Solution 5 - Java

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

Solution 6 - Java

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.

Solution 7 - Java

Consider using Properties Editor plugin (for Eclipse)

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

Solution 8 - Java

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser339108View Question on Stackoverflow
Solution 1 - JavaRaghuramView Answer on Stackoverflow
Solution 2 - JavaRalphView Answer on Stackoverflow
Solution 3 - JavasorrymissjacksonView Answer on Stackoverflow
Solution 4 - JavaAravind YarramView Answer on Stackoverflow
Solution 5 - JavaSamuel ParsonageView Answer on Stackoverflow
Solution 6 - JavaNestor MilyaevView Answer on Stackoverflow
Solution 7 - JavaDanubian SailorView Answer on Stackoverflow
Solution 8 - JavaUriel FrankelView Answer on Stackoverflow