ResourceManager not selecting correct resource set when using custom culture
Asked Answered
K

1

10

I have created a localized MVC website using the code found on this blog by Alex Adamyan.

This is working great if I use an existing culture. However, I am trying to localize for Tagalog (tl or tl-PH). Windows does not have this culture built in so I have created one (I have tried both tl and tl-PH) as per the code below:

public static void CreateCustomCultures()

{

    var cultureBuilder = new CultureAndRegionInfoBuilder(
                            "tl", CultureAndRegionModifiers.Neutral);

    cultureBuilder.LoadDataFromCultureInfo(new CultureInfo("en-US"));
    cultureBuilder.LoadDataFromRegionInfo(new RegionInfo("US"));
    cultureBuilder.IsMetric = true;
    cultureBuilder.CultureEnglishName = "Tagalog";
    cultureBuilder.CultureNativeName = "Tagalog";
    cultureBuilder.RegionEnglishName = "Tagalog";
    cultureBuilder.RegionNativeName = "Tagalog";
    cultureBuilder.TwoLetterISOLanguageName = "tl";
    cultureBuilder.ThreeLetterISORegionName = "PH";
    cultureBuilder.Register();

    var cultureBuilder2 = new CultureAndRegionInfoBuilder(
                            "tl-PH", CultureAndRegionModifiers.None);

    cultureBuilder2.LoadDataFromCultureInfo(new CultureInfo("en-US"));
    cultureBuilder2.LoadDataFromRegionInfo(new RegionInfo("US"));
    cultureBuilder2.IsMetric = true;
    cultureBuilder2.CultureEnglishName = "Tagalog";
    cultureBuilder2.CultureNativeName = "Tagalog";
    cultureBuilder2.RegionEnglishName = "Tagalog";
    cultureBuilder2.RegionNativeName = "Tagalog";
    cultureBuilder2.TwoLetterISOLanguageName = "tl";
    cultureBuilder2.ThreeLetterISORegionName = "PH";
    cultureBuilder2.Register();

}

I also have four resource files on my test site located in ~/Views/Home/Resources:

  • Home.aspx.resx;
  • Home.aspx.tl.resx
  • Home.aspx.tl-PH.resx
  • Home.aspx.de.resx

When I build, I get three appropriately named directories under my bin directory, each with a an appropriately named dll.

So when I go to my website home page http://localhost:1907 I get the default (english) language strings.

When I go to the german (de) home page http://localhost:1907/de I get the german version of the site.

When I go to the tagalog versions http://localhost:1907/tl or http://localhost:1907/tl-PH, I get the english version instead of the Tagalog version.

I have placed breakpoints in the resource fetching code and have confirmed that the current thread's culture and UI culture are correctly set to the Tagalog culture and that Tagalog is the culture being passed to resourceManager.GetString(key, culture).

Any thoughts? Did I not create my cultures correctly?

Korrie answered 18/9, 2010 at 22:40 Comment(5)
cultureBuilder2.LoadDataFromCultureInfo(new CultureInfo("en-US")); cultureBuilder2.LoadDataFromRegionInfo(new RegionInfo("US")); en-US ?Fluidextract
@Avatar: It just loads settings (like decimal and grouping separators and/or formats) from US English culture. It should not have any effect on loading appropriate resources...Luciferous
I am pretty sure that you have created your custom culture correctly. The only concern I have is your three letter ISO 3166 country code, which apparently is just two letters. However it should not matter.Luciferous
Are you creating your custom cultures in the same application that is using them? I ask because I had problems with my custom cultures not being recognised, but restarting visual studio after creating them fixed the issue. You should probably have a separate application / deployment process that registers the cultures as administrator access is required.Arbitrage
Try using .Net Reflector to open up the dlls and make sure that the data is actually being placed in them correctly. It's possible that for some reason you're actually loading english into them. red-gate.com/products/dotnet-development/reflectorMasseter
S
2

I think your cultures are never registered, at least one of them isn't.

The code below will throw an exception since you cannot specify regional values for a neutral culture. Just like you cannot create a DateTimeFormatInfo object for a neutral culture.

var cultureBuilder = new CultureAndRegionInfoBuilder(
                        "tl", CultureAndRegionModifiers.Neutral);

cultureBuilder.LoadDataFromCultureInfo(new CultureInfo("en-US"));
cultureBuilder.LoadDataFromRegionInfo(new RegionInfo("US"));
cultureBuilder.IsMetric = true;
cultureBuilder.CultureEnglishName = "Tagalog";
cultureBuilder.CultureNativeName = "Tagalog";
cultureBuilder.RegionEnglishName = "Tagalog";
cultureBuilder.RegionNativeName = "Tagalog";
cultureBuilder.TwoLetterISOLanguageName = "tl";
cultureBuilder.ThreeLetterISORegionName = "PH";
cultureBuilder.Register();

It should be something like this

var cultureBuilder = new CultureAndRegionInfoBuilder(
                        "tl", CultureAndRegionModifiers.Neutral);

cultureBuilder.LoadDataFromCultureInfo(new CultureInfo("en-US"));
cultureBuilder.CultureEnglishName = "Tagalog";
cultureBuilder.CultureNativeName = "Tagalog";
cultureBuilder.TwoLetterISOLanguageName = "tl";
cultureBuilder.Register();

The second specific culture seems alright.

Syllabary answered 19/12, 2010 at 21:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.