Search code examples
asp.netlocalizationglobalizationcultureinfo

Changing and Saving Custom Culture After Its Registered


I recently registered a few custom cultures in ASP.NET and was asked later by our client to change something about one of them. For example, I registered en-EU which is a blanket "English for European Union" language-locale for English in the EU. I was asked to re-label it from European Union to just Europe. The Iso code en-EU will remain as it is, but I need to change the English name and Native name attributes from English (European Union) to English (Europe). Can I do this without unregistering it and re-registering it? Will I lose content stored in this if I do have to unregister and re-register?


Solution

  • It looks like before you can register it again, it must have been unregistered (as Register throws an InvalidOperationException if the custom culture is already registered).

    However what you can do is initialize your CultureAndRegionInfoBuilder from the existing custom culture, then de-register it (at this point you don't need it anymore), and then make your changes and register it again.

    I.e. like this: (untested code)

      // Initalize the CultureAndRegionInfoBuilder with en-EU
      CultureAndRegionInfoBuilder car = new CultureAndRegionInfoBuilder("en-EU", 
                                             CultureAndRegionModifiers.None);
      car.LoadDataFromCultureInfo(CultureInfo.CreateSpecificCulture("en-EU"));
      car.LoadDataFromRegionInfo(new RegionInfo("en-EU"));
    
      // Unregister it
      CultureAndRegionInfoBuilder.Unregister("en-EU"); 
    
      // Update it
      car.CultureEnglishName = "English (European Union)";
      car.CultureNativeName = "English (Europe)";
    
      // Re-register it
      car.Register();