Localized Javascript Resources with MVC
We needed to have a handful of localized strings available to javascript. A simple but effective approach we're taking with asp.net mvc is adding a route such as this:
routes.MapRoute("ScriptResources", "res/resources.js", new { controller = "System", action = "ScriptResources" });
The controller simply calls View("Resources") and the View creates a simple json object like this
ForumsRes =
{
prompt_clearAbuseReason: <%= AntiXss.JavaScriptEncode(Resource.prompt_clearAbuseReason) %>,
prompt_deleteReason: <%= AntiXss.JavaScriptEncode(Resource.prompt_deleteReason) %>,
prompt_pageExit: <%= AntiXss.JavaScriptEncode(Resource.prompt_pageExit) %>,
prompt_reportAbuseReason: <%= AntiXss.JavaScriptEncode(Resource.prompt_reportAbuseReason) %>,
prompt_stickyUntil: <%= AntiXss.JavaScriptEncode(Resource.prompt_stickyUntil) %>,
prompt_undeleteReason: <%= AntiXss.JavaScriptEncode(Resource.prompt_undeleteReason) %>,
rte_alreadyOpen: <%= AntiXss.JavaScriptEncode(Resource.rte_alreadyOpen) %>
}
The web page can include it like any other script and reference a loc string like ForumsRes.rte_alreadyOpen. Another nice thing is you can easily OutputCache this too.
Simple idea, but thought it was worth sharing.