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!
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>
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.
精彩评论