Push a copy of Resources to client in javascript
By Anatoly Mironov
In one of my previous posts I told about pushing a copy of an object into client. Now I’ll try to copy my resource values into client. The problem is often that we must create multiple localization resources: first as resx-file. If we use much ajax and client side rendering, we must provide some localization there, too. If they are different subsets of localization resources, it isn’t a problem. But when you get overlapping, it becomes more difficult to manage and sync them. See how Microsoft Ajax Library provides some strings: What if we just copy the values from resx file into client? If there are not business constraint it can make the development much easier. Let’s try it. For that we need Reflection. We start with changing the access modifiers on resx file to public: Then we must get all static properties of the auto-generated class (ResXFileCodeGenerator):
var t = typeof(Takana.Localization);
var props = t.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
const string f = "\\"{0}\\":\\"{1}\\"";
var properties = props.Select(p => string.Format(f, p.Name, p.GetValue(t, null))).ToArray();
var entries = string.Join(",", properties);
var json = string.Format("{{{0}}}", entries);
var script = "var takanaLoc = " + json + ";\\n";
Page.ClientScript.RegisterStartupScript(GetType(), "takanaLoc1", script, true);