jiaoxujin 发表于 2013-1-27 05:28:52

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
* @paramunicodeStr
* @paramlanguage
* @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,10);
   if (arrayStr.length() != 5) {
   result = result + arrayStr;
   }
   else {
         if ("#".equals(String.valueOf(checkNumStr.charAt(checkNumStr.indexOf(arrayStr) - 1)))) {
         result=result+(char)c;
          } else {
         result = result + arrayStr;
          }
   }
         } catch(NumberFormatException e) {
         result = result + arrayStr;
         }
   }
   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
* @paramunicodeStr
* @paramlanguage
* @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,10);
   if (arrayStr.length() != 5) {
   result = result + arrayStr;
   }
   else {
         if ("#".equals(String.valueOf(checkNumStr.charAt(checkNumStr.indexOf(arrayStr) - 1))) &&
         "&".equals(String.valueOf(checkNumStr.charAt(checkNumStr.indexOf(arrayStr) - 2)))) {
         result=result+(char)c;
          } else {
         result = result + arrayStr;
          }
   }
         } catch (NumberFormatException e) {
         result = result + arrayStr;
         } catch (StringIndexOutOfBoundsException e1) {
         try {
         checkNumStr.charAt(checkNumStr.indexOf(arrayStr) - 1);
         } catch (StringIndexOutOfBoundsException e2) {
         result = result + arrayStr;
         continue;
         }
         result = result + "#" + arrayStr;
         }
   }
   return result;
}
页: [1]
查看完整版本: Struts国际化的东西被我小小的一个类就能搞定