GWT动态区域设置

pra*_*upd 2 gwt locale localization internationalization

我想在gwt-localeSpring的帮助下设置用户选择的语言环境LocaleContextHolder.

public static final String getCurrentLocale() {
    return LocaleContextHolder.getLocale().getLanguage();
}
Run Code Online (Sandbox Code Playgroud)

我实际上在Spring MVC和内部Dashboard中有登录界面gwtp.在登录之前必须在外部接口中选择相同的区域设置用户gwt.

不幸的是,我没有看到任何gwt内置的Locale setter.

X.gwt.xml的默认语言环境kh是:

<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"/>
Run Code Online (Sandbox Code Playgroud)

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());
                            //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();
    }
}
Run Code Online (Sandbox Code Playgroud)

home.jsp用于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>
Run Code Online (Sandbox Code Playgroud)

添加?locale=en?locale=kh到gwt url工作得很好,但是只想告诉GWT我想要以编程方式使用这个语言环境并希望它始终使用该语言环境.

当我查看*.html源代码时,我可以看到注入了<meta>正确locale传递的标记SpringController.

在此输入图像描述

参考

GWT动态国际化,Colin Alworth

我如何更改应用程序的语言环境语言

Tho*_*yer 6

使用动态主机页面注入正确的主页<meta name="gwt:property" content="locale=XXX">.

记住GWT 引导序列:一旦你onModuleLoad被调用,就已经选择了排列(包括语言环境).您必须更改引导序列,以便为用户选择正确的排列.?locale=XXX这样做(因为该locale属性具有<property-provider>读取locale查询字符串参数,以及其他内容),以及<meta>上述内容.

有关一些想法,请参阅https://code.google.com/p/google-web-toolkit-incubator/wiki/ServerSideLocaleSelection(BEWARE:已弃用的项目!)

最后,您有一些问题*.gwt.xml,从kh不是有效的区域设置开始.

国际化您的应用程序的工作流程如下:

  1. 列出您的语言环境:

    <extend-property name="locale" value="en" />
    <extend-property name="locale" value="fr" />
    
    Run Code Online (Sandbox Code Playgroud)
  2. default通过将locale属性设置为支持的语言环境的完整列表来删除语言环境:

    <set-property name="locale" value="en,fr" />
    
    Run Code Online (Sandbox Code Playgroud)
  3. 设置后备区域设置:

    <set-property-fallback name="locale" value="en" />
    
    Run Code Online (Sandbox Code Playgroud)

或者,您可以选择的语言环境如何使用性质来确定locale.queryparam,locale.cookie,locale.usemeta,locale.useragent,和locale.searchorder(见I18N.gwt.xml他们的默认和接受的价值).

最后,添加代码以选择区域设置(例如<meta>上面的动态)

  • 更新了答案,更多细节,取自上面的评论. (2认同)