在ASP.NET中创建自定义文化

时间:2009-08-20 07:24:00

标签: asp.net internationalization

我想在App_GlobalResources文件夹中为新加坡英语(en-sg)创建一个名为“shopping.en-sg.resx”的资源文件。

我在编译时遇到错误。

  

错误1命名空间'资源'   已经包含了一个定义   'shopping'c:\ WINDOWS \ Microsoft.NET \ Framework \ v2.0.50727 \ Temporary   ASP.NET   Files \ web \ 2cd6afe9 \ 737b0a13 \ App_GlobalResources.vomuzavz.1.cs 26

在搜索Google之后,我发现“en-sg”不是默认文化,我必须为它创建自定义文化。我不知道具体的步骤。

如何创建文化并删除编译错误?

我按照MSDN中的示例,创建一个名为“shopping.x-en-US-sample.resx”的文件,并将以下代码放入BasePage的函数中(protected override void InitializeCulture()):

CultureAndRegionInfoBuilder cib = null;

cib = new CultureAndRegionInfoBuilder(
  "x-en-US-sample", CultureAndRegionModifiers.None);

CultureInfo ci = new CultureInfo("en-US");
cib.LoadDataFromCultureInfo(ci);
RegionInfo ri = new RegionInfo("US");
cib.LoadDataFromRegionInfo(ri);

cib.Register();

ci = new CultureInfo("x-en-US-sample");

但是,编译错误仍然存​​在。

更新:

您可以通过在app_globalresources文件夹中创建一个空网站和两个文件“shopping.en-sg.resx”和“shopping.resx”轻松重现此问题。

3 个答案:

答案 0 :(得分:16)

您可以根据现有文化创建新文化:

string culture = "en-sg";
string name = "Singaporean English";

CultureInfo cultureInfo = new CultureInfo("en-GB");
RegionInfo regionInfo = new RegionInfo(cultureInfo.Name);

CultureAndRegionInfoBuilder cultureAndRegionInfoBuilder = new CultureAndRegionInfoBuilder(culture, CultureAndRegionModifiers.None);

cultureAndRegionInfoBuilder.LoadDataFromCultureInfo(cultureInfo);
cultureAndRegionInfoBuilder.LoadDataFromRegionInfo(regionInfo);

// Custom Changes
cultureAndRegionInfoBuilder.CultureEnglishName = name;
cultureAndRegionInfoBuilder.CultureNativeName = name;

cultureAndRegionInfoBuilder.Register();

<强>加了: 刚检查了参考文献: 我有:

using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;

已添加(已更新,基于评论):

关于错误消息:

您看到的错误是某些资源命名冲突的结果。检查资源名称,将这些编译成dll,你需要检查命名空间名称是否冲突。您可以使用反射器工具检查:http://www.red-gate.com/products/reflector/

答案 1 :(得分:3)

以下是创建en-sg文化所需的步骤和代码。

  1. 创建一个控制台应用程序。
  2. 添加对sysglobl的引用(C:\ Windows \ Microsoft.NET \ Framework \ v2.0.50727 \ sysglobl.dll)
  3. 将以下代码添加到其中。
  4. 以管理员身份在网络服务器和开发机器上运行它。
  5. 它将根据我发现的最接近的匹配(en-au)创建一种文化。然后我覆盖了名称等,使其独一无二。

    你应该只需要运行一次。它会在创建之前删除任何现有的,以防万一您希望在运行后进行任何修改。

    public static void Main()
        {
            CultureAndRegionInfoBuilder cib = null;
    
            try
            {
                Console.Clear();
                Console.WriteLine("Unregister the \"en-SG\" " + "custom culture if it already exists...");
                CultureAndRegionInfoBuilder.Unregister("en-SG");
                Console.WriteLine("The custom culture was unregistered successfully.");
            }
            catch (Exception e)
            {
                Console.WriteLine("Error while unregistering...");
                Console.WriteLine(e);
            }
    
            try
            {
                cib = new CultureAndRegionInfoBuilder("en-SG", CultureAndRegionModifiers.None);
    
                // Populate the new CultureAndRegionInfoBuilder object with culture information.
                CultureInfo ci = new CultureInfo("en-AU");
                cib.LoadDataFromCultureInfo(ci);
    
                // Populate the new CultureAndRegionInfoBuilder object with region information.
                RegionInfo ri = new RegionInfo("SG");
                cib.LoadDataFromRegionInfo(ri);
    
                cib.CultureEnglishName = "English (Singapore)";
                cib.CultureNativeName = "English (Singapore)";
                cib.IsMetric = true;
    
                // Display some of the properties of the CultureAndRegionInfoBuilder object.
                Console.WriteLine("CultureName:. . . . . . . . . . {0}", cib.CultureName);
                Console.WriteLine("CultureEnglishName: . . . . . . {0}", cib.CultureEnglishName);
                Console.WriteLine("CultureNativeName:. . . . . . . {0}", cib.CultureNativeName);
                Console.WriteLine("GeoId:. . . . . . . . . . . . . {0}", cib.GeoId);
                Console.WriteLine("IsMetric: . . . . . . . . . . . {0}", cib.IsMetric);
                Console.WriteLine("ISOCurrencySymbol:. . . . . . . {0}", cib.ISOCurrencySymbol);
                Console.WriteLine("RegionEnglishName:. . . . . . . {0}", cib.RegionEnglishName);
                Console.WriteLine("RegionName: . . . . . . . . . . {0}", cib.RegionName);
                Console.WriteLine("RegionNativeName: . . . . . . . {0}", cib.RegionNativeName);
                Console.WriteLine("ThreeLetterISOLanguageName: . . {0}", cib.ThreeLetterISOLanguageName);
                Console.WriteLine("ThreeLetterISORegionName: . . . {0}", cib.ThreeLetterISORegionName);
                Console.WriteLine("ThreeLetterWindowsLanguageName: {0}", cib.ThreeLetterWindowsLanguageName);
                Console.WriteLine("ThreeLetterWindowsRegionName: . {0}", cib.ThreeLetterWindowsRegionName);
                Console.WriteLine("TwoLetterISOLanguageName: . . . {0}", cib.TwoLetterISOLanguageName);
                Console.WriteLine("TwoLetterISORegionName: . . . . {0}", cib.TwoLetterISORegionName);
                Console.WriteLine();
    
                // Register the custom culture.
                Console.WriteLine("Register the custom culture...");
                cib.Register();
    
                // Display some of the properties of the custom culture.
                ci = new CultureInfo("en-SG");
    
                Console.WriteLine("Name: . . . . . . . . . . . . . {0}", ci.Name);
                Console.WriteLine("EnglishName:. . . . . . . . . . {0}", ci.EnglishName);
                Console.WriteLine("NativeName: . . . . . . . . . . {0}", ci.NativeName);
                Console.WriteLine("TwoLetterISOLanguageName: . . . {0}", ci.TwoLetterISOLanguageName);
                Console.WriteLine("ThreeLetterISOLanguageName: . . {0}", ci.ThreeLetterISOLanguageName);
                Console.WriteLine("ThreeLetterWindowsLanguageName: {0}", ci.ThreeLetterWindowsLanguageName);
    
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            Console.ReadKey();
        }
    

答案 2 :(得分:1)