How-to translate system message "Session expired"

General questions regarding the development with JavaFX UI for JVx.

How-to translate system message "Session expired"

Postby johnit » Tue Mar 28, 2017 4:22 pm

The application shows a red "Session expired" banner if Session is expired. My application is in German. The default translation via application doesn't work.

I guess the problem is that vaadin sends the messages in bootstrap handler and doesn't update the messages if changed?

I found this docu: https://vaadin.com/forum/#!/thread/2353411/10706163

Is it possible to integrate this in Vaadin UI?
johnit
 
Posts: 21
Joined: Fri Nov 16, 2012 5:58 pm

Re: How-to translate system message "Session expired"

Postby rzenz » Fri Mar 31, 2017 9:30 am

That banner is provided by Vaadin directly, the "normal" JVx Application translation does not work on it because it is "outside" of the JVx controlled stack. Don't worry, there is still the possibility to translate these messages, even though it is not that straightforward compared with the JVx translation system.

First, you should be aware how these messages are sent to the screen. There is the (com.vaadin.server.)SystemMessages which is the main provider for these messages. It contains the (hardcoded) strings for various dialogs and messages in Vaadin. Additionally, Vaadin uses the (com.vaadin.server.)SystemMessagesProvider interface (with the default implementation being (com.vaadin.Server.)DefaultSystemMessagesProvider to actually get these messages.

Now, the process of translating these messages is the following:

  1. Extend (com.vaadin.server.)CustomizedSystemMessages
  2. Implement (com.vaadin.server.)SystemMessagesProvider
  3. Configure the server to use your classes (web.xml)

You have to extend (com.vaadin.server.)CustomizedSystemMessages in order to provide your messages. This can either be done by overriding the getter methods, or setting your custom strings in the constructor (or any other time), like this:

Code: Select all
public class GermanSystemMessages extends CustomizedSystemMessages
{
    public GermanSystemMessages()
    {
        super();
       
        // Now you can replace messages by setting them.
        communicationErrorMessage = "The connection has been lost, must be solar flares!";
    }
   
    @Override
    public String getCommunicationErrorMessage()
    {
        if (communicationErrorNotificationEnabled)
        {
            if (Math.random() > 0.5d)
            {
                return "The connection has been lost, must be an improperly oriented keyboard.";
            }
            else
            {
                return "The connection has been lost, must be dew on the telephone lines.";
            }
        }
       
        return null;
    }
}


Afterwards, implementing (com.vaadin.server.)SystemMessagesProvider is required to return our new and shiny GermanSystemMessages:

Code: Select all
public class GermanSystemMessagesProvider implements SystemMessagesProvider
{
    private static final GermanSystemMessages MESSAGES_INSTANCE = new GermanSystemMessages();
   
    public GermanSystemMessagesProvider()
    {
    }
   
    public SystemMessages getSystemMessages(SystemMessagesInfo systemMessagesInfo)
    {
        return MESSAGES_INSTANCE;
    }
}


Now the only keypart missing is that we must configure the server to actually use our new classes, we can do this from the web.xml:

Code: Select all
<!--
**************************************************************************
WebUI configuration
**************************************************************************
-->

<servlet>
    <servlet-name>VaadinUI</servlet-name>
    <servlet-class>com.sibvisions.rad.ui.vaadin.server.VaadinServlet</servlet-class>
   
    ...
   
    <init-param>
        <param-name>systemmessagesprovider</param-name>
        <param-value>com.your.app.GermanSystemMessagesProvider</param-value>
    </init-param>
   
    ...
   
</servlet>


That's it! This is how to translate these Vaadin system messages in JVx.
User avatar
rzenz
 
Posts: 17
Joined: Mon Dec 12, 2016 1:40 pm
Location: Vienna, Austria


Return to Development