Override XML files

This is a forum to suggest new features for mojoPortal. 

This thread is closed to new posts. You must sign in to post in the forums.
9/5/2012 10:26:01 AM
Gravatar
Total Posts 18439

Re: Override XML files

Consider that the built in localization support of the ASP.NET framework is awesome.
By storing the files in the /App_GlobalResources folder I get strongly typed objects to set my labels like this:
litHeading.Text = BlogResources.BlogCategoriesLabel;

I don't have to do anything to make it localize, by default the thread will execute as the culture specified in the browser preferred language and it will automatically choose the correct .resx file if it exists (or by config settings you can force it to execute as a specific culture), so if my browsert is set to de-DE for German and BlogResources.de.resx exists it will use that and if the key "BlogCategoriesLabel" does not exist in that file it will fall back to the default English file. I get all that for free, no hunting around the file system no checking for missing files or keys needed in my code and the key must exist in the defualt file for it to compile so errors are avoided.

Similarly there is a databinding syntax like this:
<asp:DropDownList ID="ddGMapType" runat="server" EnableTheming="false" CssClass="forminput">
<asp:ListItem Value="G_NORMAL_MAP" Text="<%$ Resources:Resource, GoogleNormalMap %>" />
<asp:ListItem Value="G_SATELLITE_MAP" Text="<%$ Resources:Resource, GoogleSatelliteMap %>" />
<asp:ListItem Value="G_HYBRID_MAP" Text="<%$ Resources:Resource, GoogleHybridMap %>" />
<asp:ListItem Value="G_PHYSICAL_MAP" Text="<%$ Resources:Resource, GoogleTerrainMap %>" />
</asp:DropDownList>

again I don't have to do anything extra to make it localize.

Now do I want to throw that away and build my own thing that I have to support? NO!!!! That would be a lot of work and a huge re-write of good solid working code.

But on a case by case basis I'm willing to do things like this:

if (displaySettings.OverrideCategoryLabel.Length > 0)
{
litHeading.Text = displaySettings.OverrideCategoryLabel;
}
else
{
litHeading.Text = BlogResources.BlogCategoriesLabel;
}

9/5/2012 2:50:22 PM
Gravatar
Total Posts 537
feet planted firmly on the ground

Re: Override XML files

I completely agree with you there Joe - my hope was that ASP.Net might itself provide a way of prioritising/overriding resources, in the same way it falls back from a language file to the default. Clearly not!

Also I assume it is not possible/desirable to move such files around at runtime (from an override location to the App_GlobalResources folder)  - I think that causes the site to recompile?

So your solution is as good as we can get - either we edit the resx files and put them back after upgrading*, or use theme.skin and suggest labels that need overrides.

* to make this easier, is there a case for a different download package, one that contains changed files only?  This would make my life much easier with custom email templates and resx files. Could save LOTS of time when upgrading many sites with different customisations.

9/5/2012 3:12:27 PM
Gravatar
Total Posts 18439

Re: Override XML files

I don't want to try to keep track of which files were changed, that is to much burden that would slow down progress and make myself and other devs reluctant to make changes for beaurocratic reasons and it is likely that I and others would fail at that and forget some of the files that changed. During upgrade users should assume all files were changed, otherwise it is very likely that strange hard to diagnose bugs will happen if people don't update all the files that need to be updated. I think some people have used diff tools to do that occasionally but I think the safest bet is to update all the files. I update all the files when upgrading my own sites.

Note that you should not replace a .resx file with an older version or you may end up with missing keys that were added to the new one, so unfortunately you must re-edit any changes you make to new .resx files.

Therefore I would be very judicious about which labels you promise to change for your customers to keep that to a minimum and work with me to make the case for the ones that really really need to have an override option because they are so commonly requested to be changed by customers for valid reasons that make sense. Some customers will ask for all kinds of things that are not really reasonable so its a judgement thing and I think sometimes pushback is needed for the customer's benefit to avoid those maintenance issues with resx files.

.resx files are indeed compiled by the asp.net compiler into classes behind the scenes at application startup, that is why we started shipping most additional languages in a separate download to reduce the amount compilation. But there is not a good way to put them in other places, the designated location of /App_GlobalResources is a special asp.net folder and is what makes it possible to have strongly typed resource objects that provide intellisense and make it trivial to localize strings. As soon as we add a new key in the resx file we get intellisense for the new key in code. We can always wish for more magic but it really is already doing a lot of heavy lifting for us already so its best to work with the grain of the existing localization framework built into asp.net rather than work against the grain and make it more complicated.

Best,

Joe

9/5/2012 3:23:13 PM
Gravatar
Total Posts 2239

