Hooking meta:Resource parsing in ASP.NET 2.0?
I’m mucking around today with some internationalization code in a control that is meant to take over localization instead of the standard resource providers pulling data from a database. I have most of this functionality working but now I’m trying to figure out some better ways to integrate this into the localization scheme.
Hooking up the functionality is not difficult thanks to the provider model that ASP.NET 2.0 has introduced which allows me to replace the existing ResourceManager with my own custom one. This makes it easy to hook up custom processing to any calls to Page.GetLocalResourceObject()/GetGlobalResourceObject() as well as the parsing of <%$ Resources:LinkButton1Resource1.Text %> expressions which gets a me a long way there.
However, one very cool feature that ASP.NET provides is the ability to use this construct:
meta:resourcekey="TextBox1"
which allows ASP.NET to automatically map a control and any resource properties. So if TextBox1 exists and has resources defined for TextBox1.Text and TextBox1.ToolTip both of these properties are automatically localized which results in generated code in the BuildControl_ code of the ControlTree:
box1.ToolTip = Convert.ToString(base.GetLocalResourceObject("TextBox1Resource1.ToolTip"),
CultureInfo.CurrentCulture);
box1.Text = Convert.ToString(base.GetLocalResourceObject("TextBox1Resource1.Text"),
CultureInfo.CurrentCulture);
Great, except that this appears to be a compile time feature with ASP.NET going out at compile time parsing the resource file and source code file and mapping these properties together. The meta: tag is stripped. This means this code is generated at compile time and if the ResourceKey doesn’t exist in the resource file nothing is generated.
Does anybody know if there’s some way to intercept this parsing mechanism in any way or some sort of interface that can be trapped to allow hooking into this and provide ASP.NET with a list of matches here in the same fashion?
I know that custom build providers can be plugged in for things like the <%$ Resources:LinkButton1Resource1.Text %> expressions, but I don’t think this applies to these meta tags.
Anybody have any idea where to look for this?
The Voices of Reason
# re: Hooking meta:Resource parsing in ASP.NET 2.0?
# re: Hooking meta:Resource parsing in ASP.NET 2.0?
It's been a while since I looked into this in-depth, so please let me know if this doesn't help.
# re: Hooking meta:Resource parsing in ASP.NET 2.0?
# re: Hooking meta:Resource parsing in ASP.NET 2.0?
# re: Hooking meta:Resource parsing in ASP.NET 2.0?
Furthermore, the call type "Resources.Web.sitemap.Something" doesn't work. This is something that gives a really nice code when your are using .Resx files, but this doesn't work at all with a custom provider.
Have any of you had luck with these two things? The SDK says that implicit resources should work fine with a custom provider, requiring no implementation... apparantly only when you don't intend to actually precompile your pages?
# re: Hooking meta:Resource parsing in ASP.NET 2.0?
There are a lots of issues to watch out for like the fact that local resources are passed in without a culture (null) so you have explicitly assign a culture if your code is doing any sort of culture specific lookups into a ResourceSet.
# re: Hooking meta:Resource parsing in ASP.NET 2.0?
I would be really thrilled if you could help me then - I invariably get this message every time I try to compile:
error ASPPARSE: Object reference not set to an instance of an object.
I have fallback etc. in a stored procedure in MSSQL, and entries are written to the DB every time a lookup fails, letting the app continue with temp data.
As I said I have zero problems when I'm JIT-viewing the project, only when I'm trying to precompile, every single meta:resourceKey statement crashes the compile with the above message.
If this is getting too specific for this post, please write me at ole@michelsen.dk - I would highly appreciate your help, since I have several large projects I'm moving from .resx, and it really sux to be stuck midstream :-)
# re: Hooking meta:Resource parsing in ASP.NET 2.0?
Am I SOL?
# re: Hooking meta:Resource parsing in ASP.NET 2.0?
# re: Hooking meta:Resource parsing in ASP.NET 2.0?
are you guys able to figure out "Object reference not set to an instance of an object."? i'm having the same problem and i'm not able to figure out how can i solve this issue. I'm using CustomResource provider as described in this article http://msdn2.microsoft.com/en-us/library/aa905797.aspx. but in Provider Factory when i create LocalResourceProvider i want to change vitualpath(resourceType field in DB) depending on what i get through quesrystring. but if i do that i get the object reference error. if you guys could help me i'll be really appreciated. my email id is lax4u@hotmail.com
# re: Object Reference not set
First your Localization sample is awsome ! I implemented my own resource provider based on your sample but I am getting a Object Reference not set when using meta: tags in source code. The strings are in the database ( SQL Server ) and even when running a trace I see no calls made to the server.
Do you know what VS needs when generating meta: resources when compiling the web site
Any help appreciated
Thanks !
# re: Hooking meta:Resource parsing in ASP.NET 2.0?
One thing to remember is that if you have missing meta tags there tend to be problems. If you define a meta tag you should match it otherwise you get those object ref errors. If you use the provider I created and implements IImplicitResourceProvider it shouldn't throw those errors - they are then just ignored. The default provider shows errors.
Sometimes a full rebuild of the project can help (especially when not using WAP)...
# re: Hooking meta:Resource parsing in ASP.NET 2.0?
When using the following syntax in .aspx files
<cc1:CtButton ID="CtButton1" runat="server" onclick="CtButton1_Click"
Text="Go Back" meta:resourcekey="CtButton1Resource1"></cc1:CtButton>
when compiling the web site I get the ObjectReference not set.
Now in the table I have rows for cultures '', 'fr-Ca' and 'en-CA' for the ResouceId CtButton1Resource1.Text and CtButton1Resource1.ToolTip.
Also I tried to modify the code behing the IImplicitResourceProvider to the following to be sure that the object returned was not null but even when setting breakpoints it appears the compiler is not using that dll instance but another one.
object IImplicitResourceProvider.GetObject(ImplicitResourceKey implicitKey, CultureInfo culture) { object oValue = this.ResourceManager.GetObject(ConstructFullKey(implicitKey), culture); if (oValue == null) { return ConstructFullKey(implicitKey) + " could not be loaded"; } else { return oValue; } }
# How to Take Javascript Error Message From Resource File
Hi,
Am very new to ASP.net Technology, currently am working for a ASP.Net 3.5 Application :(,
so am stucking up every where, currently am working for Language Localization.
I have one some javascript validations in my application, i want to take the messages from Resource file, how can i take the resource file value in javascript...
somebody please help me......
Boby
# re: Hooking meta:Resource parsing in ASP.NET 2.0?
Ran into the same problem as you had, and after two days tearing my hair off, I've found where it was stuck. The problem is only when you are working with web sites, and not with web application.
The function CreateLocalResourceProvider in DbSimpleResourceProviderFactory calls WebUtils.GetAppRelativePath(virtualPath) to strip off the virtual path of the web application. Problem is that this is getting called during pre-compilation and the WebUtils.GetAppRelativePath is referring to HttpContext.Current.Request. This is null and therefore you get the 'Objectreference not set' error.
# re: Hooking meta:Resource parsing in ASP.NET 2.0?
public static string GetAppRelativePath(string logicalPath, string basepath) { logicalPath = logicalPath.ToLower(); string appPath = basepath; //HttpContext.Current.Request.ApplicationPath.ToLower(); if (appPath != "/") appPath += "/"; else // Root web relative path is empty - strip off leading slash return logicalPath.TrimStart('/'); return logicalPath.Replace(appPath, ""); }
And in DbSimpleResourceProviderFactory, where the function is called, I changed it to:
string ResourceSetName = WebUtils.GetAppRelativePath(virtualPath, DbResourceConfiguration.Current.DesignTimeVirtualPath);And now it works fine for my Web Site project.
Btw, I also did a convertion for MySql syntax in DbResourceDataManager.cs.
# re: Hooking meta:Resource parsing in ASP.NET 2.0?
since resource access is also provider based, maybe a custom resource provider would do the trick for you.