Struts国际化的东西被我小小的一个类就能搞定

Struts国际化的东西叽叽歪歪 不好用,
有那么麻烦么?在JSP 或者 Java 里调用我下面的这个小小的类就可以实现。
package properties;
import java.io.FileInputStream;
import java.util.PropertyResourceBundle;
public class IN18 {
public static void main(String[] args) {
PropertyResourceBundle properties = getPropertyResourceBundle("zh");
System.out.println("The result is: " + properties.getString("viewServiceReportDetailsPdf.TotalCost"));
}
public static PropertyResourceBundle getPropertyResourceBundle(String locale) {
//InputStream fin = null;
PropertyResourceBundle propsRB = null;
// try to find the required prop. file for the locale.
try {
String language = "";
if (locale != null) {
language = locale;
} else {
language = "en";
}
//fin = IN18.class.getResourceAsStream("properties_" + language + ".properties");
FileInputStream fis = new FileInputStream("D:\\xujin\\properties_" + language + ".properties");
//fin = (InputStream) fis;
propsRB = new PropertyResourceBundle(fis);
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
return propsRB;
}
}


如果是中文的话
System.out.println("The result is: " + properties.getString("viewServiceReportDetailsPdf.TotalCost"));
输出的应该是这样的:
材料描述
这个是汉字的Unicode编码,在浏览器一般不用处理,浏览器会自动把它转换成汉字。
但有些时候我们又必须转换它,比如,在PDF里就不能识别它为汉字。
所以,我又加了一个方法:
/**
* Convert unicode to Chinese
* @author xujin.jiao
* @param unicodeStr
* @param language
* @return str
*/
public String convertUnicdToAsiaChar(String unicodeStr, String language) {
if (!"zh".equals(language)) {
return unicodeStr;
}
String checkNumStr = unicodeStr;
String str = unicodeStr.replaceAll("&#",",").replaceAll(";",",");
String[] arrayStr = str.split(",");
String result ="";
for (int i=0; i < arrayStr.length; i++) {
try {
int c = Integer.parseInt(arrayStr[i],10);
if (arrayStr[i].length() != 5) {
result = result + arrayStr[i];
}
else {
if ("#".equals(String.valueOf(checkNumStr.charAt(checkNumStr.indexOf(arrayStr[i]) - 1)))) {
result=result+(char)c;
} else {
result = result + arrayStr[i];
}
}
} catch(NumberFormatException e) {
result = result + arrayStr[i];
}
}
return result;
}

After testing, I found out some bugs in decrypt chinese characters process.
so, i modified this method.
/**
* Convert unicode to Chinese
* @author xujin.jiao
* @param unicodeStr
* @param language
* @return str
*/
public String convertUnicdToAsiaChar(String unicodeStr, String language) {
if (!"zh".equals(language)) {
return unicodeStr;
}
String checkNumStr = unicodeStr;
String str = unicodeStr.replaceAll("&#",",").replaceAll(";",",");
String[] arrayStr = str.split(",");
String result ="";
for (int i=0; i < arrayStr.length; i++) {
try {
int c = Integer.parseInt(arrayStr[i],10);
if (arrayStr[i].length() != 5) {
result = result + arrayStr[i];
}
else {
if ("#".equals(String.valueOf(checkNumStr.charAt(checkNumStr.indexOf(arrayStr[i]) - 1))) &&
"&".equals(String.valueOf(checkNumStr.charAt(checkNumStr.indexOf(arrayStr[i]) - 2)))) {
result=result+(char)c;
} else {
result = result + arrayStr[i];
}
}
} catch (NumberFormatException e) {
result = result + arrayStr[i];
} catch (StringIndexOutOfBoundsException e1) {
try {
checkNumStr.charAt(checkNumStr.indexOf(arrayStr[i]) - 1);
} catch (StringIndexOutOfBoundsException e2) {
result = result + arrayStr[i];
continue;
}
result = result + "#" + arrayStr[i];
}
}
return result;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值