M
- a class providing message keys and translations.public interface MessagesProvider<M>
A class providing localized messages looks like:
public class Messages { public String MyClass_MESSAGE_1; public String MyClass_MESSAGE_2; public String MyOtherClass_MESSAGE; private static final MessagesProvider<Messages> PROVIDER = ServletNLS.createMessagesProvider(Messages.class); public static Messages getInstance(HttpServletRequest request) { return PROVIDER.getInstance(request); } }
Translated messages must be defined in *.properties
files along the implemented Messages
class, organized like:
messages.properties
: contains English messagesmessages_fr.properties
: contains French messagesA class can retrieve a localized message using:
String aLocalizedMessage = Messages.getInstance(request).MyClass_MESSAGE_1; String aMessageWithArg = NLS.bind(Messages.getInstance(request).MyOtherClass_MESSAGE, "hello");
The language to use is determined using the Accept-Language
header provided by the servlet request.
MessagesProvider iterates on the locales provided by the request, in order of preference,
and returns the translations if a properties file is found for the locale.
It returns the default translations provided by messages.properties
if there's no match.
ServletNLS.createMessagesProvider(Class)
Modifier and Type | Method and Description |
---|---|
M |
getInstance(HttpServletRequest request)
Provides translations depending on the preferred locale configured on the servlet request.
|
M getInstance(HttpServletRequest request)
request
- the servlet request.