JS国际化

var controller;

var rootPath = getRootPath();

//Js入口地方
$(document).ready(function() {
	jQuery.fn.ready(getPropertyFileName);
});
//获得项目名 如/TestAAA
function getRootPath() {
	//获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp
	var curWwwPath = window.document.location.href;
	//获取主机地址之后的目录,如: uimcardprj/share/meun.jsp
	var pathName = window.document.location.pathname;
	var pos = curWwwPath.indexOf(pathName);
	//获取主机地址,如: http://localhost:8083
	var localhostPaht = curWwwPath.substring(0, pos);
	//获取带"/"的项目名,如:/uimcardprj
	var projectName = pathName
			.substring(0, pathName.substr(1).indexOf('/') + 1);
	return (projectName);
}


/**
 * 国际化主调函数
 */
function getPropertyFileName(){
        //获得.properties文件名
	var PropertyFileName=$("#PropertyFileName").val();
	if(PropertyFileName==null ||PropertyFileName.length<0||PropertyFileName==""){
		PropertyFileName=getParamter("PropertyFileName");
	}
	else if(PropertyFileName.length>0&&PropertyFileName!=""){
		geti18n(PropertyFileName);
	}
	
}
//从后台读取对应文件 并返回Map值
function geti18n(PropertyFileName){
      var postUrl=rootPath+"/xx/I18N/i18n.do?PropertyFileName="+PropertyFileName;
		$.ajax({
			url:postUrl,
			success:function(result){
				if(result.error){
					alert(result.error);
				}else 
				if(result){
					controller=new LanguageController(result.lang,result.titles);
					setSpanName();
				}else{
					//alert('没找到资源文件');
				}
				
			},
			dataType:"json",
			 	async: false,
			 	error:function(){
			}
		});
		}


function LanguageController(lang,titless) {
	this.language = {
			lang: lang,
			titles:titless
	};
}

/**
 * 获取所有class为‘paltform-i18n’的dom对象,替换国际化内容 key值是.properties文件
 * 的键 根据键值到.properties文件得到对应的值
 */
function setSpanName() {
	var palt = $(".paltform-i18n");
	try {
		if (controller) {
			for ( var i = 0; i < palt.length; i++) {
				var spankey=$(palt[i]).attr("key");	
				if (spankey == null || spankey == ""||spankey==undefined) {
					continue;
				}
				var valuestr=controller.language.titles[spankey];
				if(valuestr!=null || valuestr!=""){
					$(palt[i]).html(controller.language.titles[spankey]);
				}
			}
		}else{
		}
	} catch (e) {
	}
}
//根据键值得到国际化内容
function getLanguageValue(key) {
	try {
		if (controller) {
			var value = controller.language.titles[key];
			if (value == null || value == "") {
				return key;
			}
			return value;
		
		}else{
			return key;
			}
	} catch (e) {
		top.setStatusBarText(e);
	}
}




国际化文件i18n-info.properties


000000=Database connection disconnect

100001=Operation failure

000001=Save failed

000002=Query failed

000003=delete failed

000004=Update failed

000005=Reset failed


页面

//存放文件名
<input type="hidden" name="propertyname" id="propertyname" value='propertyname'/>

<td width="17%"><span id="xx" class="paltform-i18n" key="yinbaobianhao">xx:</span></td>



后台代码
   /**
	    * 
	    *@User   :Test
	    *@date   :2014-6-17 下午04:25:55
	    *@return :Object
	    *@userFor :
	    */
	   @RequestMapping(value = "/i18n")
	   @ResponseBody
	   public Object i18n(HttpServletRequest request){
		    Map<String,Object> result = new HashMap<String, Object>();
		    Map<String,Object> content = new HashMap<String, Object>();
	        //文件名称
	        String fileName = request.getParameter("fileName");
	        //语言类型
	        String i18n = request.getParameter("i18n");
	        HttpSession session = request.getSession();
                //项目部署位置
			String ctxDir = session.getServletContext().getRealPath(String.valueOf(File.separatorChar));
	        String location = ctxDir+"jasframework\\i18n\\resources\\" + i18n + "\\" +fileName+".properties";
	        
	        try {
				InputStream fin = new FileInputStream(new File(location));
				Properties prop = new Properties();
				prop.load(fin);
				Set<Object> keys = prop.keySet() ;
				for(Object v:keys)
				{
					content.put((String)v, prop.getProperty((String)v));
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
	        result.put("lang", i18n);
	        result.put("titles", content);
	        return result ;
	   }

你可能感兴趣的:(国际化)