I'm trying to localize client side Javascript in a ASP .NET MVC application. The way I've settled on this is I want to return a JavaScript object from a view page.
The idea is very similar to what is described here: http://blogs.msdn.com/b/robj/archive/2009/03/12/localized-javascript-resources-with-mvc.aspx
It's unclear to me from the blog post what the view (or partial view?) looks like. I would like to do something like this:
<script type = "text/javascript">
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) %>}
</script>
within the view definition. By having the script tag actually in the .cshtml file, I can make an external reference to the .cshtml file and I will get intellisense (as per http://weblogs.asp.net/scottgu/archive/2007/06/21/vs-2008-javascript-intellisense.aspx).
However, I want to reference the javascript (really .cshtml file) something like this from another view:
<script type="text/javascript" src="@Url.Content("~/res/MyResources")"></script>
where the res/MyResources URL would be mapped (via the custom route) to the controller that invokes the view (or partial view) that serves up the JavaScript. The problem is, I think, that what I return in the view includes the tags. I want this at design time, so I can get the intellisense. But when the javascript is return at run time, I want to return the content of the view (or partial view) with the script tags scripted.
As you can probably tell, I'm pretty green at ASP .NET MVC, so hopefully what I said make some sense. Is there a way to strip the tags within the controller? I imagine I could do something on the client side (maybe using JQuery), but I'd rather have what is served up be a vanilla javascript external file.
I found the answer I was looking for on the MSDN ASP .NET forum. Here's the answer:
http://forums.asp.net/p/1765704/4819373.aspx/1?p=True&t=634641505060799388
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With