MVC4 本地化.从视图访问 resx [英] MVC4 localization. Accessing resx from view
问题描述
在我看来,我想从该文件的特定本地资源中访问资源字符串.正如您从网络表单中所知道的那样:
In my view I would like to access resource strings from the specific local resource on that file.. Just as you know it from web-forms:
(string)GetLocalResource("Title");
无痛且顺畅.该框架处理从文化信息代码扩展 (.en-EN.resx) 中获取的 .resx 文件.
Painless and smooth. The framework handles which .resx file to get from the culture info code extension (.en-EN.resx).
在带有 Razore 视图的 MVC4 中这可能吗?以及如何?
Is that possible in MVC4 with Razore view? And how?
我注意到我可以将 .resx 文件上的 Custom Tool
属性设置为 PublicResXFileCodeGenerator
.这样我就可以从视图中访问它.例子:我已经用丹麦语和英语为 index.cshtml 创建了 2 个资源文件.就像我在网络表单中一样.这是我希望我能写的内容(Custom Tool Name
属性设置为ViewResource"):
Iv'e noticed that i can set the Custom Tool
property on the .resx file to PublicResXFileCodeGenerator
. That way I can access it from the view. Example:
I've created 2 resource files for index.cshtml in danish and english. Just as I would in web-forms. Here's what I wish i could write (Custom Tool Name
property set to 'ViewResource'):
@ViewResource.Title
呸.如果当前文化是丹麦语,则标题将是Forside",如果是英语,则将是Home".但相反,我得到的唯一选项是选择一个特定的文件.然后从中选择所需的字符串:
Bam. If current culture is danish, title will be "Forside", and if english it would be "Home". But instead the only options I am given is choosing a specific file. And from that choose the desired string:
@ViewResource.Index_cshtml_en-EN_resx.Title
那不是动态的.所以我想我可以制作一个扩展类,以某种方式替换 en-EN/da-DK.但是,对于如此简单"并且已经很好且容易地集成到网络表单中的东西来说,这似乎真的是相对大量的工作.必须有另一种方式.当然,mvc 团队像其他一切一样为我们提供了一些智能机制:)
Thats not dynamic. So I thought I could make an extension class that would replace the en-EN/da-DK somehow. But that really seems like relatively alot of work for something so "simple" and already well and easy intergrated into web-forms. There has to be another way. Surely the mvc team has some smart mechanism for us like everything else :)
推荐答案
我使用了特殊的 .NET 文件夹 App_LocalResources
.
I used the special .NET folder App_LocalResources
.
步骤如下
将 .resx
文件添加到该文件夹(即:Resource.resx
、Resource.es-ES.resx
)
Add the .resx
files to that folder (i.e.: Resource.resx
, Resource.es-ES.resx
)
右键单击每个.resx
文件并选择properties
并确保选择以下内容
Right click on each .resx
file and select properties
and make sure the following are selected
Build Action: Embedded Resource
Custom Tool: PublicResXFileCodeGenerator
Custom Tool Namespace: Resources
然后在您的视图中,您可以使用 Resources
命名空间来访问 .resx
文件中的文本
Then in your view you can use the Resources
name space to access the text in your .resx
file
<h2>@Resources.Resource.Global_Title<h2>
@Resources
因为这是您在 Custom Tool Namespace
和 .Resource
中指定的名称,因为这是 Custom Tool Namespace
和 .Resource
的名称代码>.resx 文件
@Resources
because that is the name that you gave in the Custom Tool Namespace
and .Resource
because that is the name of the .resx
file
还要确保资源设置为Public
要从任何模型访问资源,只需添加一行
To access the resources from any model just add a single line
using Resources; //<<-- This line
namespace Concordia_CRM.Models
{
public class SolicitudDemo
{
[Required]
[Display(Name = "SD_NombreEmpresa", ResourceType = typeof(Resource))]
public string NombreEmpresa { get; set; }
...
}
可以在此发布.
这篇关于MVC4 本地化.从视图访问 resx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!