Interface MessagesProvider<M>

  • Type Parameters:
    M - a class providing message keys and translations.

    public interface MessagesProvider<M>
    Provides server side messages whose language depends on the servlet request preferred locale.

    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 messages
    • messages_fr.properties: contains French messages

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

    See Also:
    ServletNLS.createMessagesProvider(Class)
    • Method Detail

      • getInstance

        M getInstance​(HttpServletRequest request)
        Provides translations depending on the preferred locale configured on the servlet request.
        Parameters:
        request - the servlet request.
        Returns:
        the translations to use.