国际化
什么是国际化
国际化就是可以把页面中的中文变成英文。例如在页面中的登录表单:
Locale类
创建Locale类对象:
l new Locale(“zh”, “CN”);
l new Locale(“en”, “US”);
zh、en表示语言,而CN、US表示国家。一个Locale对象表示的就是语言和国家。
ResourceBundle类
ReourceBundle类用来获取配置文件中的内容。
下面是两个配置文件内容:
res_zh_CN.properties
res_en_US.properties
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); } } |
ResourceBundle的getBundle()方法需要两个参数:
l 第一个参数:配置文件的基本名称
l 第二个参数:Locale
getBundle()方法会通过两个参数来锁定配置文件!
页面国际化
其实页面国际化也是同一道理,只需要通过切换Locale来切换配置文件。我们写一个MessageUtils类,内部需要ResourceBundle的实例。
写一个过滤器MessageFilter,它会通过参数创建Locale对象,传递给MessageUtils,然后在页面中使用MessageUtils来获取文本信息。
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; } } |
MessageFilter.java
public class MessageFilter implements Filter { public void destroy() { }
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 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 { } } |
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> |
index.jsp
<body> <p><%=MessageUtils.getText("hello") + ":" +request.getParameter("username") %>:</p> </body> |
NumberFormat类
NumberFormat类用来对数字进行格式化,我们只需要使用String format(double)一个方法即可。下面是获取NumberFormat实例的方法:
l NumberFormat format = NumberFormat.getNumberFormat()
l NumberFormat format = NumberFormat.getNumberFormat(Locale)
l NumberFormat format = NumberFormat.getCurrentcyFormat()
l NumberFormat format = NumberFormat.getCurrentcyFormat(Locale)
l NumberFormat format = NumberFormat.getPercentFormat()
l NumberFormat format = NumberFormat.getPercentFormat(Locale)
DateFormat类
DateFormat类用来对日期进行格式化,我们只需要使用String format(Date)一个方法即可。下面是获取DateFormat实例的方法:
l DateFormat format = DateFormat.getDateFormat();
l DateFormat format = DateFormat.getTimeFormat();
l DateFormat format = DateFormat.getDateTimeFormat();
l DateFormat format = DateFormat.getDateFormat(int style, Localelocale);
l DateFormat format = DateFormat.getTimeFormat(int style, Localelocale);
l DateFormat format = DateFormat.getDateTimeFormat(int style, Localelocale);
其中style是对日期的长、中、短,以及完整样式。
l SHORT;
l MEDIUM;
l LONG;
l FULL
MessageFormat
MessageFormat可以把模式中的{N}使用参数来替换。我们把{N}称之为点位符。其中点位符中的N是从0开始的整数。
MessageFormat.format(Stringpattern, Object… params),其中pattern中可以包含0~n个点位符,而params表示对点位符的替换文本。注意,点位符需要从0开始。
String p = “{0}或{1}错误”;
String text =MessageFormat.format(p, “用户名”, “密码”);
System.out.println(text);//用户名或密码错误