ASP.net MVC本地化|为什么用困难的方式呢? [英] ASP.net MVC Localization | why do it the difficult way?

查看:95
本文介绍了ASP.net MVC本地化|为什么用困难的方式呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主要问题:

如果已经有内置的本地化助手,为什么还要使用自定义本地化助手呢?

Why use custom localization helpers if there is something built-in doing the same already?

长话

目前,我一直在阅读许多用于本地化asp.net mvc网站的选项.例如,大多数职位都是从2008年10月22日开始发布的.

Currently I have been reading many options to localize your asp.net mvc website. Most of the posts are old, from oktober 22 2008 for instance.

我认为链接最多的选项之一是: eWorld上的Matt Hawley .此选项创建一个可与

I think one of the most linked option is the following: Matt Hawley on eWorld. This option creates an Html helper which can be used with

Html.Resource("ResourceName") 
Html.Resource("GlobalResourceFileNameWithoutExtension, ResourceName")

获取本地和全球资源.其他使用

for local and global resources. Other use the

<asp:label meta:resourcekey="lblNameResource1" runat="server"/>

代替

<label></label>

尝试使用< .asp:labels>时发现的方法遇到的一些问题,我的部分页面收到了一些漂亮的错误,例如在部分页面呈现时:

Some problems I had while trying the methods I found were when using <.asp:labels> my partial pages recieve some pretty errors like on my partial page rendering:

Validation of viewstate MAC failed. If this application is hosted by a Web Farm or
cluster, ensure that <machineKey> configuration specifies the same validationKey and
validation algorithm. AutoGenerate cannot be used in a cluster.

最后,我想知道,如果有像这样的标准方法,为什么所有这些麻烦呢?

In the end, I am wondering, why all this trouble if there are standard methods like:

<%= GetLocalResourceObject("lblNameResourceKey") %>

使用内置函数的不利之处是什么?当然,我对必须使用字符串键并不满意,但是所有方法都使用它们,因此我认为这是不可避免的.我能想到的唯一缺点是,每个字符串都需要其键,而使用asp:labels具有一些内置的自动功能(如lblName.Text/lblName.ToolTip等).但是,为什么这个马特·霍利(Matt Hawley)如此流言?他使用与内置GetLocalResourceObject完全相同的方法,只是使用自己的辅助方法对其进行了不同的命名?

What are the downsides of using the built in functions? Of course I am not happy with having to use string keys but all methods use them so I think that is inevitable in the end. The only downside I can think of is that every string needs its key, while using asp:labels have some autofunctions builtin (like lblName.Text / lblName.ToolTip etc). But than why is this Matt Hawley so rumored? He uses the exact same approach as the built-in GetLocalResourceObject, only naming it differently with his own helper methods?

或者我想念什么吗?

推荐答案

如果您参考Matt的文章(更准确地说,

If you refer to Matt's post (more accurately the post prior to the more popularly linked one!) you'll see the reason he's added those HTML helpers is that he wanted to design something that delegates the responsibility of choosing the local resource location to the ViewEngine (i.e. if you look at the code you see that his subclassed ViewEngine puts the view location in ViewData). This helps with situations where the Views are stored in a database or something similarly non-standard or don't even derive from the Page class.

如果您只打算使用WebFormViewEngine,可能就不值得失去太多的睡眠,并且以下内容应该可以正常工作(正如您在问题中提到的那样)

If you're only ever going to use the WebFormViewEngine it might not be worth losing much sleep over and something like the following should work fine (as you mention in your question)

<%= GetLocalResourceObject("lblNameResourceKey") %>

您提供的第二种方法使用服务器端Label控件.我猜你为什么会出错是因为对ViewState的依赖正在悄悄溜走,并且由于MVC不会发出ViewState页面会出错.

The second method you give uses a serverside Label control. I'd guess the reason why you're getting errors is that a dependency on ViewState is sneaking in somewhere and as MVC does not emit ViewState the page is going to error.

关于使用ASP.NET GetLocalResourceObject及其与全球等效的缺点:-

Regarding the downsides of using ASP.NET GetLocalResourceObject and it's Global equivalent:-

缺点

  • 每个资源键都有很多字符串,没有强类型的输入/错误突出显示.

优势

  • 很多灵活性,可以在不重新编译整个应用程序的情况下部署resx文件.

Craig在回答中提到的Visual Studio生成的强类型资源需要与之形成鲜明对比:-

Which needs to be contrasted with strongly typed resources generated by Visual Studio that Craig mentioned in his answer:-

缺点

  • 更改字符串将强制重新编译应用程序并重新部署应用程序DLL.

优势

  • 严格键入,因此没有ASP.NET本地化黑魔法,因此视图中没有神奇的字符串清洁代码.

与这些事情总是存在权衡,因此最好选择最适合您的情况(例如,您的部署策略是什么,在字符串资源中期望的更改量)

There's always tradeoffs associated with these things so it's best to choose what's most suitable for your situation (e.g. what your deployment strategy is, the amount of change you expect in the string resources)

这篇关于ASP.net MVC本地化|为什么用困难的方式呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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