MVC4本地化.从视图访问resx [英] MVC4 localization. Accessing resx from view

查看:68
本文介绍了MVC4本地化.从视图访问resx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,我想从该文件上的特定本地资源访问资源字符串.正如您从网络表单中所了解的那样:

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

Bam.如果当前的文化是丹麦语,则标题为"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.但是对于如此简单"的东西而言,这似乎确实是相当多的工作,并且已经很好,很容易地集成到Web表单中.必须有另一种方式.就像其他所有东西一样,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.resxResource.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是因为它是.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

Also make sure that the resources are set to 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; }

...
}

更多详细信息,请参见

More details can be found on this post.

这篇关于MVC4本地化.从视图访问resx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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