views:

237

answers:

2

My current solution consists of several Class Libraries and a Website. I'm in the process of globalizing the application and I realized that my resources need to be accessed by all the projects not just the website so placing my resources in the App_GlobalResources folder didn't work.

I added my resources to one my class Libraries and now I'm trying to figure out what the best way of accessing the resources are from my markup. When my resources were in the App_GlobalResources folder I was able to access them by using an expression such as this:

<$ Resources: MyApp.Name %>  for server controls

Or

<%=Resources.MyApp.Name %> for plain text

What's the best way of accessing my Resources from my website aspx files now that they are in a Class Library DLL?

Thanks for your help!

A: 

I use an Util class library which apart from a host of other useful classes and functions contains an i18n class with a Public Shared Function that returns the value (string) of a resource in a resource file based on its name and LCID. I add a reference to that dll in my web projects and where ever needed, say in a code-behind file for a label I write : MyLabel.Text = i18n.GetResourceString("MyStringName",1033) You can find a more detailed description of the method I use here I trust that with the details provided there you can adopt or adapt my solution.

esjr
That works for the code-behind but what about the markup?
mga911
I'm sorry, but I don't understand.You could write in the mark-up : <asp:Label id="MyLabel" runat="server" Text='<%# Util.i18n.GetResourceString("MyStringName", 1033) %>' />Assuming the assembly is called Util.dll.But you still would have to call MyLabel.DataBind from the code-behind for it to work.So you might as well writeMyLabel.Text = i18n.GetResourceString("MyStringName",1033)directly in the code-behind onload.
esjr
By the way, this would work without code-behind : <div> <%=Util.i18n.GetResourceString("NameText", 1033)%> </div>Again assuming, the assembly is Util.dll, referenced in your project and i18n's GetResourceString is a Public Shared Function.
esjr
+1  A: 

I found a great article which discusses Extending the Resource-Provider Model. It allows for the use of expressions to access external resources:

The syntax for a $Resources expression for the default provider model (explicit global resources) is the following.

<%$ Resources: [resourceType], [resourceKey] %>

The same expression can be used to access external resources when the ExternalResourceProviderFactory is configured, with the following syntax change.

<%$ Resources: [assemblyName]|[resourceType], [resourceKey] %>

For example, to retrieve a resource from the CommonResources.dll assembly, from the global resource type "CommonTerms", you would use the following explicit expression.

<asp:Label ID="labGlobalResource" runat="server" Text="<%$ Resources:CommonResources|CommonTerms, Hello %>" ></asp:Label>
mga911