Rick Strahl's Web Log

Wind, waves, code and everything in between...
ASP.NET • C# • HTML5 • JavaScript • AngularJs
Contact   •   Articles   •   Products   •   Support   •   Search
Ad-free experience sponsored by:
ASPOSE - the market leader of .NET and Java APIs for file formats – natively work with DOCX, XLSX, PPT, PDF, images and more

ResoureProvider, ResourceManagers Relationships in ASP.NET 2.0


:P
On this page:

 

I’ve been chomping away at my custom resource provider/manager and I have most of this working nicely with the ability to dynamically update resource content which was the original goal of this whole process.

 

But even as all of this is coming together I still feel a bit lost on the high level picture of the relationships between the ResourceProvider, ResourceManager and ResourceSets.

 

It looks to me that there’s one ResourceProvider per page, which in turn gets one ResourceSet at a time for each locale that is accessed. Locale information is pulled into ResourceSets one at a time it looks like as needed so as to minimize overhead until needed. The sets are cached automatically by the provider (or ResourceManager if one is used) so the resources load once and then stay cached.

 

All fine and good. ASP.NET allows overriding of the ResourceProvider via web.config and a custom implementation.

 

What I can’t figure out is how this actually happens and what is parceling out the ResourceManagers and Providers. For example, ASP.NET has a set of global resources which can be accessed with Resources.Resources. If you do this and look at the ResourceManager you’ll find that it’s the standard .NET resource manager not my custom resource manager.

 

It looks like there’s also no way to get a ‘global’ or ‘currently active’ reference to a ResourceProvider or ResourceManager. So if I’m in custom control how can I get a reference to the active ResourceProvider/Manager so I can do custom localization for example. Right now the control would have to know about a specific instance of the provider/manager.

 

I suppose I can use Page.GetLocalResourceObject(), PageGetGlobalResourceObject() for this? Is that the idea? These methods will go down to the active provider and do their thing properly. The page (or TemplateControl really) has its own ResourceProvider:

 

protected object GetLocalResourceObject(string resourceKey)

{

 

    if (this._resourceProvider == null)

    {

 

        this._resourceProvider = ResourceExpressionBuilder.GetLocalResourceProvider(this);

 

    }

 

    return ResourceExpressionBuilder.GetResourceObject(this._resourceProvider, resourceKey, null);

 

}

 

But it’s not publicly accessible…

 

This makes it tough for example to ask for a ResourceWriter and write resources out to the resource store unless you hardcode to a specific provider…

 

Has anybody found some useful documentation that talks about the overall structure of the ResourceProvider model or more simply how to get access to the active ResourceProvider object?

Posted in ASP.NET  Localization  

The Voices of Reason


 

Vadi
October 12, 2006

# re: ResoureProvider, ResourceManagers Relationships in ASP.NET 2.0

sorry to distract you, But I am wondering how this "Localization" stuffs cane applied to Javascript text outputs. For example, I would like to show a "Loading .. " text strip from the JS. And, I dont think that I can have multiple versions of "Loading .." texts based on the browsers language thru JS.

Is there anything out there ?

-
Vadi

# DotNetSlackers: ResoureProvider, ResourceManagers Relationships in ASP.NET 2.0


Rick Strahl
October 13, 2006

# re: ResoureProvider, ResourceManagers Relationships in ASP.NET 2.0

Vadi, you can do server side translation of your client side JavaScript so that translated text gets generated into the page.

function StartLoading()
{
   var Ctl = $('lblProgressText');
   Ctl.innerHTML = '<%= this.GetGlobalResourceObject("Resources","LoadingText") %>';
}


It's not real pretty, but that works. Alternately you can also generate a Resource section right into the JavaScript with static strings and then use those static 'variables' in the code.

Juma
October 13, 2006

# re: ResoureProvider, ResourceManagers Relationships in ASP.NET 2.0

Another cool way to do it is to use the new Localize server control in ASP.NET 2.0 and you will see the text localized automagically using the resource files.

function StartLoading()
{
var Ctl = $('lblProgressText');
ctl.innerHTML = '<asp:Localize ID="Localize1" runat="server" meta:resourcekey="Localize1Resource1"></asp:Localize>';

}

Rick Strahl
October 15, 2006

# re: ResoureProvider, ResourceManagers Relationships in ASP.NET 2.0

Juma, yeah that it's an interesting use of that control. I suspect though that using the code I showed is quite a bit more efficient since no extra controls are involved and no impliict resource lookup and assignment which is more expensive for perf.

Vadi
November 01, 2006

# re: ResoureProvider, ResourceManagers Relationships in ASP.NET 2.0

Out of planet for a while .. Sorry
But, what if the javascript is embedded in a JS file. I think this both methods would not work.

Rick Strahl
November 02, 2006

# re: ResoureProvider, ResourceManagers Relationships in ASP.NET 2.0

No obviously that won't work since a standalong JavaScript file by its very definition is 'static'. You can however make it an ASPX file and serve the JavaScript out of that.

Vadi
November 02, 2006

# re: ResoureProvider, ResourceManagers Relationships in ASP.NET 2.0

Yeh, you're right. But, I am wondering what could be a perfect solution for this problem. Because, nowadays people mostly uses JS to make async. calls to the server or UI jimmicks. So, this issue would be definetly a critical problem when it comes to resource handling.

And same time, not all the JS codes can be embedded in the .aspx page and it is not handy too. One way I can think of is embed constant variables in the .aspx page that can get the data from the way you have mentioned here and use the constant variables in JS functions while displaying any text.

I think, may be ATLAS kind of frameworks may properly address this issue.

What do you think ?

Greg Kuper
March 21, 2007

# re: ResoureProvider, ResourceManagers Relationships in ASP.NET 2.0

Did you ever figure out anymore about the ResourceProvider? I am trying to figure out how to clear the cached values. Thanks. Love you blog, always a lot of help to me.

Rick Strahl
March 21, 2007

# re: ResoureProvider, ResourceManagers Relationships in ASP.NET 2.0

Greg, yes I did - I created a fairly complex data driven resource manager with a UI front end for editing resources in real time. It works well. As far as I know you cannot unload resources once they are loaded - the resource provider model doesn't make any allowances for this especially in ASP.NET because the provider is essentially completely hidden from the ASP.NET layer once invoked.

I've taken to unloading the AppDomain to refresh - by touching web.config. That seems to work but it's a pretty drastic step. OTOH, this is a develop time operation in most cases so it's not that big of a deal.

You can see a sample of the resource manager and interface here:
http://www.west-wind.com/presentations/wwhoverpanel/sample/ResourceProvider/CustomerList.aspx

Greg Kuper
April 13, 2007

# re: ResoureProvider, ResourceManagers Relationships in ASP.NET 2.0

Wow Rick that editor is very nice is that something you are selling?

Rick Strahl
April 14, 2007

# re: ResoureProvider, ResourceManagers Relationships in ASP.NET 2.0

No but it's a sample you can download from here:

http://www.west-wind.com/weblog/posts/8746.aspx

Grady
September 19, 2007

# re: ResoureProvider, ResourceManagers Relationships in ASP.NET 2.0

Did you ever figure out how to set a global resource object in a custom control? Thanks!
 

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2019