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

  • 相关阅读:
    有用的java工具
    AOP在大规模软件开发项目中的应用(图)
    java并发编程实践笔记
    深入JVM系列(三)之类加载、类加载器、双亲委派机制与常见问题
    JVM相关问答
    软件架构设计的六大原则
    JVM 运行时数据区域
    JVM 类加载过程
    JVM 垃圾回收算法
    Memcached内存分配优化及使用问题
  • 原文地址:https://www.cnblogs.com/cklovefan/p/5266721.html
Copyright © 2011-2022 走看看