Difference between these two ways of localizing a string in an aspx/ascx file?
Asked Answered
P

4

12

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?

Pretor answered 21/5, 2010 at 14:57 Comment(1)
Doesn't this syntax <%= Resources.ResourceFile.ResourceName %> only give you access to global resources? Can you access App_LocalResources? I can't seem to.Alundum
M
2

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...

Magruder answered 26/5, 2010 at 10:49 Comment(1)
Doesn't this syntax <%= Resources.ResourceFile.ResourceName %> only give you access to global resources? Can you access App_LocalResources? I can't seem to.Alundum
S
2

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

Speiss answered 26/5, 2010 at 5:48 Comment(2)
Considering you're directly quoting MSDN documentation, you should probably put that text into a block quote.Downcast
hey man i already posted link of msdn there and the text pasted by me after reading documentation at msdnSpeiss
M
2

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...

Magruder answered 26/5, 2010 at 10:49 Comment(1)
Doesn't this syntax <%= Resources.ResourceFile.ResourceName %> only give you access to global resources? Can you access App_LocalResources? I can't seem to.Alundum
B
1

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.

Bouchier answered 23/5, 2010 at 4:32 Comment(3)
I'm not sure I understand. In the first example, it does not create a new resource file for each page. It has one resource (called ResourceFile in the example) that contains ResourceName. Both examples are calling the same resource file for the same resource.Pretor
@Pretor - First approach usually used for Local Resources and the second one usually use for GobalResources. but behind the scene both of them use a same method. e.g. GetGlobalResource and GetLocalReource which both of them give some parameter in different ways and pass them to a Getresource method which is the main overload or method.Bouchier
ah I see. I was just using it incorrectly then. I was referencing a single global resource file each time I did it that way.Pretor
K
1

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.
Knickknack answered 26/5, 2010 at 17:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.