Created
January 3, 2013 01:03
-
-
Save hanigamal/4439943 to your computer and use it in GitHub Desktop.
resmanager.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!Util) { | |
var Util = {}; | |
} | |
Util.ResourceManager = function(culture, resourcePath) | |
{ | |
this.createResObj(culture, resourcePath); | |
} | |
Util.ResourceManager.prototype = | |
{ | |
getString : function(key) | |
{ | |
if(typeof key != "string") | |
{ | |
throw new Error("a key with string type need to be specified"); | |
} | |
if(typeof Util_ResourceManager_LocalizedResObj =="object") | |
{ | |
if(key in Util_ResourceManager_LocalizedResObj) | |
{ | |
return Util_ResourceManager_LocalizedResObj[key]; | |
} | |
} | |
if(typeof Util_ResourceManager_WorldResObj == "object") | |
{ | |
if(key in Util_ResourceManager_WorldResObj) | |
return Util_ResourceManager_WorldResObj[key]; | |
} | |
throw new Error("string not found for key:" + key); | |
}, | |
createResObj: function(culture, resPath) | |
{ | |
//check whether the resource object is created | |
if(typeof Util_ResourceManager_LocalizedResObj === 'undefined'){ | |
this.downloadResFile(resPath + "/" + culture + ".txt", "Util_ResourceManager_LocalizedResObj"); | |
} | |
if(typeof Util_ResourceManager_WorldResObj === 'undefined'){ | |
this.downloadResFile(resPath + "/en-us.txt", "Util_ResourceManager_WorldResObj"); | |
} | |
}, | |
downloadResFile : function(url, objName) | |
{ | |
var req = this.createXmlHttpRequest(); | |
req.open("GET", url, false); | |
req.setRequestHeader("Accept", "application/x-javascript"); | |
req.send(); | |
this.constructResObject(req.responseText, objName); | |
}, | |
constructResObject : function(resString, objName) | |
{ | |
tag = document.createElement('script'); | |
tag.type = "text/javascript"; | |
tag.text = "var " + objName + "={" + resString + "}"; | |
if (tag != null) { | |
document.getElementsByTagName("head")[0].appendChild(tag); | |
} | |
}, | |
createXmlHttpRequest : function() | |
{ | |
if (typeof XMLHttpRequest != "undefined") { | |
return new XMLHttpRequest(); | |
} | |
else if (typeof ActiveXObject != "undefined") { | |
var ieReq = new ActiveXObject("Msxml2.XMLHTTP"); | |
if (!ieReq) { | |
ieReq = new ActiveXObject("Microsoft.XMLHTTP"); | |
} | |
return ieReq; | |
} | |
else { | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment