App_LocalResources 如何与 MVC 一起工作? [英] How does the App_LocalResources work with MVC?

查看:13
本文介绍了App_LocalResources 如何与 MVC 一起工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究我的第一个 MVC 应用程序,我想对它进行本地化.我知道如何使用 App_GlobalResources,但我更喜欢使用 App_LocalResources.

I've been working through my first MVC application, and I'd like to localize it. I know how to use the App_GlobalResources but I'd prefer to use the App_LocalResources.

在典型的 WebForms 应用程序中,您将 App_LocalResources 文件夹与 ASPX 文件放在同一目录中……这在 MVC 中是否相同(IE:我是否将它们放在views"目录中的相应子文件夹中)?

In a typical WebForms application, you put the App_LocalResources folder in the same directory as the ASPX file... is this the same in MVC (IE: do I put them in the respective subfolders in the "views" directory)?

另外,在您的视图中访问 App_GlobalResources 时,您可以这样做 <%=Html.Encode(Resources.Index.Title)%> 但是我无法使用 App_LocalResources.我不想让我的 <label runat="server" 来利用 meta:resourcekey

Also, when accessing the App_GlobalResources in your view you can do it like so <%=Html.Encode(Resources.Index.Title)%> however I cannot do that with the App_LocalResources. I don't want to have to make my <label runat="server" in order to take advantage of the meta:resourcekey

我发现了一种方法来做到这一点,但我不确定这是否是最好的方法.思路是这样的

I discovered ONE method to do this, but I'm not sure if this is the best method. Here's the idea

  1. 您将 App_LocalResources 文件夹作为子文件夹放在各自的 View 文件夹中
  2. 您创建与您的视图相对应的 resx 文件... IE:Index.resxAbout.resx
  3. 您将 Resx 文件属性设置为Public"(PublicResXFileCodeGenerator)、Embeded Resource"、Views.[viewname]"命名空间
  4. 创建一个名为Title"的新资源名称,并将您想要的任何内容放入 Value 中.
  1. You put the App_LocalResources folder as a sub folder within your respective View folder
  2. You create resx files that correspond to your Views... IE: Index.resx or About.resx
  3. You set the Resx files properties to be "Public" (PublicResXFileCodeGenerator), "Embeded Resource", "Views.[viewname]" namespace
  4. Create a new resource name called "Title" and put whatever you want in the Value.

现在我可以使用类似这样的方式引用本地资源

Now I can reference the Local Resources using something like this

<%= MyProj.Views.Home.Index.Title%>

我看到的唯一问题是资源是嵌入的,如果不重新编译项目,我无法添加新的语言环境.

The only problem I see with this is the fact that the resources are embedded and I cannot add new locales without re-compiling the project.

这是我对这种方法的参考.

推荐答案

不应将 App_GlobalResources 或 App_LocalResources 与 MVC 一起使用.这是因为这些资源的工作方式.这些资源没有嵌入到您的项目中,而是由 ASP.NET 运行时编译.这意味着当不在 ASP.NET 运行时中运行时,您的资源将不可用,这意味着如果您使用其中的资源,您将失去对视图和控制器进行单元测试的能力.

You should not use App_GlobalResources or App_LocalResources with MVC. That is because of the way these resources work. These resources are not embedded in your project, but are instead compiled by the ASP.NET runtime. This means that your resources will not be available when not running inside the ASP.NET runtime, which means you would lose the ability to unit test your views and controllers if you use resources in them.

这篇文章提供了更多关于它的细节.

This post gives more details about it.

相反,您应该使用您在问题中提到的方法.将您的资源放在您想要的任何位置,并将其设置为嵌入式资源",自定义工具应为PublicResXFileCodeGenerator".

Instead, you should use the approach you mention yourself in your question. Put your resource whereever you want, and set it to 'Embedded resource' and the custom tool should be 'PublicResXFileCodeGenerator'.

这篇关于App_LocalResources 如何与 MVC 一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