問題描述
我一直在研究我的第一個 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
我發(fā)現(xiàn)了一種方法來做到這一點,但我不確定這是否是最好的方法.思路是這樣的
I discovered ONE method to do this, but I'm not sure if this is the best method. Here's the idea
- 您將
App_LocalResources
文件夾作為子文件夾放在各自的 View 文件夾中 - 您創(chuàng)建與您的視圖相對應的 resx 文件... IE:
Index.resx
或About.resx
- 您將 Resx 文件屬性設置為Public"(PublicResXFileCodeGenerator)、Embeded Resource"、Views.[viewname]"命名空間
- 創(chuàng)建一個名為Title"的新資源名稱,并將您想要的任何內(nèi)容放入 Value 中.
- You put the
App_LocalResources
folder as a sub folder within your respective View folder - You create resx files that correspond to your Views... IE:
Index.resx
orAbout.resx
- You set the Resx files properties to be "Public" (PublicResXFileCodeGenerator), "Embeded Resource", "Views.[viewname]" namespace
- Create a new resource name called "Title" and put whatever you want in the Value.
現(xiàn)在我可以使用類似這樣的方式引用本地資源
Now I can reference the Local Resources using something like this
<%= MyProj.Views.Home.Index.Title%>
我看到的唯一問題是資源是嵌入的,如果不重新編譯項目,我無法添加新的語言環(huán)境.
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.
這篇文章提供了更多關于它的細節(jié).
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 一起工作?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!