.net - How to access global resources for localization in class library? -


i've got asp.net website (not web application) , have localized text ui using resources files (.resx). i've got class library contains business logic , there's validation method returns error message. need localise error message cannot find way access global resource file in website. on website, in code behind, can use resources.localisedtext.myerrormessage (where localisedtext name of resource file localisedtext.resx). in class library, cannot reference it.

i've found link http://weblogs.asp.net/rinze/archive/2008/12/03/using-globalization-resources-resx-in-a-class-library-or-custom-control.aspx says can done doesn't seem work me because cannot right namespace website?

i managed work based on resx's designer file. if open , @ resourcemanager property's get accessor, can copy line in order access same resources.

full explanation

first of all, need open resource file's .designer.cs file. should in solution explorer in following image:

resource file designer file location in solution explorer

just double click open , scan (it should near top):
internal static global::system.resources.resourcemanager resourcemanager {

in get accessor, should see assign temp variable. in case looked this:

/*...*/ temp = new global::system.resources.resourcemanager("resources.lang",                global::system.reflection.assembly.load("app_globalresources")); 

so, based on that, created resourcemanager in class wanted use in class library so:

resourcemanager lang = new resourcemanager("resources.lang",                                            assembly.load("app_globalresources")); 

note 1: don't forget add using system.resources; , using system.reflection; top of file.
note 2: doesn't require adding reference web project class library.

then, call getstring method on lang variable resources want.
e.g. lang.getstring("myerrormessage").
it's not elegant calling resources.lang.myerrormessage, gets job done.

hope helps. drop comment below if have questions.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -