请看下面的内容#LocalStrings_zh_CN.propertiespage.title=国际化form.label=请输入你的姓名form.button=提交中文繁体版:#LocalStrings_zh_TW.propertiespage.title=國際化form.label=請輸入您的姓名form.button=提交英文版:#LocalStrings_tw.propertiespage.title=internationalizationform.label=Please enter your nameform.button=submitLocalStrings_zh_CN.properties、LocalStrings_zh_TW.properties和LocalStrings_tw.properties这三个文件均以系统缺省的编码方式保存。form.jsp改成:<%@ page contentType="text/html;charset=utf-8"%><%@ page import="beans.LocaleStrings"%><%LocalStrings lss = new LocalStrings("LocalStrings", request.getLocale());%><html> <head> <title><%=lss.getString("page.title")%></title> </head> <body> <form method="POST" action="test.jsp"> <label><%=lss.getString("form.label")%></label> <input type="text" name="name" size="20"> <input type="submit" value="<%=lss.getString("form.button")%>"> </form> </body></html>beans.LocalStrings的源文件如下:package beans;import java.util.ResourceBundle;import java.io.UnsupportedEncodingException;import java.util.Locale;public class LocalStrings{ protected ResourceBundle rb; protected boolean encoded; public LocalStrings(String baseName, Locale locale) { this.rb = ResourceBundle.getBundle(baseName, locale); if(locale.getCountry().equals("CN") || locale.getCountry().equals("TW")) encoded = true; } public String getString(String key) { String value = rb.getString(key); if(encoded) { try { byte bs[] = value.getBytes("ISO-8859-1"); return new String(bs, "GBK"); } catch(UnsupportedEncodingException uee) { return value; } } return value; }}从浏览器访问form.jsp,我们应该看到正确的输出,然后通过浏览的Internet选项,将浏览器的语言设置成“中文(台湾)[zh-tw]”,再访问form.jsp,我们就能看到繁体字的提示信息了;再将浏览器语言设置成“英语(美国)[en-us]”,再访问form.jsp。为什么要用LocalStrings到重新编码这些字符串呢,查看ResourceBundle的源程序可以发现,它读取文件中时,并没有对字符串进行编码,所以我们不得不在LocalStrings中对使用正确的编码对这些区域相关的字符串编码
我不明白的是 3个properties文件LocalStrings_zh_CN.properties、LocalStrings_zh_TW.properties和LocalStrings_tw.properties
<%LocalStrings lss = new LocalStrings("LocalStrings", request.getLocale());%>要读取的是LocalString.properties文件 可是根本没有这个文件 只有LocalStrings_zh_CN.properties、LocalStrings_zh_TW.properties和LocalStrings_tw.properties这3个文件啊 难道说是这3个文件中都包含LocalStrings字符串 根据程序来看,使用<%LocalStrings lss = new LocalStrings("LocalStrings", request.getLocale());%>可以根据情况来选择读取对应3个properties中的一个
是这样理解的吗 请高手指点一下 实在是不明白
我不明白的是 3个properties文件LocalStrings_zh_CN.properties、LocalStrings_zh_TW.properties和LocalStrings_tw.properties
<%LocalStrings lss = new LocalStrings("LocalStrings", request.getLocale());%>要读取的是LocalString.properties文件 可是根本没有这个文件 只有LocalStrings_zh_CN.properties、LocalStrings_zh_TW.properties和LocalStrings_tw.properties这3个文件啊 难道说是这3个文件中都包含LocalStrings字符串 根据程序来看,使用<%LocalStrings lss = new LocalStrings("LocalStrings", request.getLocale());%>可以根据情况来选择读取对应3个properties中的一个
是这样理解的吗 请高手指点一下 实在是不明白
解决方案 »
- java properties 读取配置文件,并修改其中的一个key的值 并保存
- spring3报错'sessionFactory' or 'hibernateTemplate' is required
- EL表达式获取属性问题
- The session has been logged off or has expired-BO
- 在线求助:如何创建视图
- 100分:怎么还在当前页显示。
- 重发:请教jsp服务器配置,急!很久没有弄出来了
- 网上考试,怎么实现对简答题的判分(电脑自动)?
- 高分寻求:如何通过IP地址查询Whois信息?
- 请问关于JavaBean,Servlet的问题
- 如何利用XML或自关联数据库+JSP+javascript实现树形列表???
- params.htm提交到params.jsp的问题?
不知道是不是这个,之前我也在调这个,很怪异的