【问题标题】:GWT Dynamic LocaleGWT 动态语言环境
【发布时间】:2013-04-30 06:42:57
【问题描述】:

我想设置 gwt-locale 在 Spring LocaleContextHolder 的帮助下采用用户选择的语言环境。

public static final String getCurrentLocale() {
    return LocaleContextHolder.getLocale().getLanguage();
}

我实际上在 Spring MVC 中有登录界面,在 gwtp 中有内部 Dashboard。登录前在外部界面中选择的相同语言环境用户也必须传递给gwt

不幸的是,我没有看到任何 gwt 内置的语言环境设置器。

我的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"/>

我的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();
    }
}

用于 gwt 加载的 home.jsp

<!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>

?locale=en?locale=kh 添加到 gwt url 效果很好,但只想告诉 GWT 我希望以编程方式使用此语言环境并希望它始终与该语言环境一起工作。

当我查看*.html 源代码时,我可以看到注入的&lt;meta&gt; 标记具有从SpringController 传递的正确locale

参考文献

GWT dynamic internationalization,科林·阿尔沃斯

How i change the locale language of the application

【问题讨论】:

    标签: gwt localization internationalization locale


    【解决方案1】:

    使用dynamic host page 注入正确的&lt;meta name="gwt:property" content="locale=XXX"&gt;

    记住 GWT bootstrap sequence:一旦你的 onModuleLoad 被调用,就已经选择了排列(包括语言环境)。您必须更改引导顺序,以便它为用户选择 proper 排列。 ?locale=XXX 这样做(因为locale 属性有一个&lt;property-provider&gt; 可以读取locale 查询字符串参数等),以及上面的&lt;meta&gt;

    另请参阅https://code.google.com/p/google-web-toolkit-incubator/wiki/ServerSideLocaleSelection 了解一些想法(注意:已弃用的项目!)

    最后,您的*.gwt.xml 存在一些问题,首先是kh 不是valid locale

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

    1. 列出您的地区:

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

      <set-property name="locale" value="en,fr" />
      
    3. 设置后备语言环境:

      <set-property-fallback name="locale" value="en" />
      

    您可以选择使用属性locale.queryparamlocale.cookielocale.usemetalocale.useragentlocale.searchorder 确定区域设置的方式(请参阅I18N.gwt.xml 了解它们的默认值和可接受的值)。

    最后,添加代码以选择语言环境(例如上面的动态&lt;meta&gt;

    【讨论】:

    • 我也添加了&lt;c:if test="${isgwt}"&gt; &lt;meta name="gwt:property" value="locale=${locale}"&gt; &lt;/c:if&gt;home.jsp。(更新home.jsp 以上)我从SpringController 传递“语言环境”参数。但是LocaleInfo.getCurrentLocale().getLocaleName() 总是返回kh,即使我从SpringController 传递locale=en有什么遗漏或放错地方了吗?
    • 你应该有&lt;set-property name="locale" value="kh"/&gt;,要么删除该行,要么将value设置为kh,en;但是?locale=en 不应该工作。你确定 Spring 的 ${locale} 是正确的吗?
    • 2 件事:添加 &lt;set-property-fallback name="locale" value="kh"/&gt; 以声明 default 语言环境; &lt;meta&gt; 的属性名称应该是 content,而不是 value(我的错,我最初在答案中输入错误)
    • 用更多细节更新了答案,取自上面的 cmets。
    • 它对我有一点改变:(注意“values”属性)
    【解决方案2】:

    Thomas Broyer启发的solution

    X.gwt.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.3.0//EN" "http://google-web-toolkit.googlecode.com/svn/tags/2.3.0/distro-source/core/src/gwt-module.dtd">
    <module rename-to="x">
        <inherits name="com.google.gwt.user.User"/>
        <inherits name="com.google.gwt.i18n.I18N" />
        <inherits name="com.google.gwt.http.HTTP" />
        <inherits name="com.google.gwt.json.JSON"/>
    
        <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-fallback name="locale" value="kh"/>
    
        <entry-point class="com.prayagupd.client.XEntryPoint"/>
    
    </module>
    

    还有,home.jsp

    <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>
    

    从 Spring Controller 传递的语言环境

    {
        //...
        modelMap.put("locale", locale);
        return "home";
    }
    

    【讨论】:

      【解决方案3】:

      Thomas Broyer 的回答是正确的,除了一件事。不需要强制使用“动态主机页面”,meta标签可以在客户端动态定义:

      <script type="text/javascript">
          $.ajax("rest/service/default-locale").done(function(data) {
              if (data) {           
                  var metaLocale = $("<meta name='gwt:property' content='locale=" + data + "'>");
                  $("head").append(metaLocale);
              }
              var jsLink = $("<script src='myapp.nocache.js'>");
              $("head").append(jsLink);
          });
      </script>
      

      这样任何额外的修改都可以在 GWT 应用启动之前在客户端完成。

      【讨论】:

        猜你喜欢
        • 2015-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多