编程国际化

1、什么是国际化


  国际化就是可以把页面中的内容可变成中文可变成英文。例如在页面中的登录表单:



  要页面中的文字切换,就不能使用硬编码,例如下面的页面中都是硬编码:


  这样的编码是不能实现语言之间的切换的。上图中的中文想转换成英文,就需要把它们都变成活编码:

  只有这样能够实现灵活的获取才能实现切换。


2、Locale类


  为考虑用户的语言和地理区域,在Java平台上通过java.util.Locale对象来描述本地化的配置和上下文。

  创建Locale类对象:

<span style="font-size:18px;">new Locale(“zh”, “CN”);
new Locale(“en”, “US”);</span>

  zh、en表示语言,而CN、US表示国家。一个Locale对象表示的就是语言和国家。


3、ResourceBundle类


  ReourceBundle类用来获取配置文件中的内容。

  下面是两个配置文件内容:

     res_zh_CN.properties:



     res_en_US.properties:

 

  实例:

<span style="font-size:18px;">public class Demo1 {
	@Test
	public void fun1() {
		ResourceBundle rb = ResourceBundle.getBundle("res", new Locale("zh", "CN" ));
		String username = rb.getString("msg.username");
		String password = rb.getString("msg.password");
		System.out.println(username);
		System.out.println(password);		
	}
	
	@Test
	public void fun2() {
		ResourceBundle rb = ResourceBundle.getBundle("res", new Locale("en", "US" ));
		String username = rb.getString("msg.username");
		String password = rb.getString("msg.password");
		System.out.println(username);
		System.out.println(password);		
	}
}</span>

  ResourceBundle的getBundle()方法需要两个参数:

    第一个参数:配置文件的基本名称;

    第二个参数:Locale。

  getBundle()方法会通过两个参数来锁定配置文件。


4、页面国际化


    


  其实页面国际化也是同一道理,只需要通过切换Locale来切换配置文件。我们写一个MessageUtils类,内部需要ResourceBundle的实例。再写一个过滤器MessageFilter,它会通过参数创建Locale对象,传递给MessageUtils,然后在页面中使用MessageUtils来获取文本信息。


<span style="font-size:18px;">MessageUtils.java
public class MessageUtils {
	private static String baseName = "res";
	private static Locale locale;
	public static String getText(String key) {
		return ResourceBundle.getBundle(baseName, locale).getString(key);
	}
	public static Locale getLocale() {
		return locale;
	}
	public static void setLocale(Locale locale) {
		MessageUtils.locale = locale;
	}
}</span>


<span style="font-size:18px;">MessageFilter.java
public class MessageFilter implements Filter {
	public void destroy() {
	}

	public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws Exception {
		HttpServletRequest req = (HttpServletRequest) request;
		String l = req.getParameter("request_locale");
		Locale locale = null;
		if(l != null && !l.isEmpty()) {
			String[] strs = l.split("_");
			locale = new Locale(strs[0], strs[1]);
			req.getSession().setAttribute("WW_TRANS_I18N_LOCALE", locale);
		} else {
			locale = (Locale)req.getSession().getAttribute("WW_TRANS_I18N_LOCALE");
		}
		if(locale == null) {
			locale = req.getLocale();
		}
		MessageUtils.setLocale(locale);
		chain.doFilter(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {
	}
}</span>

<span style="font-size:18px;">login.jsp
  <body>
    <h1><%=MessageUtils.getText("msg.login") %></h1>
    <form action="<c:url value='/index.jsp'/>" method="post">
    	<%=MessageUtils.getText("msg.username")%>:<input type="text" name="username"/><br/>
    	<%=MessageUtils.getText("msg.password")%>:<input type="password" name="password"/><br/>
    	<input type="submit" value='<%=MessageUtils.getText("msg.login")%>'/>
    </form>
  </body></span>


<span style="font-size:18px;">index.jsp
  <body>
    <p><%=MessageUtils.getText("hello") + ":" +request.getParameter("username") %>:</p>
  </body></span>


5、NumberFormat


  NumberFormat类用来对数字进行格式化,我们只需要使用String format(double)一个方法即可。下面是获取NumberFormat实例的方法:


    ● NumberFormat format = NumberFormat.getNumberFormat();

    ● NumberFormat format = NumberFormat.getNumberFormat(Locale);

    ● NumberFormat format = NumberFormat.getCurrentcyFormat();

    ● NumberFormat format = NumberFormat.getCurrentcyFormat(Locale);

    ● NumberFormat format = NumberFormat.getPercentFormat();

    ● NumberFormat format = NumberFormat.getPercentFormat(Locale)。


6、DateFormat


  DateFormat类用来对日期进行格式化,我们只需要使用String format(Date)一个方法即可。下面是获取DateFormat实例的方法:


    ● DateFormat format = DateFormat.getDateFormat();

    ● DateFormat format = DateFormat.getTimeFormat();

    ● DateFormat format = DateFormat.getDateTimeFormat();

    ● DateFormat format = DateFormat.getDateFormat(int style, Locale locale);

    ● DateFormat format = DateFormat.getTimeFormat(int style, Locale locale);

    ● DateFormat format = DateFormat.getDateTimeFormat(int style, Locale locale)。


  其中style是对日期的长、中、短,以及完整样式:SHORT、MEDIUM、LONG、FULL。


7、MessageFormat


  MessageFormat可以把模式中的{N}使用参数来替换。我们把{N}称之为点位符。其中点位符中的N是从0开始的整数。

  MessageFormat.format(String pattern, Object… params),其中pattern中可以包含0~n个点位符,而params表示对点位符的替换文本。注意,点位符需要从0开始。

<span style="font-size:18px;">String p = “{0}或{1}错误”;
String text = MessageFormat.format(p, “用户名”, “密码”);
System.out.println(text);//用户名或密码错误</span>


  小结:国际化的关键就是防止硬编码,运用反射等技术实现编码灵活性。



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值