【问题标题】:CultureNotFoundException: Culture is not supported after upgrading to .net 4CultureNotFoundException:升级到 .net 4 后不支持文化
【发布时间】:2011-02-23 18:04:17
【问题描述】:

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

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

在我的本地 PC 上,这可以正常工作。 但是在网络服务器上,当我去设置文化时,我得到了不支持文化的错误? 我检查了 Windows\Globalization 文件夹,那里有一个文化文件,用于正确的文化?

相同的代码在 3.5 下运行良好?

【问题讨论】:

  • 大声谢谢!任何有用的建议:)?它阻止我们切换到自定义文化,这意味着我们无法升级到 .Net 4.0,因为我们有一半的客户群使用自定义文化,因此我们可以从 resx 文件中呈现不同的字符串

标签: c#-4.0 globalization cultureinfo


【解决方案1】:

万一其他人遇到这个问题,我必须编写一个命令行工具来取消注册文化,然后重新注册。

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

【讨论】:

    【解决方案2】:

    如果运行良好,我遵循了如何在本地机器上添加自定义文化的众多指南。但是,我在开发服务器上的 CultureInfo 构造函数上遇到了 CultureNotFoundException,但名称上有错误。 nlp 文件已正确创建,我怀疑注册表步骤是问题所在,但我找不到任何有关它的资源。此外,它还破坏了机器上所有其他应用程序的任何其他语言映射。

    最终,我遵循了构建全局 Windows 和 Web 应用程序的开发人员指南:第 11 章 - codeproject 上的自定义文化,并用所需的修改替换了现有的文化,这可以正常工作。

    我仍然想知道为什么我原来的自定义文化不起作用,而替代品却起作用了。无论如何,我也想分享我的经验。

    【讨论】:

    • 你能把链接发到 CodeProject 的页面吗?谢谢
    猜你喜欢
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 2020-03-09
    • 2018-01-19
    • 2021-04-23
    • 1970-01-01
    相关资源
    最近更新 更多