1 首先写三个language.properties文件如下图
2 传入参数判断 为哪一种语言
String editon = request.getParameter("editon") == null ? "e" : request.getParameter("editon"); String separator=File.separator; String path="en_US"; if(editon.equals("e")) { path="en_US"; } else if(editon.equals("s")) { path="zh_CN"; }else if(editon.equals("t")){ path="zh_tw"; }
String paths = this.getServletContext().getRealPath("/") +"language" + separator + path + separator +"language.properties"; System.out.println( " add_user_alert_rulers.jsp paths = " + paths); VtcMessage msg = new VtcMessage(paths); InternationalUtil international=new InternationalUtil();
package pcpapp; import java.io.FileInputStream; import java.io.InputStream; import java.io.PrintStream; import java.util.Properties; public class VtcMessage { private static Properties prpIniKeys = new Properties(); private static String strMessageFileName = "strMessageFileName"; private static String charsetName = "GBK"; private boolean debug = false; public void setDebug(boolean blnDebug) { this.debug = blnDebug; } public boolean getDebug() { return this.debug; } public void setCharset(String charsetName) { charsetName = charsetName; } public String getCharset() { return charsetName; } public String getMessage(String sErrorCode) { String sTmp = ""; try { sTmp = prpIniKeys.get(sErrorCode).toString(); sTmp = new String(sTmp.getBytes("iso-8859-1"), charsetName); return sTmp; } catch (Exception e) { e = e; sTmp = "The System/Error Message not found. Error Code No.: " + sErrorCode; return sTmp; } finally { } return sTmp; } public String getMessage(String sErrorCode, String sParam) { int nStart = 0; String sTmp = new String(); String sReturn = new String(); try { sTmp = prpIniKeys.get(sErrorCode).toString(); nStart = sTmp.indexOf("PARAMETER1"); sReturn = sTmp.substring(0, nStart) + sParam + sTmp.substring(nStart + 10); sReturn = new String(sReturn.getBytes("iso-8859-1"), charsetName); return sReturn; } catch (Exception e) { e = e; sReturn = "The System/Error Message not found. Error Code No.: " + sErrorCode; return sReturn; } finally { } return sReturn; } public String getMessage(String sErrorCode, String sParam1, String sParam2) { int nStart1 = 0; String sTmp = ""; String sReturn = ""; try { sTmp = prpIniKeys.get(sErrorCode).toString(); nStart1 = sTmp.indexOf("PARAMETER1"); int nStart2 = sTmp.indexOf("PARAMETER2"); sReturn = sTmp.substring(0, nStart1) + sParam1 + sTmp.substring(nStart1 + 10, nStart2) + sParam2 + sTmp.substring(nStart2 + 10); sReturn = new String(sReturn.getBytes("iso-8859-1"), charsetName); return sReturn; } catch (Exception e) { e = e; sReturn = "The System/Error Message not found. Error Code No.: " + sErrorCode; return sReturn; } finally { } return sReturn; } public VtcMessage() { String strUatPath = "/ini/characterRes.ini"; try { InputStream is = VtcMessage.class.getResourceAsStream(strUatPath); prpIniKeys.load(is); is.close(); } catch (Exception e) { e.printStackTrace(); } } public VtcMessage(String strUatPath) { try { FileInputStream inpStreamIni = new FileInputStream(strUatPath); prpIniKeys.load(inpStreamIni); inpStreamIni.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { VtcMessage vtcMsg = new VtcMessage(); System.out.println(vtcMsg.getMessage("CN006006")); } }
6 internalUit.java
package com.cpcnet.mc.util; import java.lang.reflect.Field; import java.util.Properties; import pcpapp.VtcMessage; public class InternationalUtil extends VtcMessage{ /** * ��д��ȡ���ʻ��ַ�������������� */ public String getMessage(String sErrorCode, Class vtcClass) { String sTmp = ""; try { if ("VtcMessage".equals(vtcClass.getSimpleName())) { Field field = vtcClass.getDeclaredField("prpIniKeys"); field.setAccessible(true); Properties prpIniKeys = (Properties) field.get(null); sTmp=prpIniKeys.get(sErrorCode).toString(); return sTmp; }else{ super.getMessage(sErrorCode); } } catch (Exception e) { sTmp = "The System/Error Message not found. Error Code No.: " + sErrorCode; return sTmp; } return sTmp; } }