• 实现资源国际化


    实现网站资源国际化,中英双语切换。

    采用<fmt>标签,js页面通过ajax异步传递到后台处理,实现中英文切换。

    1,action

    package com.language.action;

    import org.apache.struts2.ServletActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    /**
    * 语言切换
    * @author Junwei.Zeng
    * @date 2016-8-25
    */
    public class LanguageModAction extends ActionSupport{

    private static final long serialVersionUID = 1L;
    private String language;
    public void languageMod(){
    System.out.println("当前语言:"+language);

    ServletActionContext.getRequest().getSession().setAttribute("language", language);
    }
    //set && get
    public String getLanguage() {
    return language;
    }
    public void setLanguage(String language) {
    this.language = language;
    }
    }

    2,js页面

    <span id="Chinese">中文</span>
    <span id="English">English</span><br>

    <script type="text/javascript">
    $("#Chinese").click(function(){
    var url = window.location.href;
    var p = "zh_CN";
    $.ajax({

    url : "languageModAction",
    type : "GET",
    data : {
    "language" : p,
    },
    dataType : "text",
    success : function(data) {
    window.location=url;
    }
    });
    });


    $("#English").click(function(){
    var url = window.location.href;
    var p = "en_US";
    $.ajax({
    url : "languageModAction",
    type : "GET",
    data : {
    "language" : p,
    },
    dataType : "text",
    success : function(data) {
    window.location=url;
    }
    });
    });
    </script>

    <fmt:setLocale value="${language}" />
    <fmt:bundle basename="globalMessages">
    <fmt:message key="test" /><br>
    <fmt:message key="username" /><br>
    <fmt:message key="password" /><br>
    <fmt:message key="cityLine" /><br>
    <fmt:message key="WeChat" /><br>
    <fmt:message key="FashionStreet" /><br>
    </fmt:bundle>

  • 相关阅读:
    根据当前日期转目的国地区时间戳
    时间戳转换作用域问题
    字符串拼接问题
    input全选和取消全选
    循环遍历渲染模块
    jQuery实现获取选中复选框的值
    React组件
    underscore.js依赖库函数分析二(查找)
    underscore.js依赖库函数分析一(遍历)
    React入门
  • 原文地址:https://www.cnblogs.com/zjw199306/p/5805630.html
Copyright © 2020-2023  润新知