Fixing the sample code for getting ELK cultures on other platforms

by Michael S. Kaplan, published on 2006/11/22 08:46 -05:00, original URI: http://blogs.msdn.com/b/michkap/archive/2006/11/22/1122416.aspx


Sometimes one blog post leads to something that explains a problem someone was seeing with another blog post.

For example. take Sample code for getting ELK cultures on other platforms, which was based on ELK cultures for other platforms.

There was a comment in the post that I missed from Andy Brown which explained that there was a problem getting the code to work right on Server 2003. I really wish I had noticed the comment (I am not sure what happened there, sorry about that Andy!), but my more recent Where the hell did Replacement Locales come from? post does explain what is going on here....

A replacement locale (which is what cy-GB is on XP SP2 and Vista) cannot change the TextInfo or CompareInfo properties, for the reasons the later post gives. And of course those two properties are cy-GB in the LDML file.

But on Server 2003, cy-GB is not a replacement culture, and further its TextInfo and CompareInfo properties are set to something invalid (since it is not built in and the install of it by this whole process has not happened yet. So it will fail with the error Andy pointed out.

To fix, you have to set these two properties to something valid either in LDML as this post suggests, or programatically as I would suggest since you are writing code anyway:

using System;
using System.Globalization;

namespace Testing {
    class Test {
        [STAThread]
        static void Main(string[] args) {
            CultureAndRegionInfoBuilder carib = CultureAndRegionInfoBuilder.CreateFromLdml(@"c:\cy-GB.ldml");
            CultureInfo ci = new CultureInfo("en-GB");
            carib.TextInfo = ci.TextInfo;
            carib.CompareInfo = ci.CompareInfo;
            carib.Register();
        }
    }
}

And there you have it, the code that will work here! (Or you can hand-edit the LDML file if the extra two lines of code scare you!)

 

This post brought to you by "" (U+183d, a.k.a. MONGOLIAN LETTER ZA) 


# Andy Brown on 6 Feb 2007 4:16 PM:

Fantastic, I'm off to try that ... thanks!

Andy


Please consider a donation to keep this archive running, maintained and free of advertising.
Donate €20 or more to receive an offline copy of the whole archive including all images.

referenced by

2010/09/10 Doing the minimum isn't always the best plan...

go to newer or older post, or back to index or month or day