System messages

Hello!

In the servletInitialized method you can customize (translate to german for example) system messages:

final com.vaadin.flow.server.CustomizedSystemMessages messages = new com.vaadin.flow.server.CustomizedSystemMessages();
messages.setCookiesDisabledCaption(“Cookies ausgeschaltet”);
messages.setCookiesDisabledMessage(
“Für diese Anwendung sind Cookies erforderlich.
Bitte aktivieren Sie Cookies in Ihrem Browser und klicken Sie hier oder drücken Sie ESC, um es erneut zu versuchen.”);

    messages.setInternalErrorCaption("<b>Interner Fehler</b>");
    messages.setInternalErrorMessage(
            "Bitte benachrichtigen Sie den Administrator.<br>Notieren Sie sich nicht gespeicherte Daten und <u>klicken Sie hier</u> oder drücken Sie ESC, um fortzufahren.");

    messages.setSessionExpiredCaption("<b>Sitzung abgelaufen</b>");
    messages.setSessionExpiredMessage(
            "Ihre Sitzung ist wegen zu langer Inaktivität leider abgelaufen.<br>Notieren Sie sich nicht gespeicherte Daten und <u>klicken Sie hier</u> oder drücken Sie ESC, um fortzufahren.");

    this.getService().setSystemMessagesProvider(e -> messages);

But these changes are static and every user will see these translated messages.

How can I do this for every user language?

There is a second message: “Server connection lost, trying to reconnect.
And a third one: “You are offline”.

How can I customize this one?

Is there any i18n support for system messages?

Thank you very much
Thomas

Hi Thomas. Unfortunately there is no up to date documentation for localizing system messages in Vaadin 10+. The principles are though the same as before in Vaadin 8.

You’ll need to set a custom SystemMessagesProvider to the VaadinService when it is initialized, using e.g. [a VaadinServiceInitListener]
(https://vaadin.com/docs/v13/flow/advanced/tutorial-service-init-listener.html), and there in your custom implementation you can check the current user’s Locale from SystemMessagesInfo when you provide the system messages to use. You can check the Vaadin 8 documentation for [Customizing System Messages]
(https://vaadin.com/docs/v8/framework/application/application-errors.html).

I hope this helps,
Pekka

Also, for the reconnect dialog (“Server connection lost, trying to reconnect…” & “Server connection lost.”) you’ll have to setup the texts for a UI using getUI().getReconnectDialogConfiguration() and setting up the texts there.

For the PWA offline page, you’ll have to [create your own custom offline page]
(https://vaadin.com/docs/v13/flow/pwa/tutorial-pwa-offline.html) to customize the text there. There is really no way of localizing that properly, since the user locale might in theory change at any point, and the offline page needs to be generated/served based on the locale parsed from the initial request.

I’ve created an issue for improving the documentation we have on localization for system messages https://github.com/vaadin/flow/issues/6183. Also, even the basic documentation for system errors is missing, like described in this issue https://github.com/vaadin/flow-and-components-documentation/issues/633

Hello Pekka!

If I changed the system messages for the first user to german all messages are shown in german. From now on english speaking users will see german messages. Of course I can switch to english whe an english speaking user enters the system. But then all german users will see the english messages.

Is this correct?

My code is:

public class ApplicationServiceInitListener
        implements com.vaadin.flow.server.VaadinServiceInitListener, org.rapidpm.dependencies.core.logger.HasLogger {
    /**
     * serviceInit
     */
    @Override
    public void serviceInit(com.vaadin.flow.server.ServiceInitEvent newEvent) {
        newEvent.getSource().addUIInitListener(initEvent -> {
            // Zum Beispiel: de, en
            final java.lang.String lang = com.vaadin.flow.server.VaadinService.getCurrentRequest().getLocale()
                    .getLanguage();

            this.logger().info("A new UI has been initialized: "
                    + com.vaadin.flow.server.VaadinService.getCurrentRequest().getLocale().getDisplayCountry() + " ("
                    + com.vaadin.flow.server.VaadinService.getCurrentRequest().getLocale().getDisplayLanguage() + ")");

            switch (lang) {
                case "de":
                    // https://vaadin.com/forum/thread/17782105/system-messages
                    final com.vaadin.flow.server.CustomizedSystemMessages messages = new com.vaadin.flow.server.CustomizedSystemMessages();
                    messages.setCookiesDisabledCaption("<b>Cookies ausgeschaltet</b>");
                    messages.setCookiesDisabledMessage(
                            "Für diese Anwendung sind Cookies erforderlich.<br>Bitte aktivieren Sie Cookies in Ihrem Browser und <u>klicken Sie hier</u> oder drücken Sie ESC, um es erneut zu versuchen.");

                    messages.setInternalErrorCaption("<b>Interner Fehler</b>");
                    messages.setInternalErrorMessage(
                            "Bitte benachrichtigen Sie den Administrator.<br>Notieren Sie sich nicht gespeicherte Daten und <u>klicken Sie hier</u> oder drücken Sie ESC, um fortzufahren.");

                    messages.setSessionExpiredCaption("<b>Sitzung abgelaufen</b>");
                    messages.setSessionExpiredMessage(
                            "Ihre Sitzung ist wegen zu langer Inaktivität leider abgelaufen.<br>Notieren Sie sich nicht gespeicherte Daten und <u>klicken Sie hier</u> oder drücken Sie ESC, um fortzufahren.");

                    newEvent.getSource().setSystemMessagesProvider(e -> messages);

                    break;

                default:
                    // Do nothing
            } //switch 
        });
    }
}

Thank you four your help.

Thomas

Right, my bad, I should have been more specific.

I think you should create a SystemMessageProvider, set it to the VaadinService and there you will be able to get the current request and locale of the user (per UI/request), and then be able to deliver the right error messages for each locale.

So in the ServiceInitListener you would call newEvent.getSource().setSystemMessagesProvider(new MyCustomLozalizedSystemMessageProvider()); and then you separately implement class MyCustomLocalizedSystemMessageProvider implements SystemMessageErrorProvider { ...} which will return a customized SystemMessages for a specific locale.

Hello Pekka!

No, no. It was my mistake. I thought too complicated.

It is very easy and now my messages are working in the right language.

Thanke you very much for your help,
Thomas

Hello Pekka!

It is me again.

With your description I switched all messages to german.

In my servlet I am using @com.vaadin.flow.router.PreserveOnRefresh.

Now I am pressing refresh in my browser. After that all system messages switched to english.

My solution is switching all messages to german again in “public void beforeEnter(com.vaadin.flow.router.BeforeEnterEvent newEvent)”.

Is this the right solution or is it still a refresh problem in the framework.

Thank you very much
Thomas

I think you’ve run into https://github.com/vaadin/flow/issues/6357, at least the description matches that pretty much 1-1 …

Hi!

OK. That’s the problem. And the question: Bug or no bug.

Thomas