为什么GWT会忽略浏览器区域设置?

时间:2021-06-10 22:59:43

GWT gets locale from either the locale property or the locale query string. If neither is specified, it uses the "default" (ie en_US) locale.

GWT从locale属性或语言环境查询字符串中获取语言环境。如果两者都未指定,则使用“default”(即en_US)语言环境。

Why doesn't it get it from the browser settings?

为什么不从浏览器设置中获取它?

It seems the only solution to this is to replace your static html launch page with something like a JSP that reads the browser locales and sets the locale or redirects using the query string. There has to be a better solution than this or simply hard-coding a locale, surely?

似乎唯一的解决方案是用JSP读取浏览器语言环境并使用查询字符串设置语言环境或重定向,以替换静态html启动页面。必须有一个比这更好的解决方案,或者只是硬编码一个语言环境,当然?

6 个解决方案

#1


6  

You can also put this switch in your *.gwt.xml

您也可以将此开关放在* .gwt.xml中

<set-configuration-property name="locale.useragent" value="Y"/>

this will add language selecting based on language selected in browser. You can also control search order for locale by setting

这将根据浏览器中选择的语言添加语言选择。您还可以通过设置来控制区域设置的搜索顺序

  <set-configuration-property name="locale.searchorder" value="queryparam,cookie,meta,useragent"/>

But beware that in IE this doesn't work - you should develop server-side language pick based on 'Accept-Language' header send by the IE.

但请注意,在IE中这不起作用 - 您应该根据IE发送的“Accept-Language”标头开发服务器端语言选择。

#2


3  

If you put a list of available languages into your *.gwt.xml file it will by default switch to the first language listed.

如果将可用语言列表放入* .gwt.xml文件中,它将默认切换到列出的第一种语言。

<!-- Slovenian in Slovenia -->
<extend-property name="locale" values="sl"/>

<!-- English language, independent of country -->
<extend-property name="locale" values="en"/>

#3


1  

You can use a cookie to save and send this value, but for that you have to add in your *.gwt.xml first

您可以使用cookie来保存并发送此值,但为此您必须首先添加* .gwt.xml

<set-configuration-property name="locale.cookie" value="yourCookieName"/>
<set-configuration-property name="locale.searchorder" value="queryparam,cookie,meta,useragent"/>

Note that "queryparam" has the biggest priority here, that allows to set a new locale using the http query and ignore the value on the cookie.

请注意,“queryparam”在此处具有最大优先级,允许使用http查询设置新的语言环境并忽略cookie上的值。

#4


0  

This worked for me, I hope it also works for you.

这对我有用,我希望它也适合你。

My problem was that I have not declared any locale value in .gwt.xml module descriptor. In that case only the default locale is used. GWT does that way because any different supported locale means a new compilation iteration/permutation. Therefore only declared locales are used.

我的问题是我没有在.gwt.xml模块描述符中声明任何语言环境值。在这种情况下,仅使用默认语言环境。 GWT就是这样做的,因为任何不同的受支持语言环境都意味着新的编译迭代/置换。因此,仅使用声明的语言环境。

Here you are an example:

这是一个例子:

<!-- Locales -->
<extend-property name="locale" values="en_US"/>
<extend-property name="locale" values="es"/>    
<set-property-fallback name="locale" value="en_US"/>
<set-configuration-property name="locale.useragent" value="Y" />
<set-configuration-property name="locale.searchorder" value="queryparam,cookie,meta,useragent" />

The first and second lines set the available/supported locales (English from US and Spanish without specific country in my example). The third line sets the default locale in case no one is detected (this default declaration must be set after the default value is declared in a extend-property line). The fourth line enables the locale detection by means of the HTTP-Headers Accept-Language sent by browser (probably is enabled by default and not needed to set at all). The final line sets the order in which the different detection mechanisms try to detect the locale:

第一行和第二行设置可用/支持的语言环境(英语来自美国和西班牙语,在我的示例中没有特定国家/地区)。如果没有检测到任何一个,第三行设置默认语言环境(必须在extend-property行中声明默认值后设置此默认声明)。第四行通过浏览器发送的HTTP-Headers Accept-Language启用语言环境检测(可能默认启用,根本不需要设置)。最后一行设置了不同检测机制尝试检测语言环境的顺序:

  1. As a parameter in the URL query
  2. 作为URL查询中的参数

  3. From cookies
  4. As a meta value in the HTML page
  5. 作为HTML页面中的元值

  6. From the HTTP header sent by browser
  7. 从浏览器发送的HTTP标头

#5


0  

