Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a single rule to cope with single quotes in writing Spring Resource Boundle?

Spring's ResourceBundleMessageSource uses MessageFormat for replacing placeholders ({0}) inside messages.

MessageFormat requires that single quotes (') are escaped using two single quotes ('') (see: MessageFormat Javadoc). However, by default messages that does not contain any arguments will not be parsed by MessageFormat, so single quotes in messages without arguments don't need to be escaped.

So your translator have to be aware of two rules in writing and maintaining resource bundle:

  • write ('') if the message with the single quotes contains at least one placeholders ({0}) too;
  • write (') if the message with the single quotes contains none placeholders.

Is there a single rule to cope with single quotes in writing Spring Resource Boundle?

like image 385
taringamberini Avatar asked Mar 13 '14 13:03

taringamberini


People also ask

How do you handle a single quote in a string in Java?

A string literal is bracketed by either single quotes ( ' ) or double quotes ( " ). When single quotes bracket a string literal, the value of the literal is the value within the quotes.

How do you handle a single quote in a string?

A single quote is not used where there is already a quoted string. So you can overcome this issue by using a backslash following the single quote. Here the backslash and a quote are used in the “don't” word. The whole string is accompanied by the '$' sign at the start of the declaration of the variable.

Is it better to use single or double quotes in Javascript?

Both single (' ') and double (" ") quotes are used to represent a string in Javascript. Choosing a quoting style is up to you and there is no special semantics for one style over the other. Nevertheless, it is important to note that there is no type for a single character in javascript, everything is always a string!

How do you pass a single quote in a shell script?

Enclosing characters in single quotation marks (') holds onto the literal value of each character within the quotes. In simpler words, the shell will interpret the enclosed text within single quotes literally and will not interpolate anything including variables, backticks, certain \ escapes, etc.


1 Answers

ResourceBundleMessageSource provides a flag called alwaysUseMessageFormat that can be used if MessageFormat should be applied to all messages.

The single rule is...

Configure one time for all your resource boundle with:

<bean 
    id="messageSource" 
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="alwaysUseMessageFormat" value="true" />
    ...
</bean>

and your translator have to be aware of a single rule in writing and maintaining resource bundle:

  • write always ('')

See also Why Spring MessageSource arguments are not filled correctly in some locales.

like image 74
taringamberini Avatar answered Oct 18 '22 04:10

taringamberini