Difference between these two ways of localizing a string in an aspx/ascx file?
When I started localizing a website the first time, I just did the localization like this:
<%= Resources.ResourceFile.ResourceName %>
and it seems to work perfectly fine. However, the ReSharper 5.0 Beta does it like this:
<asp:Localize Text="<%$ Resources: ResourceFile, ResourceName %>" runat="server">
Value
</asp:Localize>
Does it matter which way it gets done?
Also, why does ReSharper keep the original text inside the localize control?开发者_开发问答 I thought it was there in case the Value inside the Resource file was empty, it could show the "default" text. This does not appear to be the case. Is it safe to remove it and just self close the localize control?
well, you can't use the <%= %> server tag on a asp server control.
so
<asp:Localize Text="<%= Resources.ResourceFile.ResourceName %>" runat="server">
Value
</asp:Localize>
will result in a compilation error.
Unfortunately you cannot pass dynamic data to server control properties unless it is databound where you can apply the <%# %>
server tag, for example:
<asp:Repeater runat="server">
...
<asp:Localize Text="<%# Resources.ResourceFile.ResourceName %>" runat="server">
Value
</asp:Localize>
...
</asp:Repeater>
You can always move this to the codebehind but that sucks.
the <%$ %>
'thing' works however if you go by it prepare to enter maintenance hell (unless of course we are talking about a 3 page application...)
Personally i use the <%= %>
and i never use re-sharper to globalize/localize my apps.
Also, i've never used the <asp:Localize />
server control and i've had no problems...
Following information i found on msdn which might help you to understand difference you want
To retrieve global resources using strong typing
Resources.ResourceFile.ResourceName is used to retrieve global resources using strong typing
Resources are compiled into the namespace Resources, and each default resource becomes a member of the Resources class. For example, if you have created the default resource file WebResources.resx and the file contains a resource named WelcomeText, you can reference the resource in code as shown in the following code
String welcome; welcome = Resources.WebResources.WelcomeText;
for more detail : http://msdn.microsoft.com/en-us/library/ms227982.aspx
Explicit Localization
<asp:Button ID="Button1" runat="server"
Text="<%$ Resources:WebResources, Button1
Caption %>
The resource expression takes the following form, where Class is optional, unless the resource is a global one, and ResourceID is required:
The Class value identifies the resource file to use when you use global resources. When .resx files are compiled, the base file name, without extensions, is used as the class name of the resulting assembly, explicitly. If you want to use resources from a local resource file (one that matches the current page name), you do not have to include a class name. This is because ASP.NET matches the page class to the resource class.
The ResourceID value is the identifier of the resource to read. In the previous example, the Text property for the button is read from the global resource file WebResources.resx (or the appropriate localized version). In that file, ASP.NET uses the value for the resource with the identifier Button1Caption and for the page itself. To set page properties, you can use resource expressions in the @ Page directive
more about this : http://msdn.microsoft.com/en-us/library/ms227427(v=VS.100).aspx
The first One Create a seperate Rsource file foreach page (module) but the second one Create One (or afew) andput all of resourcekeys on it.
The second approach let you easily create new languages for your application because all of the strings gathered in one place and you can give it to anybody for translation.
afaik there is a difference, and its a matter of timing.
I haven't confirmed it, but I'd really expect <%$ to occur a lot earlier in the page life cycle.
- <%= its pretty much a <% Response.Write("Some Text") %>, which is why you can't use it in a Lot of places in the aspx i.e. it need to be done when the page is being Rendered
- <%# occurs during DataBind / which is far from initialization of the page/control. Note that the DataBind code might be using other properties that are set earlier, so that's an important difference.
- Given the above and that you can use <%$ in control properties, I'd really expect it to occur v. early in the page/control lifecycle.
精彩评论