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:
''
) if the message with the single quotes contains at least one placeholders ({0}
) too;'
) 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?
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.
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.
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!
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.
ResourceBundleMessageSource
provides a flag called alwaysUseMessageFormat
that can be used if MessageFormat
should be applied to all messages.
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:
''
)See also Why Spring MessageSource arguments are not filled correctly in some locales.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With