Skip to content

Instantly share code, notes, and snippets.

@goenning
Created December 16, 2011 15:06
Show Gist options
  • Save goenning/1486389 to your computer and use it in GitHub Desktop.
Save goenning/1486389 to your computer and use it in GitHub Desktop.
Resource file to javascript
[HttpGet]
public ActionResult LocalizedScript()
{
string javaScript = SerializeResourceToJavaScript();
return Content(javaScript, "application/javascript");
}
[NonAction]
private string SerializeResourceToJavaScript()
{
CultureInfo uiCulture = Thread.CurrentThread.CurrentUICulture;
var resourceSet = CommonStrings.ResourceManager.GetResourceSet(uiCulture, true, true);
StringBuilder js = new StringBuilder();
js.Append("var CommonStrings = {").Append(Environment.NewLine);
foreach (DictionaryEntry entry in resourceSet)
{
js.AppendFormat("\"{0}\": \"{1}\",", entry.Key.ToString(), entry.Value.ToString());
js.AppendLine();
}
js.Append("}");
return js.ToString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment