struts-i18n国际化实现

1、i18n是什么意思?

其来源是英文单词 internationalization的首末字符i和n,18为中间的字符数,是“国际化”的简称。对程序来说,在不修改内部代码的情况下,能根据不同语言及地区显示相应的界面。

2、i18n实现的原理如下(主要是利用ResourceBundle和Locale类实现):

1、新建一个java工程

2、在src下新建一个包com.etc.test,在包下新建Main.java类

  package com.etc.test;
     import java.util.Locale;
     import java.util.ResourceBundle;

     public class Main {
	public static void main(String[] args) {
		//ResourceBundle bundle = ResourceBundle.getBundle("message");//根据myeclipse的编码(默认输出简体中文)
		ResourceBundle bundle = ResourceBundle.getBundle("message",Locale.ENGLISH);//指定输出相应的英文
		String h = bundle.getString("hello");
		System.out.println(h);
	}
     }	
        3、新建message_zh_CN.properties文件(因为在Main.java中指定了是message,所以新建的文件都是message_国家编码(Locale类中可以找到相应国家的编码))


        在properties界面添加相应的键值对

        

  4、新建message_en.properties文件,在properties界面添加相应的键值对


5、执行Main.java类:

默认输出对应的简体中文


配置参数,输出相应国家的语言


struts2中的i18n国际化是如何实现的呢?

1、新建web工程struts-i18n,添加struts2支持

2、配置struts.xml文件

<constant name="struts.custom.i18n.resources" value="message"></constant>

3、新建message_zh_CN.properties文件(struts.xml中指定了message)


4、在src下新建com.etc.action包,在包下新建HelloAction.java

package com.etc.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport{

	private static final long serialVersionUID = -5708491200911094607L;
	public String hello(){
		System.out.println(getText("hello"));
		return Action.SUCCESS;
	}
}
5、配置struts.xml文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.custom.i18n.resources" value="message"></constant>
<package name="hello" extends="struts-default" namespace="/">
	<action name="hello" class="com.etc.action.HelloAction" method="hello">
		<result>/hello.jsp</result>
	</action>
</package>
</struts>   
6、在WebRoot下新建hello.jsp页面

  <%@ taglib prefix="s" uri="/struts-tags" %>
  <body>
  <s:text name="success"></s:text>
  </body>
7、页面运行:http://localhost:8080/struts-i18n/hello.action,页面显示:成功(success对应的简体中文)


控制台输出:你好(hello对应的简体中文)



总结:

struts2 i18n国际化开发步骤:

1、struts.xml中配置

<constant name="struts.custom.i18n.resources" value="message">
     其中struts.custom.i18n.resources可以在default.properties文件中找到



2、新建message_xx.properties文件,配置相应的键值对

3、在Action类中使用getText("xx");访问properties中的值

System.out.println(getText("hello"));

4、在Jsp页面中使用<s:text>标签访问properties中的值

<s:text name="success"></s:text>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值