使用自定义区域性时 ResourceManager 未选择正确的资源集

发布于 2024-09-19 15:30:29 字数 2456 浏览 5 评论 0原文

我使用 this 上找到的代码创建了一个本地化的 MVC 网站亚历克斯·阿达米安 (Alex Adamyan) 的博客

如果我使用现有的文化,这会非常有效。但是,我正在尝试本地化他加禄语(tl 或 tl-PH)。 Windows 没有内置这种文化,所以我按照下面的代码创建了一个(我已经尝试了 tl 和 tl-PH):

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

}

我的测试站点上还有四个资源文件,位于 ~/Views/Home/Resources:

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

当我构建时,我在 bin 目录下得到三个适当命名的目录,每个目录都有一个适当命名的 dll。

因此,当我转到网站主页 http://localhost:1907 时,我会得到默认(英语)语言字符串。

当我转到德语(de)主页 http://localhost:1907/de 时,我得到了德语版本网站的。

当我转到他加禄语版本 http://localhost:1907/tlhttp://localhost:1907/tl-PH,我得到的是英文版本而不是他加禄语版本。

我已在资源获取代码中放置断点,并确认当前线程的区域性和 UI 区域性已正确设置为他加禄语区域性,并且塔加拉族语是传递给 resourceManager.GetString(key,culture) 的区域性。

有什么想法吗?我没有正确地创造我的文化吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

下雨或天晴 2024-09-26 15:30:29

我认为你们的文化从未被注册过,至少其中一种没有被注册。

下面的代码将引发异常,因为您无法为中性区域性指定区域值。就像您无法为中立区域性创建 DateTimeFormatInfo 对象一样。

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 cultureBuilder = new CultureAndRegionInfoBuilder(
                        "tl", CultureAndRegionModifiers.Neutral);

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

第二种特定文化似乎还可以。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文