Bernard
Bernard

Reputation: 2043

CultureNotFoundException: Culture is not supported after upgrading to .net 4

We set the culture to a custom culture using CultureInfo(customCulture)

We create the customCulture in the global.cs in app_start just in case.

On my local PC this works fine. However on the web server, when I go to set the culture, I get the error that the culture is not supported? I've checked the Windows\Globalization folder and there is a culture file there for the correct culture?

The same code worked fine under 3.5?

Upvotes: 3

Views: 3458

Answers (2)

Guy Senerman
Guy Senerman

Reputation: 31

I followed the numerous guides of how to add a custom culture and on local machine if worked perfectly. However, I got CultureNotFoundException on the CultureInfo constructor on a development server with error on the name. The nlp file was created correctly and I suspect the registry step was the issue, but I couldn't find any resource about it. More over, it corrupted any other language mapping on the machine for all other applications.

Eventually I followed the The Developer's Guide to Building Global Windows and Web Applications: Chapter 11 - Custom Cultures on codeproject and replaced an existing culture with the needed modifications, which works correctly.

Still I wonder why my original custom culture didn't work and the replacement did. In any case I also wanted to share my experience.

Upvotes: 1

Bernard
Bernard

Reputation: 2043

In case anyone else gets this issue, I had to write a command line tool to deregister the culture and then re-register it.

        using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Globalization;

    namespace InstallCulture
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Is CustomCulture already installed?
                string parentCultureName = "en-GB";
                string extendedName = "custom";
                System.Globalization.CultureInfo[] userCultures = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.UserCustomCulture);
                Console.WriteLine(string.Format("number of user cultures:{0}", userCultures.Count()));
                Console.WriteLine(string.Format("Setting culture {0}-{1}", parentCultureName, extendedName));
                // Install CustomCulture based on language parent.
                System.Globalization.CultureAndRegionInfoBuilder builder = new System.Globalization.CultureAndRegionInfoBuilder(
                        string.Format("{0}-{1}", parentCultureName, extendedName),
                        System.Globalization.CultureAndRegionModifiers.None);

                builder.LoadDataFromCultureInfo(new System.Globalization.CultureInfo("en-GB"));
                builder.LoadDataFromRegionInfo(new System.Globalization.RegionInfo("GB"));
                Console.WriteLine("CultureName:. . . . . . . . . . {0}", builder.CultureName);
                Console.WriteLine("CultureEnglishName: . . . . . . {0}", builder.CultureEnglishName);
                Console.WriteLine("CultureNativeName:. . . . . . . {0}", builder.CultureNativeName);

                foreach(System.Globalization.CultureInfo cultInfo in userCultures)
                {
                    Console.WriteLine("Found culture in userCultures");
                    if(cultInfo.Name.ToLower() == string.Format("{0}-{1}", parentCultureName, extendedName).ToLower())
                    {
                        Console.WriteLine(string.Format("{0} already registered", cultInfo.Name));
                        CultureAndRegionInfoBuilder.Unregister("en-GB-custom");
                        Console.WriteLine("Unregistered culture en-GB-custom");
                    }
                }

                Console.WriteLine("Registering culture en-GB-PolHol");
                builder.Register();
                Console.WriteLine("Create new culture info object");
                System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-GB-custom");
                Console.WriteLine("Name: . . . . . . . . . . . . . {0}", culture.Name);
                Console.WriteLine("EnglishName:. . . . . . . . . . {0}", culture.EnglishName);
                Console.WriteLine("NativeName: . . . . . . . . . . {0}", culture.NativeName);
                Console.WriteLine("Set threads to new culture");
                System.Threading.Thread.CurrentThread.CurrentCulture = culture;
                System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
                Console.WriteLine("Set culture done");
            }
        }
    }

Upvotes: 2

Related Questions