public class ServletNLS extends Object
This class requires OSGi to be running.
Modifier and Type | Method and Description |
---|---|
static <M> MessagesProvider<M> |
createMessagesProvider(Class<M> messagesClass)
Returns a
MessagesProvider implementation for the specified class. |
static ResourceBundle |
getResourceBundle(org.osgi.framework.Bundle plugin,
HttpServletRequest request)
Returns the resource bundle containing localized messages for the specified plug-in, depending on the servlet request preferred locale.
|
static ResourceBundle |
getResourceBundle(org.eclipse.core.runtime.IConfigurationElement element,
HttpServletRequest request)
Returns the resource bundle containing localized messages for the plug-in contributing the specified element, depending on the servlet request
preferred locale.
|
public static ResourceBundle getResourceBundle(org.osgi.framework.Bundle plugin, HttpServletRequest request)
The plug-in must have a /OSGI-INF/I10n/
folder containing *.properties
files:
bundle.properties
: contains English messagesbundle_fr.properties
: contains French messagesA class can retrieve a bundle localized message using:
String localizedMessage = ServletNLS.getResourceBundle(bundle, request).getString("my_message_key");
The language to use is determined using the Accept-Language
header provided by the servlet request.
ServletNLS 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 bundle.properties
if there's no match.
plugin
- a plug-in having a /OSGI-INF/I10n/bundle.properties
resource.request
- the servlet request.null
if none.public static ResourceBundle getResourceBundle(org.eclipse.core.runtime.IConfigurationElement element, HttpServletRequest request)
element
- an extension whose contributing plug-in defines translations.request
- the servlet request.null
if none.getResourceBundle(Bundle, HttpServletRequest)
public static <M> MessagesProvider<M> createMessagesProvider(Class<M> messagesClass)
MessagesProvider
implementation for the specified class.M
- a subclass of NLS providing message keys and translations.messagesClass
- a class extending NLS.MessagesProvider
implementation for the specified class.