Reputation: 3957
I'm attempting to change the built in .Net culture fr-CA, using the following method:
CultureAndRegionInfoBuilder cib = new CultureAndRegionInfoBuilder("fr-CA", CultureAndRegionModifiers.Replacement);
try
{
Console.WriteLine("Updating " + cib.CultureName);
cib.NumberFormat.CurrencyGroupSeparator = ",";
cib.NumberFormat.CurrencyDecimalSeparator = ".";
cib.NumberFormat.NumberGroupSeparator = ",";
cib.NumberFormat.NumberDecimalSeparator = ".";
cib.Register();
Console.WriteLine("Culture updated.");
}
catch (Exception e)
{
Console.WriteLine(e);
Console.ReadKey();
}
However, the Register() call fails with "System.InvalidOperationException: The 'Register' method failed because the custom culture 'fr-CA' already exists."
Is it possible to update the built-in culture? According to the docs (http://msdn.microsoft.com/en-us/library/system.globalization.cultureandregioninfobuilder.cultureandregioninfobuilder.aspx), it looks like I can just update it, though I might be reading that wrong.
Upvotes: 1
Views: 2705
Reputation: 5156
You may be better to create a copy of an existing culture and Register it with a new name such as "fr-CACustom".
[Note that a custom culture can be registered on a computer only by a user who has administrative rights on that computer.]
try
{
CultureAndRegionInfoBuilder cib = new CultureAndRegionInfoBuilder("fr-CACustom", CultureAndRegionModifiers.None);
cib.LoadDataFromCultureInfo(new CultureInfo("fr-CA"));
Console.WriteLine("Creating" + cib.CultureName);
cib.NumberFormat.CurrencyGroupSeparator = ",";
cib.NumberFormat.CurrencyDecimalSeparator = ".";
cib.NumberFormat.NumberGroupSeparator = ",";
cib.NumberFormat.NumberDecimalSeparator = ".";
// add some region info also
cib.LoadDataFromRegionInfo(new RegionInfo("CA"));
cib.RegionEnglishName = "RegionInfo derived from French-speaking Canada";
cib.Register();
Console.WriteLine("New culture created.");
}
catch (Exception e)
{
Console.WriteLine(e);
Console.ReadKey();
}
This code registers the custom culture identified by the culture name in the %WINDIR%\Globalization system directory, where %WINDIR% is the Windows operating system directory. This will make the custom culture available to other applications.
CultureInfo test = new CultureInfo("fr-CACustom");
[Note: in order to have access to the CultureAndRegionInfoBuilder class you need a reference to the sysglobl assembly which is not reference by default in most Visual Studio project templates.]
Upvotes: 1
Reputation: 3957
I just had to add:
CultureAndRegionInfoBuilder.UnRegister("fr-CA");
before Register().
Upvotes: 6