Best way to expose resource strings to JavaScript files in ASP.NET MVC?
Best way to expose resource strings to JavaScript files in ASP.NET MVC?
回答1
settings.
Thus, if the browser is set to French, the French resources, and only the French Resources, will be returned.
Steep 1. Create a resource file RLocalizedText.resx into App_GlobalResources folder. And create fill it with all the string.
Steep 2. Create ResourcesController
using System.Web.Mvc;
namespace PortalACA.Controllers
{
public class ResourcesController : Controller
{
// GET: Resources
public ActionResult Index()
{
Response.ContentType = "text/javascript";
return View();
}
}
}
Steep 3. Create Index.cshtml view from ResourcesController
@using System.Collections
@using System.Globalization
@using System.Resources
@using Resources
@{
Layout = null;
// Get a set of resources appropriate to
// the culture defined by the browser
ResourceSet resourceSet =
@RLocalizedText.ResourceManager.GetResourceSet
(CultureInfo.CurrentUICulture, true, true);
}
// Define the empty object in javascript
var Resources = {};
@foreach (DictionaryEntry res in resourceSet)
{
// Create a property on the javascript object for each text resource
@:Resources.@res.Key = "@Html.Raw(
HttpUtility.JavaScriptStringEncode(res.Value.ToString()))";
}
The choose where you want use it.
Steep 4A (Layout). If you want use it on whole site, then need put this script reference on _Layout (Shared View) over @RenderSection
<script src="@Url.Content("~/Resources/Index")"></script>
@RenderSection("scripts", required: false)
Steep 4B (View). If you want to see only in some views.
@section Scripts
{
<script src="@Url.Content("~/Resources/Index")"></script>
}
Steep 5. Now it is the time to use it. Select a view where you need see the strings on Resources files and put this code.
@section Scripts
{
<script>
$(document).ready(function () {
alert(Resources.General_Excepcion);
});
</script>
}
That's all Folks! Hope it help others!
回答2
The solution I have used is to use a Razor to create a json object that contains all the resource strings for a given application area eg "Customers". Like so:
<script type="text/jscript">
@{
ResourceSet resourceSet = JsCustomersRes.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
var sbInitial = " var CustomersResourceObj = {";
var sb = new StringBuilder(sbInitial);
var resEnum = resourceSet.GetEnumerator();
while (resEnum.MoveNext())
{
if (sb.ToString() != sbInitial)
{
sb.Append(",");
}
sb.Append("\"" + resEnum.Key + "\":\"" + resEnum.Value.ToString().Replace("\r\n", "").Replace("\"", "\\\"") + "\"");
}
sb.Append("}");
}
@(Html.Raw( sb.ToString()));
The resource file "JsCustomersRes" can be placed along with the particular controller views directory or in the shared view directory. It should have "Custom Tool" set to "PublicResXFileCodeGenerator" in the file advanced properties.
You can then get the resource string from the json object in your script:
var resString = CustomersResourceObj[key];
where "key" is the key string of the resource string you want. Just add the Razor as a partial view to your layout page or individual page as you require and that's it!
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2019-09-17 jQuery Ajax calls and the Html.AntiForgeryToken()
2018-09-17 bat文件中调用传参的问题
2015-09-17 异常的中英文对照
2014-09-17 多线程和信号量