If your entry page is a JSP you can inspect the request's Accept-Language header to dynamically set the locale.

如果您的输入页面是JSP,则可以检查请求的Accept-Language标头以动态设置区域设置。

#6


0  

add this entry in your *.gwt.xml file to see the effect!

在* .gwt.xml文件中添加此条目以查看效果!

Please check the following line for more information!

请查看以下行以获取更多信息!

<set-configuration-property name="locale.useragent" value="Y"/>

#1


6  

You can also put this switch in your *.gwt.xml

您也可以将此开关放在* .gwt.xml中

<set-configuration-property name="locale.useragent" value="Y"/>

this will add language selecting based on language selected in browser. You can also control search order for locale by setting

这将根据浏览器中选择的语言添加语言选择。您还可以通过设置来控制区域设置的搜索顺序

  <set-configuration-property name="locale.searchorder" value="queryparam,cookie,meta,useragent"/>

But beware that in IE this doesn't work - you should develop server-side language pick based on 'Accept-Language' header send by the IE.

但请注意,在IE中这不起作用 - 您应该根据IE发送的“Accept-Language”标头开发服务器端语言选择。

#2


3  

If you put a list of available languages into your *.gwt.xml file it will by default switch to the first language listed.

如果将可用语言列表放入* .gwt.xml文件中,它将默认切换到列出的第一种语言。

<!-- Slovenian in Slovenia -->
<extend-property name="locale" values="sl"/>

<!-- English language, independent of country -->
<extend-property name="locale" values="en"/>

#3


1  

You can use a cookie to save and send this value, but for that you have to add in your *.gwt.xml first

您可以使用cookie来保存并发送此值,但为此您必须首先添加* .gwt.xml

<set-configuration-property name="locale.cookie" value="yourCookieName"/>
<set-configuration-property name="locale.searchorder" value="queryparam,cookie,meta,useragent"/>

Note that "queryparam" has the biggest priority here, that allows to set a new locale using the http query and ignore the value on the cookie.

请注意,“queryparam”在此处具有最大优先级,允许使用http查询设置新的语言环境并忽略cookie上的值。

#4


0  

This worked for me, I hope it also works for you.

这对我有用,我希望它也适合你。

My problem was that I have not declared any locale value in .gwt.xml module descriptor. In that case only the default locale is used. GWT does that way because any different supported locale means a new compilation iteration/permutation. Therefore only declared locales are used.

我的问题是我没有在.gwt.xml模块描述符中声明任何语言环境值。在这种情况下,仅使用默认语言环境。 GWT就是这样做的,因为任何不同的受支持语言环境都意味着新的编译迭代/置换。因此,仅使用声明的语言环境。

Here you are an example:

这是一个例子:

<!-- Locales -->
<extend-property name="locale" values="en_US"/>
<extend-property name="locale" values="es"/>    
<set-property-fallback name="locale" value="en_US"/>
<set-configuration-property name="locale.useragent" value="Y" />
<set-configuration-property name="locale.searchorder" value="queryparam,cookie,meta,useragent" />

The first and second lines set the available/supported locales (English from US and Spanish without specific country in my example). The third line sets the default locale in case no one is detected (this default declaration must be set after the default value is declared in a extend-property line). The fourth line enables the locale detection by means of the HTTP-Headers Accept-Language sent by browser (probably is enabled by default and not needed to set at all). The final line sets the order in which the different detection mechanisms try to detect the locale:

第一行和第二行设置可用/支持的语言环境(英语来自美国和西班牙语,在我的示例中没有特定国家/地区)。如果没有检测到任何一个,第三行设置默认语言环境(必须在extend-property行中声明默认值后设置此默认声明)。第四行通过浏览器发送的HTTP-Headers Accept-Language启用语言环境检测(可能默认启用,根本不需要设置)。最后一行设置了不同检测机制尝试检测语言环境的顺序:

  1. As a parameter in the URL query
  2. 作为URL查询中的参数

  3. From cookies
  4. As a meta value in the HTML page
  5. 作为HTML页面中的元值

  6. From the HTTP header sent by browser
  7. 从浏览器发送的HTTP标头

#5


0  

If your entry page is a JSP you can inspect the request's Accept-Language header to dynamically set the locale.

如果您的输入页面是JSP,则可以检查请求的Accept-Language标头以动态设置区域设置。

#6


0  

add this entry in your *.gwt.xml file to see the effect!

在* .gwt.xml文件中添加此条目以查看效果!

Please check the following line for more information!

请查看以下行以获取更多信息!

<set-configuration-property name="locale.useragent" value="Y"/>