javalocalizationproperties-file

How to use single quotes with MessageFormat


On my current project, we are using properties files for strings. Those strings are then "formatted" using MessageFormat. Unfortunately, MessagFormat has a handling of single quotes that becomes a bit of a hindrance in languages, such as French, which use a lot of apostrophes.

For instance, suppose we have this entry

login.userUnknown=User {0} does not exist

When this gets translated into French, we get:

login.userUnknown=L'utilisateur {0} n'existe pas

This, MessageFormat does not like...

And I, do not like the following, i.e. having to use double quotes:

login.userUnknown=L''utilisateur {0} n''existe pas

The reason I don't like it is that it causes spellchecking errors everywhere.

Question: I am looking for an alternative to the instruction below, an alternative that does not need doubling quotes but still uses positional placeholders ({0}, {1}ā€¦). Is there anything else that can I use?

MessageFormat.format(Messages.getString("login.userUnkown"), username);

Solution

  • No there is no other way as it is how we are supposed to do it according to the javadoc.

    A single quote itself must be represented by doubled single quotes '' throughout a String

    As workaround, what you could do is doing it programmatically using replace("'", "''") or for this particular use case you could use the apostrophe character instead which is ā€™ it would be even more correct actually than using a single quote.