Mark Gilbert's Blog

Science and technology, served light and fluffy.

How resourceful of you (Part 2 of 2)

My previous post explored the details involved in using ASP.NET 2.0 resource files to localize your application.  In this post, I’ll talk about a few of the snags that I hit when implementing this technique.

Note: To prevent the blog from replacing my special HTML strings with the single character representations (for example, © or ®), I’ve added extra spaces in between each character (for example & c o p y ;).  In order for you to get these to work, please remove the extra spaces.

One of the first phrases that I needed to render included a copyright symbol.  Having some knowledge of HTML, my first stab at it was the following:

<data name=”SiteCopyright”>

<value>& c o p y ; 2007 My Company</value>

</data>

That caused the server to throw an exception.  Removing the & c o p y ; allowed the page to render normally.  I found that & r e g ; (for the registered trademark symbol) also caused the page to error out.

After some trial and error, I found that the “numeric” representation of the copyright and registered trademark symbols worked:

<data name=”SiteCopyright”>

<value>& # 1 6 9 ; Landscape Forms copyright, 2007</value>

</data>

“169” is the ASCII decimal value for the copyright symbol (174 is the value for the registered trademark symbol).

That was the first snag.  Luckily, I had been editing the RESX files directly on the server using Notepad, just to make the troubleshooting go faster.  My development team hit a second snag when a colleague of mine happened to open the file up in the Visual Studio editor, made an innocuous change to a completely unrelated phrase, and uploaded the new version.  The site started throwing errors again.  When we examined the file that made it up to the server – again, using Notepad – we found that the Visual Studio resource editor had replaced & c o p y ; with the ACTUAL copyright symbol of ©.  That of course didn’t sit well with the XML resource files, or with SharePoint.  Once we re-replaced the character, SharePoint accepted the file.

For this third and final snag, I need to climb up on my virtual soap box, so here goes:

<soapbox>In my first attempts to get resource files functioning within SharePoint, I got a LOT of messages to the effect that “resource X not found”.  For these early attempts, I was using a sample that I had copied down off a technical article that formatted the resource file contents as follows (I have since lost the link to that article, so I apologize for the hearsay):

<Data Name=”SiteCopyright”>

<Value>& c o p y ; 2007 My Company</Value>

</Data>

Compare it with the version at the very beginning of this post.  If it only takes you a second or two to see the critical difference, then you’re better than I was.  It took me an hour to figure out what was going on (keep reading).

After probably 45 minutes of my head making contact with the monitor muttering “Why doesn’t this work?!?”, I finally broken down, deleted my resource file, and just tried referencing one of the resource files that were installed with SharePoint.  At this point in my troubleshooting, I still wasn’t positive that I was even putting my custom RESX files in the correct directory.  Once I pointed my custom page layouts to the built in RESX files, and saw them working, I knew I had THAT part at least correct.  The issue then was with my custom RESX file itself.

I decided to take a different approach to building the new custom RESX files – copying the built-in file that I had just proven worked.  I deleted all but one of the <data> tags that were there originally, and pasted in my custom ones.  Once I had them side by side, the difference became glaringly obvious.

Are you ready?

<data name=”SiteCopyright”>

<value>& c o p y ; 2007 My Company</value>

</data>

<Data Name=”SiteCopyright”>

<Value>& c o p y ; 2007 My Company</Value>

</Data>

Notice the capitalization of the first letter in the keywords.  No, it can’t be THAT.  Can it?  The answer is “Yes, yes it can”.  Years of writing HTML by hand (and/or working with a very tolerant browser such as Internet Explorer) has led me into a bad habit of assuming that the markup was not case sensitive.  As I found out here XML is, in fact, case sensitive.  As a result, once I dropped the uppercase letters to lower, SharePoint recognized the tags just fine.

I want to be clear about what I found frustrating with this.  My issue was not that I WANTED to capitalize the first letter of all of my keywords.  In truth, I usually write everything using lower case.

My problem wasn’t even with the author from whom I grabbed the original sample (and where the upper case letters came from in the first place).

My problem was with how the XML itself was being interpreted, and that going from <Data> to <data> suddenly conjured my resources into existence, because as far as the SharePoint and ASPNET engines were concerned, they simply didn’t exist before I cast that level 6 spell of ToLower.</soapbox>

Ok, now that I’m off of my soapbox, I can continue to address this issue with a better semblance of calm.  I did some digging and found that the keywords are case sensitive for a couple of reasons – processing performance, and language localization.  Tim Bray (who was heavily involved in the development of the XML standard) answers this best.  These two links will show you the same response by Bray:

I unfortunately was not able to locate the original context that this post/response was made, so the best that I can do is say that these comments are attributed to Bray, and given his background it seems highly likely that he actually made them.

The reasons he cites for the case-sensitivity are good ones, and now that I realize that’s what’s going on, I can be more vigilant when I’m writing XML (which isn’t really all that often).  More importantly, I can add this as another check in my troubleshooting book, and hopefully save myself 59 minutes of head-banging the next time SharePoint says it can’t find a particular resource tag.

Advertisements

March 5, 2007 - Posted by | MOSS

Sorry, the comment form is closed at this time.

%d bloggers like this: