Class ServletNLS


  • public class ServletNLS
    extends Object
    Provides facilities to handle internationalization for servlets.

    This class requires OSGi to be running.

    • Method Detail

      • getResourceBundle

        public 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.

        The plug-in must have a /OSGI-INF/I10n/ folder containing *.properties files:

        • bundle.properties: contains English messages
        • bundle_fr.properties: contains French messages

        A 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.

        Parameters:
        plugin - a plug-in having a /OSGI-INF/I10n/bundle.properties resource.
        request - the servlet request.
        Returns:
        the resource bundle declared in the specified plug-in, null if none.
      • getResourceBundle

        public static ResourceBundle getResourceBundle​(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.
        Parameters:
        element - an extension whose contributing plug-in defines translations.
        request - the servlet request.
        Returns:
        the resource bundle declared in the specified plug-in, null if none.
        See Also:
        getResourceBundle(Bundle, HttpServletRequest)
      • createMessagesProvider

        public static <M> MessagesProvider<M> createMessagesProvider​(Class<M> messagesClass)
        Returns a MessagesProvider implementation for the specified class.
        Type Parameters:
        M - a subclass of NLS providing message keys and translations.
        Parameters:
        messagesClass - a class extending NLS.
        Returns:
        a MessagesProvider implementation for the specified class.