Serializing .NET Resources (.resx) as JSON (part 2)

So after playing with my own code after a while I realized that there was a slight flaw in my thinking.  Since every application is slightly different in the way it determines what culture to use, and that each request is technically a different Thread, I need a more generic way to capture that information.

I decided to use a querystring value.  Here is my updated code in regards to reading the querystring culture, setting it and fired up the appropriate ResourceManager..

 

ResourceManager rm = new ResourceManager(ConfigurationManager.AppSettings["JSResourcesAssemblyType"].ToString(),
                Assembly.LoadFile(ConfigurationManager.AppSettings["JSResourcesAssemblyPath"].ToString()));
            if (context.Request.QueryString["CultureCode"] == null) return;
            var culture = context.Request.QueryString["CultureCode"].ToString();            
            ResourceSet rs = rm.GetResourceSet(new CultureInfo(culture), true, true);            
            var sbInitial = "var rm = {";
            var sb = new StringBuilder(sbInitial);            
            var resEnum = rs.GetEnumerator();
            while (resEnum.MoveNext())
            {
                if (sb.ToString() != sbInitial) sb.Append(",");
                sb.Append("\"" + resEnum.Key + "\":\"" + 
                    resEnum.Value.ToString().Replace("\r\n", "").Replace("\"", "\\\"") + "\"");
            }
 
            sb.Append("}");
            sb.ToString();
 
            context.Response.ContentType = "text/javascript";
            context.Response.Write(sb.ToString());
06.09.2011 09:36 by Danny Gershman | Comments (0) | Permalink

Comments

Add comment


(Will show your Gravatar icon)  


Enter Numbers In Image Above: biuquote
  • Comment
  • Preview
Loading