我们使用CultureInfo(customCulture)将文化设置为自定义文化

为了以防万一,我们在app_start中的global.cs中创建customCulture。

在我的本地PC上可以正常工作。
但是,在Web服务器上,当我设置区域性时,会收到错误消息,提示不支持区域性?
我已经检查了Windows \ Globalization文件夹,那里有一个用于正确区域性的区域性文件?

相同的代码在3.5下可以正常工作吗?

最佳答案

万一其他人遇到此问题,我必须编写一个命令行工具来注销区域性,然后重新进行注册。

        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");
            }
        }
    }

关于c#-4.0 - CultureNotFoundException:升级到.net 4后不支持文化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5094973/

10-13 06:46