Re: Override XML files

I use WinMerge to copy over my changes to .resx files. This works very well and I haven't run into any issues with it.

The scripts I use for updating all of our mojoPortal sites use robocopy for copying over the "changed" files. I put quotes around changed because often times nothing has really changed in the file but it was opened and saved at some point so the date/time stamp changed so the file appears to be different. 

9/5/2012 3:39:23 PM
Gravatar
Total Posts 1203
Proud member of the mojoPortal team

Help support mojoPortal!
Add-on modules

Re: Override XML files

Hey Joe, would you be willing to share your robocopy site update scripts? I think a lot of people would find them really useful.

Jamie

9/5/2012 3:52:01 PM
Gravatar
Total Posts 2239

Re: Override XML files

I'm working on rewriting them with PS so once I do that, I'll share them. You may want to check out Shuan Geisert's post "Automatically Upgrading mojoPortal Using PowerShell" over at the CODE Colorado State University website.

Thanks,
Joe D.

9/5/2012 5:46:53 PM
Gravatar
Total Posts 216
Community Expert

mojoPortal Hosting & Design @ i7MEDIA!

Re: Override XML files

Joe, your answer gave me an idea.

Basically, the code already reads a different language file and resolves to a default file if the key isn't found in the other language file. This is the functionality we need, so it begs the question - is it possible to sort of 'fool' the code into thinking you're using another language file?

If so, we could make our own language file that's really just an override file, then declare what language we want the site to render in, and it would read that file first, render any keys given there, and then check the default file for keys that are missing. This way we could only change whatever labels we wanted and wouldn't need to update the file with mojo.

The main thing I think we would need is a way to declare our 'language' in the web.config or something, such that it tells the code to look for that language's xml file first.

I don't understand the code behind mojoPortal very well, so please forgive me if this is a stupid question.

Thanks,
-Isaac

9/5/2012 6:37:49 PM
Gravatar
Total Posts 18439

Re: Override XML files

Hi Isaac,

That is good out of the box creative thinking! I'm not sure how feasible it is, you'd have to implement a custom cultureinfo class and plug it in somehow to the .net framework, but it did make me remember reading a blog post where someone was trying to do that for Klingon, with a quick google I found something about that but probably you would need to do more googling and research to determine how feasible it is.

But from a quick glance I'm not sure its possible without code changers in mojoPortal, it all depends on if you can plug in a new culture info class directly without needing changes in mojoPortal.

From a quick read it sounds like it would require changing all use of culture info to use some other custom class that inherits from culture info base class, but maybe changes in the .net framework over time have made it possible. I just don't know without more research.

Best,

Joe

 

9/5/2012 6:52:24 PM
Gravatar
Total Posts 18439

Re: Override XML files

Actually the more I think about it the more clever and brilliant that idea seems.

Skip the Klingon and choose an obscure langauge that we don't support and aren't likely to. Create .resx files for that language that only have the things you want to override then force it to use that culture. Technically you could use any language that you don't care to support in your site and for which you are not deploying the resx files we ship even if it is a supported language, but safer to pick something more obscure.

Fanatastic creative idea Isaac!

Best,

Joe

9/5/2012 6:56:19 PM
Gravatar
Total Posts 18439

Re: Override XML files

Here is a list of languages supported by .NET as of version 2 there is probably a more up to date list somehwere/, by choosing one of those you don't have to implement any custom cultureinfo, just pick one we don't support and give it a try and let us know how it goes.

Best,

Joe

9/5/2012 7:29:47 PM
Gravatar
Total Posts 216
Community Expert

mojoPortal Hosting & Design @ i7MEDIA!

Re: Override XML files

Alright, I've managed to get this working on my site.

I decided to use Konkani because it's at the bottom of this list.
Steps are as follows:

  1. Duplicate the resource file you want to change. I changed blogresources.resx in my case.
  2. Rename the file to use the language extension you want, as found in the link Joe shared.
  3. Open the user.config and add your override code as found here. In my case I added two keys:
    <add key="UseCultureOverride" value="true" />
    <add key="site1uiculture" value="kok" />
    It is important to note that you should probably only use the uiculture for this, so your date and time format strings aren't changed.
  4. Change your new resource file to include what keys you want. It is probably a good idea to remove all of the keys except for the ones you want to change.

A really great aspect about this method is that mojoPortal already supports using different cultures for each site in your mojo installation, so it makes it easy even if you have multiple clients on the same install or if you want to use different text for your own different sites.

Thanks again Joe, this will work well for me I think.
-Isaac

You must sign in to post in the forums. This thread is closed to new posts.