10

I want to create a resource file for Singaporean English (en-sg) named "shopping.en-sg.resx" in App_GlobalResources folder.

I get error during compilation.

Error 1 The namespace 'Resources' already contains a definition for 'shopping' c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\web\2cd6afe9\737b0a13\App_GlobalResources.vomuzavz.1.cs 26

After searching Google, I discover that "en-sg" is not a default culture and I have to create custom culture for it. I don't know the detailed steps of this.

What should I do to create the culture and remove the compilation error?

I follow the example in MSDN, create a file called "shopping.x-en-US-sample.resx" and put the following code into BasePage's function (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");

However, compilation error is still exist.

UPDATED:

You can easily reproduce the problem by creating an empty website and two files "shopping.en-sg.resx" and "shopping.resx" in the app_globalresources folder.

Billy
  • 15,516
  • 28
  • 70
  • 101

3 Answers3

18

You can create a new culture based on an existing culture:

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();

Added: Just checked the references: I have :

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

Added (updated, based on comments):

With regards the error message:

The error you're seeing is the result of some resource naming conflict. Check the resource names, these get compiled into dlls to you need to check that the namespace names dont conflict. You can check this using the reflector tool: http://www.red-gate.com/products/reflector/

Mark Redman
  • 24,079
  • 20
  • 92
  • 147
  • This code only needs to be run once on the machine where you need the additional culture. Maybe you're getting the error becuase it already exists? – Mark Redman Aug 20 '09 at 07:46
  • Put the code in global.asax? If I change "en-sg" to "en-us", then that's fine. I just want a piece of code that I can simply put it in somewhere and after that I can use the shopping.en-sg.resx – Billy Aug 20 '09 at 07:59
  • Just updated the answer with some additiona info re the error message. – Mark Redman Aug 20 '09 at 08:02
  • "en-SG" is not a default valid culture in ASP.NET. – Billy Aug 20 '09 at 08:18
  • still searching Google for the compilation error, no freaking idea. http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/dc572e07-62e9-495f-a38d-c0df2cb7b58a - "bizbiz" has the same problem, but no solution is provided. – Billy Aug 20 '09 at 08:41
  • try removing the resource dlls relating to en-SG, if that runs it will suggest a naming conflict, then add back them one a time to see which one it is. – Mark Redman Aug 20 '09 at 09:05
  • You can easily reproduce the problem by creating an empty website and the shopping.en-sg.resx in the app_globalresources folder. I have already tried to remove all the dlls for testing. – Billy Aug 20 '09 at 09:26
  • 1
    I think it should be mentioned that the above code requires system administrative permissions to run. From the MSDN article: "A custom culture can be registered on a computer only by a user who has administrative rights on that computer. Consequently, apps typically do not create and install custom cultures. Instead, you can use the CultureAndRegionInfoBuilder class to create a special-purpose tool that an administrator can use to create, install, and register a custom culture." – DavKa Apr 13 '16 at 06:38
  • 1
    So, there is no way to create an instance of a custom culture, without registering it system-wide? This doesn't sound good for my ears... MS must make this more easy to use. – user2173353 Jun 27 '19 at 11:26
  • @user2173353 This answer was almost 10 years ago, please continue your research and see if there is a better more modern option, if/when you find one, you could add your own answer providing updated solution? – Mark Redman Jun 27 '19 at 15:51
3

Here are the steps and code required to create the en-sg culture.

  1. Create a console app.
  2. Add a reference to sysglobl (C:\Windows\Microsoft.NET\Framework\v2.0.50727\sysglobl.dll)
  3. Add the code below to it.
  4. Run it on the web server(s) and dev machine(s) as an Administrator.

It will create a culture based on what I found to be its closest match (en-au). I have then overridden names etc to make it unique.

You should only have to run this once. It removes any existing ones before creating this just in case you wish to make any modifications after running it.

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();
    }
m1dst
  • 333
  • 2
  • 9
1

How to: Create Custom Cultures (from MSDN)

Community
  • 1
  • 1
Fredrik Mörk
  • 155,851
  • 29
  • 291
  • 343