Search code examples
javagwtlocale

How do I set GWT locale onLoad without using the queryParam?


My application consists of several parts. I want to preserve user from changing locale by changing URL in all my application except for one small GWT place. I need to provide locale to that place in URL to be sure that place is loaded in correct language.

What can I do?

I see the following options:

1) Create separate module from that place and allow using queryparam as a source of locale in that module's xml settings file. As far as I understand I need to put down something like <set-configuration-property name="locale.searchorder" value="queryparam,cookie"/> Probably it would works, but its a little bit hard for such a small task.

2) Another option is to implement the desired functionality manually. I have written the following code:

String languageCode = Window.Location.getParameter("lang");
Cookies.setCookie(COOKIE_NAME, languageCode, new Date(System.currentTimeMillis() * 1000 * 3600 * 24 * 365 * 100));
Scheduler.get().scheduleDeferred(new ScheduledCommand()
{
    @Override
    public void execute()
    {
        Window.Location.reload();
    }
});

It works, but the problem is with deferred invocation: if I use it, the page loads, then reload signal comes and reloading starts after page has been displayed. User observes the strange blinking. If I do not use the deferred invocation the cookies are not set, I dont know why (can you explain this, please?).

So how would you solve this task?


Solution

  • We avoid setting the cookie in the GWT Code. We instead set up the cookie in the landing page based on user action or settings and then redirect to the GWT application.

    Step 1 - Login - A JSP page sets up the cookie and does authentication.

    Step 2 - On success directs the URL to html file hosting the GWT application.

    Step 3 - GWT needs to just the read the cookie information via the module xml file.

    In your .gwt.xml file provide the cookie which the Login jsp is setting up.

    <set-configuration-property name="locale.cookie" value="GWT_LOCALE" />
    

    Also Reference

    1) GWT i18n, change metaTag and reload application

    2) https://developers.google.com/web-toolkit/doc/latest/DevGuideI18nLocale

    3) http://learninggwt.blogspot.in/2011/07/gwt-internationalization-and-cookies.html