Globalization difference in inline tags in ASP.NET

对着背影说爱祢 提交于 2019-12-19 03:27:32

问题


Is there any advantage/downfalls between using the inline write tag instead of the resource tag? Example:

<%=Resources.Site.SampleString %>

The resources tag (expression tag) as seen in any MSDN example:

<asp:Literal id="Literal1" runat="server" text="<%$ Resources:Site, SampleString %>" />

I find the first option far easier to use, and it has IntelliSense, but maybe it won't function the same?


回答1:


These methods will function exactly the same. The latter simply calls first one; there is a reason why strongly-typed resources access code is being generated in the background. Therefore you can use whatever method you please.

By the way, there is also another way - by using meta:resourcekey attribute. So you should be able to write:

<asp:Literal id="Literal1" runat="server"
             meta:resourcekey="SampleString" text="Default one" />

and it all should work exactly the same.

EDIT on implicit Localization.

What I forgot to mention, is that with meta:resourcekey certain conditions have to be met. The conditions are:

  • Values are taken from App_LocalResources, therefore related resource file need to exist
  • Related resource file name must be pagename.resx, for example: Default.aspx.resx, Default.aspx.es.resx
  • The resource file must contain keys in form of resourcekey.propertyname, for example SampleString.Text, SampleString.ID (although I wouldn't localize control ID's)
  • Because of how resources are generated, the key mentioned above must exist in invariant resource file (Default.aspx.resx), or it won't be localized.



回答2:


I realised after some time that the <%=Resources.Site.SampleString %> method does not have designer support (which is understandable). This doesn't bother me, but it is a difference (for future readers).

So if you need or want designer support, the second option would be necessary.



来源:https://stackoverflow.com/questions/6017960/globalization-difference-in-inline-tags-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!