GWT i18n, change metaTag and reload application

99封情书 提交于 2019-12-11 06:32:20

问题


I'm trying to internationalize my GWT application.

I read the tutorials and see some examples.

Is there a way to do the language change without adding the url the tag "?locale=de"

My objective is at the menu login screen, the user selects the languague, and then it reloads. I managed to do that with the adding of the locale to the url.[Ugly way to me]

I was reading that there is possible with meta tags in Html file. So i tryed to put that in the html file

It loads in that language, but i can't change no more the language.

Is possible to chage the language only using meta tags?

In code, i can read and chage the meta tag value but when i refresh the change i made is lost

    NodeList<Element> tags = Document.get().getElementsByTagName("meta");
    for (int i = 0; i < tags.getLength(); i++) {
        MetaElement metaTag = ((MetaElement) tags.getItem(i));
        System.out.println("metaTag.getName() = " + metaTag.getName());
        System.out.println("metaTag.getContent = " + metaTag.getContent());
        if (metaTag.getName().equals("gwt:property")) {
            metaTag.setContent("locale=de");
        }
    }
    Window.Location.reload();

回答1:


You can use cookies to read/write information about your locale instead of meta tag or url.

In your .gwt.xml file

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

In you java code to read locale info

final String cookieName = LocaleInfo.getLocaleCookieName();
String cookie = Cookies.getCookie( cookieName );

In you java code to write locale info

private void setLocaleCookie( String locale )
{
    final String cookieName = LocaleInfo.getLocaleCookieName();
    if ( cookieName != null )
    {
        Date expires = new Date();
        expires.setYear( expires.getYear() + 1 );
        Cookies.setCookie( cookieName, locale, expires );
    }
    if ( !control )
    {
        com.google.gwt.user.client.Window.Location.reload();
    }
}

Also Reference

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

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



来源:https://stackoverflow.com/questions/14363596/gwt-i18n-change-metatag-and-reload-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!