ResourceManager not selecting correct resource set

2019-04-22 05:31发布

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?

1条回答
神经病院院长
2楼-- · 2019-04-22 05:40

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.

查看更多
登录 后发表回答