如何在其他语言中显示相同的.NET MVC视图? [英] How can I display the same .NET MVC view in other languages?

查看:96
本文介绍了如何在其他语言中显示相同的.NET MVC视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在其他语言中显示相同的视图?下面的代码在屏幕上放置一个带有英文标签的文本框。这很好,但有限。如何以西班牙语,德语显示相同的视图...?

我的.Net MVC模型包含属性属性'Display'和'Required'。



型号......

[必填(ErrorMessage =需要电子邮件)]

[DataType(DataType.EmailAddress)]

[显示(姓名=电子邮件:)]

公共字符串电子邮件{get;组; } b / b
查看...

@ Html.LabelFor(m => m。电子邮件)


@ Html.TextBoxFor(m => m.Email ,new {@class =requiredField})


@Html。 ValidationMessage(Email,*)




感谢您的时间和考虑。

How can I display the same view in other languages? The code below puts a textbox on the screen with a label in English. That’s nice, but limited. How do I display the same view in Spanish, German…?
My .Net MVC model contains property attributes ‘Display’ and ‘Required’.

Model…
[Required(ErrorMessage = "E-Mail is required")]
[DataType(DataType.EmailAddress)]
[Display(Name = "E-Mail:")]
public string Email { get; set; }

View…

@Html.LabelFor(m => m.Email)

@Html.TextBoxFor(m => m.Email, new { @class = "requiredField"})

@Html.ValidationMessage("Email", "*")


Thank you for your time and consideration.

推荐答案

请参阅: http://msdn.microsoft.com/en-us/library/vstudio/c6zyy3s9%28v=vs.100%29.aspx [ ^ ]。



-SA


您可以创建自己的属性类,该属性类派生自预定义的属性类,并覆盖适当的函数以加载字符串来自资源。



虽然我还没有尝试过,但也可以编写像TextBoxFor这样的辅助函数,然后加载适当的字符串。资源键可能由字段名称和上面示例中的函数后缀(如 Email_Required )组成,因此可以自动完成。



另外,我已经比较了任何解决方案的性能。



在我的项目中,我使用了为每个解决方案派生了一个属性类的approch我使用的属性/资源文件对。



LoginDisplayNameAttribute(字符串键)将从中获取一个字符串LoginDisplayResource使用键。如果找不到密钥,那么您可能有一些备份逻辑来尝试其他位置或将密钥本身用作文本。



取决于您编译或复制资源的方式处理丢失的资源可能或多或少容易,因为在某些情况下如果密钥不存在可能会引发异常,这可能会使调试变得更难(你要么必须处理很多异常,要么可能不会停止在异常对于调试目的而言非常重要的其他地方......)



因此,如果资源未编译但只是包含,则最好使用信号源对于每个属性类。



大多数这些东西都可以在网上找到,因为有很多ASP.NET可用的例子。
You can create your own attribute class that derive from predefined one and the override appropriate function to load string from a resource.

Although I haven't tried it, it might also be possible to write your helper function like TextBoxFor and then load appropriate string. Resource key might be composed of the field name and a "function" suffix like Email_Required in the example above so that it can be done automaticaly.

Also, I have compared the performance of any solution.

In my project, I use the approch of having derived atribute classes for each pair of attribute/resource file that I use.

Say LoginDisplayNameAttribute(string key) would get a string from LoginDisplayResource using key. If the key is not found, then you might have some backup logic to try other locations or use the key itself as text.

Depending on how you compile or copy resources, it might be more or less easy to deal with missing resource as in some case an exception might be thrown if the key does not exist which might make debugging somewhat harder (you either have to deal with a lot of exception or it might not stop at other places when the exception really matters for debugging purpose...)

Thus if resource are not compiled but simply include, it might be best to use a signle source for each attribute class.

Most of this stuff can be found on the web as there are a lot of example for ASP.NET available.


这篇关于如何在其他语言中显示相同的.NET MVC视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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