public class MessagesHelper extends Object
locale
.
The locale dependent strings have to be stored in property files. They are accessed via Java's
ResourceBundle
. To access the property files, they all have to
be in the same package and must start with the same prefix. Therefore the set's qualified name is
the package name, followed by a dot (.), followed by the prefix, e.g.
"org.faktorips.internal.messages". For further information see the
ResourceBundle
documentation.
In order to have a fallback if a message was requested in a not existing locale
ResourceBundle
provides a strategy to find a message anyway: If the message was not found
the fallback strategy will try to find a resource for the system default language. If this also
does not succeed a resource bundle without language specification would be taken. This leads to a
bad behavior: Imagine you provide English as your default language and hence having your English
translation in a property file without locale suffix. Additionally you provide a German
translation with suffix _de. Now you start your runtime with system default language German but
you try to get a message in English. You would always get the German one because the strategy
does not find a resource for _en but finds one for _de (your system default). The file without
suffix would never be read. To avoid this problem we recommend to add a language suffix to every
resource. Additionally to get a fallback to your default language you have to provide the default
language to the MessagesHelper
. When calling getMessage(String, Locale)
) the we
first check for the primary language, second then the ResourceBundle
will check for
system default language and third we would provide the message in specified default language.
If a localized String contains sections that have to be replaced with replacements before the
String is presented to a user, you can use the method with replacement objects as a parameter.
The mechanism used here is the one of MessageFormat
.
The property file loaded by the ResourceBundle
is cached internally by
SoftReference
s. So you do not have to worry about performance problems instantiation this
class multiple times.
Example:
In the message "The sum insured must be at least {0}." the sum insured must be inserted at runtime depending on the chosen product.
Constructor and Description |
---|
MessagesHelper(String qualifiedName,
ClassLoader loader,
Locale defaultLocale)
Creates a new StringsSet with the indicated qualified name.
|
Modifier and Type | Method and Description |
---|---|
String |
getMessage(String key,
Locale locale)
Getting the message for the given key in the specified locale.
|
String |
getMessage(String key,
Locale locale,
Object... replacements)
Getting the message for the given key in the specified language.
|
String |
getMessageOr(String key,
Locale locale,
String fallBack)
Getting the message for the given key in the specified language.
|
public MessagesHelper(String qualifiedName, ClassLoader loader, Locale defaultLocale)
qualifiedName
- The qualified name of your resource without suffix nor extension for
".properties" example org.sample.messagesloader
- The ClassLoader
to load the ResourceBundle
defaultLocale
- If no message was found the system default locale is used as fallback.
If there is also no resource bundle in system's default language we try to find a
message in defaultLocalepublic String getMessage(String key, Locale locale)
key
- the key of the messagelocale
- the locale of the message you want to getpublic String getMessage(String key, Locale locale, Object... replacements)
MessageFormat
.key
- The key of the messagelocale
- The locale of the message you want to getreplacements
- the replacements in the message textpublic String getMessageOr(String key, Locale locale, String fallBack)
key
- The key to identify the messagelocale
- the locale of the expected messagefallBack
- a fall back text if there is no messageCopyright © 2018. All rights reserved.