GWT动态区域设置 [英] GWT Dynamic Locale
问题描述
我想在Spring LocaleContextHolder
的帮助下设置 gwt-locale
以用户选择的语言环境。
public static final String getCurrentLocale(){
return LocaleContextHolder.getLocale()。getLanguage();
}
实际上,Spring MVC中的登录界面和 gwtp
。同样的locale用户在登录之前必须在外部界面中选择
不幸的是,我没有看到任何gwt内置的Locale setter。
$ b
我的 X.gwt.xml
的默认区域设置为 kh
是:
< inherits name =com.google.gwt.uibinder.UiBinder/>
<继承名称=com.google.gwt.inject.Inject/>
<继承名称=com.gwtplatform.mvp.Mvp/>
< inherits name =gwtquery.plugins.droppable.Droppable/>
< source path =client/>
< source path =shared/>
< define-configuration-property name =gin.ginjectoris-multi-valued =false/>
< set-configuration-property name =gin.ginjectorvalue =com.prayagupd.client.mvp.XGInjector/>
< set-configuration-property name =UiBinder.useSafeHtmlTemplatesvalue =true/>
< extend-property name =localevalues =kh/>
< extend-property name =localevalues =en/>
< set-property name =localevalue =kh/>
< set-property-fallback name =localevalue =kh/>
< entry-point class =com.prayagupd.client.XEntryPoint/>
我的 XEntryPoint.java
读作:
public class XEntryPoint implements EntryPoint {
private final IUserServiceAsync rpc = GWT.create(IUserService.class);
@Override
public void onModuleLoad(){
//
rpc.getLocale(new AsyncCallback< String>(){
@Override
public void onSuccess(String locale){
GWT.log(Locale From Spring:+ locale);
GWT.log(Locale From GWT:+ LocaleInfo.getCurrentLocale ().getLocaleName());
//这里我想设置语言环境为gwt
//类似GWTLocale.setLocale(locale);
}
@Override
public void onFailure(Throwable caught){
GWT.log(caught.getMessage());
}
});
DelayedBindRegistry.bind(ginjector);
ginjector.getPlaceManager()。revealCurrentPlace();
home.jsp for gwt-loading
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Strict // ENhttp ://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd>
<%@ tag import =java.util.Calendar%>
<%@ tag body-content =scriptless%>
<%@ taglib prefix =springuri =http://www.springframework.org/tags%>
<%@ taglib prefix =formuri =http://www.springframework.org/tags/form%>
<%@ taglib prefix =curi =http://java.sun.com/jsp/jstl/core%>
<%@ taglib prefix =sprtagdir =/ WEB-INF / tags%>
<%@ attribute name =isgwtrequired =truetype =java.lang.Boolean%>
<! - <!DOCTYPE html> - >
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< link rel =shortcut icontype =image / pnghref =/ images / favicon.png/>
< script type =text / javascriptsrc =js / reload.captcha.js>< / script>
< script type =text / javascriptsrc =js / date.picker.js>< / script>
< link rel =stylesheethref =/ styles / innerstyle.csstype =text / css/>
< c:if test =$ {not isgwt}>
< link rel =stylesheethref =/ styles / mainstyler.csstype =text / css/>
< script type =text / javascriptsrc =js / modernizer.custom.js>< / script>
< script type =text / javascriptsrc =js / jquery.js>< / script>
< script type =text / javascriptsrc =js / jquery-ui-custom.min.js>< / script>
< script src =js / jquery.thumbnailScroller.js>< / script>
< / c:if>
< c:if test =$ {isgwt}>
< meta name =gwt:propertycontent =locale = $ {locale}>
< script type =text / javascriptlanguage =javascriptsrc =upd / upd.nocache.js>< / script>
< / c:if>
< script type =text / javascript>
$(window).load(function(){
$('#slider')。nivoSlider();
});
< / script>
< title>< c:out value =$ {locale}>< / c:out>< spring:message code =page.header/><< ; /标题>
< / head>
< body>
< c:when test =$ {empty username}>
< div class =header_con>
< div class =header_in>
< spr:header />
< spr:login />
< div class =clear>< / div>
< / div>
< div class =main_con>
< jsp:doBody />
< spr:footer />
< / div>
< div class =clear>< / div>
< / div>
< / c:当>
< c:其他>
< div id =mainHolder>
< div id =wrapper>
< spr:headerInner />
style =position:absolute; width:0; height:0; border :0>< / iframe>
< div>
< div id =gwt_holder>
< c:if test =$ {isgwt}>
< div id =loaderclass =loader>
< / div>
< / c:if>
< div id =gwt>< / div>
< / div>
< / div>
< / div>
< / div>
< / c:否则>
< / body>
< / html>
添加?locale = en
或?locale = kh
到gwt url完美地工作,但只想告诉GWT一次,我想以编程方式使用该语言环境,并希望它始终与该语言环境一起工作。
当我查看 *。html
源代码时,我可以看到注入了< meta>从 SpringController
。
传递的标记与正确的 locale
参考资料
GWT动态国际化,Colin Alworth
使用 动态主机页面 注入合适的< meta name =gwt:propertycontent = locale = XXX>
。
请记住GWT bootstrap sequence :一旦你调用了 onModuleLoad
,排列的选择其中包括语言环境)已经完成。你必须改变bootstrap序列,以便为用户选择适当的排列。 ?locale = XXX
会这样做(因为 locale
属性有一个 < property-provider>
,它读取 locale
查询字符串参数,以及上面的< meta>
>。
另请参阅 https ://code.google.com/p/google-web-toolkit-incubator/wiki/ServerSideLocaleSelection 了解一些想法(请注意:已弃用的项目!)
最后,您的 *。gwt.xml
有几个问题,以 kh
开头,不是有效区域。
国际化的工作流程您的应用程序如下所示:
-
列表y我们的地区:
< extend-property name =localevalue =en/>
< extend-property name =localevalue =fr/>
-
删除默认
将 locale
属性设置为支持的语言环境的完整列表:
< ; set-property name =localevalue =en,fr/>
-
设置备用区域设置:
< set-property-fallback name =localevalue =en/>
您也可以选择区域设置使用属性 locale.queryparam
, locale.cookie
, locale.usemeta
, locale.useragent
和 locale.searchorder
(请参阅 I18N。 gwt.xml
为它们的默认值和接受值)。
最后,添加代码以选择语言环境(例如动态< meta>
以上)
I want to set gwt-locale
taking user chosen locale with the help of Spring LocaleContextHolder
.
public static final String getCurrentLocale() {
return LocaleContextHolder.getLocale().getLanguage();
}
I actually have login interface in Spring MVC and inner Dashboard in gwtp
. The same locale user chooses in outer interface before login has to be passed to gwt
as well.
Unfortunately, I don't see any gwt inbuilt Locale setters.
My X.gwt.xml
with default locale as kh
is :
<inherits name="com.google.gwt.uibinder.UiBinder" />
<inherits name="com.google.gwt.inject.Inject" />
<inherits name="com.gwtplatform.mvp.Mvp" />
<inherits name="gwtquery.plugins.droppable.Droppable"/>
<source path="client" />
<source path="shared" />
<define-configuration-property name="gin.ginjector" is-multi-valued="false"/>
<set-configuration-property name="gin.ginjector" value="com.prayagupd.client.mvp.XGInjector"/>
<set-configuration-property name="UiBinder.useSafeHtmlTemplates" value="true" />
<extend-property name="locale" values="kh" />
<extend-property name="locale" values="en" />
<set-property name="locale" value="kh"/>
<set-property-fallback name="locale" value="kh"/>
<entry-point class="com.prayagupd.client.XEntryPoint"/>
My XEntryPoint.java
reads as :
public class XEntryPoint implements EntryPoint {
private final IUserServiceAsync rpc = GWT.create(IUserService.class);
@Override
public void onModuleLoad() {
//
rpc.getLocale(new AsyncCallback<String>() {
@Override
public void onSuccess(String locale) {
GWT.log("Locale From Spring : " + locale);
GWT.log("Locale From GWT : " + LocaleInfo.getCurrentLocale().getLocaleName());
//here i want to set locale to gwt
//something like GWTLocale.setLocale(locale);
}
@Override
public void onFailure(Throwable caught) {
GWT.log(caught.getMessage());
}
});
DelayedBindRegistry.bind(ginjector);
ginjector.getPlaceManager().revealCurrentPlace();
}
}
home.jsp for gwt-loading
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >
<%@tag import="java.util.Calendar"%>
<%@ tag body-content="scriptless"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="spr" tagdir="/WEB-INF/tags"%>
<%@ attribute name="isgwt" required="true" type="java.lang.Boolean"%>
<!-- <!DOCTYPE html> -->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="shortcut icon" type="image/png" href="/images/favicon.png" />
<script type="text/javascript" src="js/reload.captcha.js"></script>
<script type="text/javascript" src="js/date.picker.js"></script>
<link rel="stylesheet" href="/styles/innerstyle.css" type="text/css" />
<c:if test="${not isgwt}">
<link rel="stylesheet" href="/styles/mainstyler.css" type="text/css" />
<script type="text/javascript" src="js/modernizer.custom.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-ui-custom.min.js"></script>
<script src="js/jquery.thumbnailScroller.js"></script>
</c:if>
<c:if test="${isgwt}">
<meta name="gwt:property" content="locale=${locale}">
<script type="text/javascript" language="javascript" src="upd/upd.nocache.js"></script>
</c:if>
<script type="text/javascript">
$(window).load(function() {
$('#slider').nivoSlider();
});
</script>
<title><c:out value="${locale}"></c:out><spring:message code="page.header" /></title>
</head>
<body>
<c:choose>
<c:when test="${empty username}">
<div class="header_con">
<div class="header_in">
<spr:header />
<spr:login />
<div class="clear"></div>
</div>
<div class="main_con">
<jsp:doBody />
<spr:footer />
</div>
<div class="clear"></div>
</div>
</c:when>
<c:otherwise>
<div id="mainHolder">
<div id="wrapper">
<spr:headerInner />
<iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1'
style="position: absolute; width: 0; height: 0; border: 0"></iframe>
<div>
<div id="gwt_holder">
<c:if test="${isgwt}">
<div id="loader" class="loader">
</div>
</c:if>
<div id="gwt"></div>
</div>
</div>
</div>
</div>
</c:otherwise>
</c:choose>
</body>
</html>
Adding ?locale=en
or ?locale=kh
to gwt url works perfectly, but want to tell GWT only once that I want this locale programmatically and want it to work always with that locale onwards.
When I look at the *.html
source code, I can see the injected the <meta>
tag with proper locale
passed from SpringController
.
References
GWT dynamic internationalization, Colin Alworth
How i change the locale language of the application
解决方案 Use a dynamic host page where you inject the proper <meta name="gwt:property" content="locale=XXX">
.
Remember the GWT bootstrap sequence: once your onModuleLoad
has been called, the choice of the permutation (which includes the locale) has already been made. You have to alter the bootstrap sequence so it chooses the proper permutation for the user. ?locale=XXX
does this (because the locale
property has a <property-provider>
that reads the locale
query-string parameter, among other things), as well as the <meta>
above.
See also https://code.google.com/p/google-web-toolkit-incubator/wiki/ServerSideLocaleSelection for some idea (BEWARE: deprecated project!)
Finally, there are a few issues with your *.gwt.xml
, starting with kh
not being a valid locale.
The workflow for internationalizing your app is as follows:
list your locales:
<extend-property name="locale" value="en" />
<extend-property name="locale" value="fr" />
remove the default
locale by setting the locale
property to the full list of supported locales:
<set-property name="locale" value="en,fr" />
set the fallback locale:
<set-property-fallback name="locale" value="en" />
Optionally, you can select how the locale is determined using the properties locale.queryparam
, locale.cookie
, locale.usemeta
, locale.useragent
, and locale.searchorder
(see the I18N.gwt.xml
for their default and accepted values).
And finally, add code to select the locale (e.g. the dynamic <meta>
above)
这篇关于GWT动态区域设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!