VVulph
VVulph

Reputation: 29

How do we map a .NET CustomCulture to a resource files

I created a class library project and added a customr culture based on http://msdn.microsoft.com/en-us/library/ms172469(v=vs.90).aspx

The class library project is referenced by a simple asp.net webproject, where I simply display a string message based on the current culture.

I added a resource file 'messages.x-en-US-sample.resx' besides the messages.es-ES.resx. When I set my ui culture and culture (currentThread) to x-en-US-sample, the resource strings do not automatically map to this file, as they do when i use e.g. es-ES culture which automatically picks up messages.es-ES.resx.

What am i missing here ?

Upvotes: 2

Views: 1579

Answers (2)

Late Starter
Late Starter

Reputation: 1109

The following is a simple test I put together. As long as your resource files have the extensions shown, this will work.

   // Resource file names:
    // XXX.resx (default)
    // XXX.de-DE.resx (German)
    // XXX.en-Hobbit.resx (custom) 

    static void Main()
    {
        BuildAndRegisterHobbitCulture();

        const string goodDay = "GoodDay";

        // Default.
        Console.WriteLine("{0}: {1}", Thread.CurrentThread.CurrentCulture.Name, 
            Resources.ResourceManager.GetString(goodDay));

        // German.
        SetCulture("de-DE");
        Console.WriteLine("{0}: {1}", Thread.CurrentThread.CurrentCulture.Name, 
            Resources.ResourceManager.GetString(goodDay));

        // Custom - Hobbit.
        SetCulture("en-Hobbit");
        Console.WriteLine("{0}: {1}", Thread.CurrentThread.CurrentCulture.Name, 
            Resources.ResourceManager.GetString(goodDay));

        Console.ReadLine();
    }

    private static void SetCulture(string culture)
    {
        var cultureInfo = CultureInfo.GetCultureInfo(culture);
        Thread.CurrentThread.CurrentCulture = cultureInfo;
        Thread.CurrentThread.CurrentUICulture = cultureInfo;
    }

    private static void BuildAndRegisterHobbitCulture()
    {
        if (CultureInfo.GetCultures(CultureTypes.AllCultures).Any(ci => ci.Name == "en-Hobbit"))
            return;

        var cultureAndRegionInfoBuilder = new CultureAndRegionInfoBuilder("en-Hobbit", CultureAndRegionModifiers.None);
        cultureAndRegionInfoBuilder.LoadDataFromCultureInfo(CultureInfo.CreateSpecificCulture("en-GB"));
        cultureAndRegionInfoBuilder.LoadDataFromRegionInfo(new RegionInfo("en-GB"));
        cultureAndRegionInfoBuilder.Register();
    }

Upvotes: 2

Carlos Landeras
Carlos Landeras

Reputation: 11063

Why you set x-en-US culture?

Set is to:

Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US");

using the resource file:

MyResourceManager.yourcountry-en-US.resx

I think x-en-US is not a valid CultureInfo code, should be en-US

You have here a microsoft example where you can register custom cultures and assign them:

public static void Main()
   {
      // Create a custom culture for ru-US.
      CultureAndRegionInfoBuilder car1 = new CultureAndRegionInfoBuilder("ru-US", 
                                             CultureAndRegionModifiers.None);
      car1.LoadDataFromCultureInfo(CultureInfo.CreateSpecificCulture("ru-RU"));
      car1.LoadDataFromRegionInfo(new RegionInfo("en-US"));

      car1.CultureEnglishName = "Russian (United States)";
      car1.CultureNativeName = "русский (США)";
      car1.CurrencyNativeName = "Доллар (США)";
      car1.RegionNativeName = "США";

      // Register the culture. 
      try {
         car1.Register();
      }    
      catch (InvalidOperationException) {
         // Swallow the exception: the culture already is registered.
      }

      // Use the custom culture.
      CultureInfo ci = CultureInfo.CreateSpecificCulture("ru-US");
      Thread.CurrentThread.CurrentCulture = ci;
      Console.WriteLine("Current Culture: {0}", 
                        Thread.CurrentThread.CurrentCulture.Name);
      Console.WriteLine("Writing System: {0}", 
                        Thread.CurrentThread.CurrentCulture.TextInfo);
   }

Upvotes: 0

Related Questions