zoukankan      html  css  js  c++  java
  • java学习笔记 (9) —— Struts2 国际化

    1、Test.java

    package com.i18n;
    
    import java.util.Locale;
    
    public class Test1 {
    
        public static void main(String[] args) 
        {
            Locale[] locales = Locale.getAvailableLocales();
            for(Locale locale: locales){
                System.out.println(locale.getDisplayCountry() + " : " + locale.getCountry());
            }
        }
    }

    2、Test2.java

    package com.i18n;
    
    import java.util.*;
    
    public class Test2 {
    
        public static void main(String[] args) 
        {
            //获取本地locale
            Locale locale = Locale.getDefault();
            ResourceBundle bundle = ResourceBundle.getBundle("hellofile",locale);
            String value = bundle.getString("hello");
            System.out.println(value);
        }
    }

     3、Test3.java

    package com.i18n;
    
    import java.text.MessageFormat;
    import java.util.Locale;
    import java.util.ResourceBundle;
    
    
    public class Test3 {
        
        public static void main(String[] args) {
            Locale locale = Locale.getDefault();
            ResourceBundle bundle = ResourceBundle.getBundle("hellofile",locale);
            String value = bundle.getString("hello");
            String result = MessageFormat.format(value,new Object[]{"北京"});
            System.out.println(result);
        }
    }

    4、定义properties文件

    hellofile_en_US.properties

      hello = hello:{0}

    hellofile_zh_CN.properties

      hello = u4f60u597d : {0}

    Struts2 实现国际化

    1、配置struts.xml文件

            <!-- 找配置文件 以  message 开头的信息 -->
            <constant name="struts.custom.i18n.resources" value="message"></constant>

    2、建立properties 文件名为 message

      message_en_US.properties —— addUser = Add User Information

      message_zh_CN.properties —— addUser = u6dfbu52a0u7528u6237

    3、修改register.jsp

          <center>
              <s:text name="addUser"></s:text>
          </center>

    4、根据浏览器的httpheader ,决定输出内容

  • 相关阅读:
    BeautifulSoup的基本用法
    打印实例
    webservice和一般处理程序
    C# 后台调用存储过程
    表格增加删除
    asp.net C# 获得配置文件AppSettings 的值
    深入浅出zookeeper(一)
    resource下的excel文件下载被损害
    csdn添加目录
    spring面试题,求求你别问我spring了!
  • 原文地址:https://www.cnblogs.com/cklovefan/p/5266721.html
Copyright © 2011-2022 走看看