Wednesday, March 23, 2011

Max Length of CultureInfo.Name Property

I was looking for the answer to the question: "What is the maximum length of the CultureInfo.Name property?" and came across this question. I've answered the question but thought that I would find the answer easier if it was on my blog (ie. here).

The reason that the maximum is 84 characters is because that is what the CultureAndRegionInfoBuilder class restricts the length to. But you can't just declare a culture called "ThisIsLongerThan8Characters" because you must seperate the culture name with - (or _ it seems) with each part being a maximum of 8 characters long.

The following code creates a culture with a length of 84 characters, registers it, uses it and unregisters it.

string cultureName = "qwertyui-12345678-qwertyui-12345678-qwertyui-12345678-qwertyui-12345678-qwertyui-123";
Console.WriteLine( "MAX LENGTH: " + cultureName.Length );
try {
    CultureAndRegionInfoBuilder.Unregister( cultureName );
} catch {
    Console.WriteLine( "Cannot remove culture" );
}

CultureAndRegionInfoBuilder builder = new CultureAndRegionInfoBuilder( cultureName , CultureAndRegionModifiers.None );
             
CultureInfo ci = new CultureInfo( "en-AU" );
RegionInfo ri = new RegionInfo( "US" );

builder.LoadDataFromCultureInfo( ci );
builder.LoadDataFromRegionInfo( ri );
builder.Register();

CultureInfo info = new CultureInfo( cultureName );
            
Console.WriteLine( DateTime.Now.ToString( info.DateTimeFormat.LongDatePattern ) );
Console.WriteLine( info.Name );
Console.WriteLine( info.DisplayName );

try {
    CultureAndRegionInfoBuilder.Unregister( cultureName );
} catch {
    Console.WriteLine( "Cannot remove culture" );
}

No comments:

Post a Comment