How to get the Current Locale in GWT using LocaleInfo

后端 未结 3 776
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 15:14

I have tried to get the current locale on the client using the LocaleInfo class and the method getCurrentLocale(). However it always returns the value default locale no matt

相关标签:
3条回答
  • 2020-12-19 15:30

    Locales for GWT are not automatically obtained from the system/browser settings. As per the GET locale documentation the locale to use at run time can be set in one of two ways:

    1. By specifying a locale request parameter in your application's URL. I.e. http://www.myapplication.com/?locale=es
    2. By including a gwt:property meta tag in the host page:

      <meta name="gwt:property" content="locale=es" />

      This is the preferred method for production applications. Your host page can determine the user's locale in a number of ways including, but not limited to, allowing the user to configure a local or detecting their preferred locale based on the Accept-Language request header.

    0 讨论(0)
  • 2020-12-19 15:34

    It's almost impossible to accurately get the locale of the user in JavaScript so GWT doesn't even try [1].
    LocaleInfo returns information about the locale selected among the ones you compiled in your app; that is, one of the locales you added using <extend-property name="locale" ... /> in your *.gwt.xml.

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

    [1] well, in recent versions it does try (using the browser's compile-time locale), but only to select one of the compiled-in locales, so that doesn't change anything to the behavior of LocaleInfo.

    0 讨论(0)
  • 2020-12-19 15:35

    To make your GWT app auto-detect your locale, just rename your base index.html file (the file that includes the "nocache.js" script) to index.jsp and add the following line to the section:

    <meta name="gwt:property" content="locale=<%=request.getLocale()%>">
    
    0 讨论(0)
提交回复
热议问题