Max length of the CultureInfo.Name property
Mayb开发者_开发技巧e somebody knows, what is the possible maximum length of the CultureInfo.Name property in the .NET Framework 4.0.
Answer: 84
Documentation: (thanks @lethek in comment) http://msdn.microsoft.com/en-us/library/system.globalization.cultureandregioninfobuilder.cultureandregioninfobuilder%28v=vs.100%29.aspx
I was able to make a new culture using the CultureAndRegionInfoBuilder class that was 84 characters long with the below code.
The CultureAndRegionInfoBuilder will not allow a name longer than 84 characters. To get to 84 characters you need to ensure that each part that you seperate by "-" must be 8 characters or less. You cannot just create a culture called "ThisIsLongerThan8Characters" because it is longer than 8 characters without a seperator. (Note that I think you can use "_" in the same manner, but I've not tried it)
To get the following code working you need to reference the sysglobl
assembly and import the System.Globalization
namespace.
The code below tries to unregister the culture at the start and at the end. Once registration is done I load a CultureInfo, format a date and display the Name and DisplayName.
Hope this is helpful.
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" );
}
It 11. Here is the code:
var count = CultureInfo.GetCultures(CultureTypes.AllCultures)
.Select(ci => ci.Name.Length)
.Max();
According the following code :
public static void Test()
{
var culturesNames = from c in CultureInfo.GetCultures(CultureTypes.AllCultures)
select new { c.DisplayName, c.DisplayName.Length, c.Name };
foreach (var ci in culturesNames.OrderBy((o) => o.Length))
{
Console.WriteLine("{0} : {2} {1}", ci.DisplayName, ci.Length, ci.Name);
}
}
The max length is 50. Do not forget a developer can create custom culture info.
精彩评论