简体   繁体   中英

Accessing Resources File from separate assembly

I have created a Resource file in my Web Project to localize the application. As such, it will contain all the translated text for labels and buttons, etc in the Web App. However I also have some back end code that I use to generate and send emails out to my users and I would like those emails to be localized as well. The code that sends out the emails is in a separate assembly from the Web Project so I'm wondering what the best way to share this resource file with the assembly is?

In my Web Project code I'm just doing this:

HttpContext.GetGlobalResourceObject("Localization", "somekey").ToString();

so I could simply do this in the assembly code:

System.Web.HttpContext.GetGlobalResourceObject("Localization", "somekey").ToString();

and that will work fine as long as the assembly is invoked from my web app...but if for whatever reason I want to invoke that assembly's method from a different app that is not web based then there will be no HttpContext and it will fail. Is there a better recommended way of doing this so I can share the resources between the two?

thanks a lot

You can build an ExpressionBuilder to access strings from another assembly.

The article .NET String Resources includes the class StringExpressionBuilder with the expression Strings:

<asp:Label ID="AppTextLabel" runat="server" 
    Text="<%$ Strings:MyCompany.MyApp.MyStrings, AppText %>" />
<asp:Label ID="LibTextLabel" runat="server" 
    Text="<%$ Strings:MyCompany.MyLib.MyStrings, LibText %>" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM