ResourceManager not selecting correct resource set when using custom culture

有些话、适合烂在心里 提交于 2019-12-22 03:55:12

问题


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:


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.



来源:https://stackoverflow.com/questions/3743801/resourcemanager-not-selecting-correct-resource-set-when-using-custom-culture

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!