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

原文地址:https://www.cnblogs.com/cklovefan/p/5266721.html