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 ,决定输出内容

  • 相关阅读:
    洛谷 P1233 木棍加工
    洛谷 P3378 【模板】堆(小根堆)
    leetcode难度及面试频率
    设计模式大全
    多线程经典面试题
    查找子字符串----KMP算法深入剖析
    线程与进程的区别
    海量数据面试题----分而治之/hash映射 + hash统计 + 堆/快速/归并排序
    解析STL中典型的内存分配
    C++ 常量类型 const 详解
  • 原文地址:https://www.cnblogs.com/cklovefan/p/5266721.html
Copyright © 2011-2022 走看看