【问题标题】:ASP.Net Core 2.1 MVC Localization culture fallbackASP.Net Core 2.1 MVC 本地化文化回退
【发布时间】:2019-02-22 14:24:39
【问题描述】:

我有一个关于 ASP.Net Core 2.1 MVC 应用程序本地化的问题。

因此,在我的创业公司中,我拥有三种受支持的文化:

services.Configure<RequestLocalizationOptions>(options =>
        {
            var supportedCultures = new[]
            {
                new CultureInfo("fr-CH"),
                new CultureInfo("en-GB"),
                new CultureInfo("de-DE")
            };

            options.DefaultRequestCulture = new RequestCulture(culture: "fr-CH", uiCulture: "fr-CH");
            options.SupportedCultures = supportedCultures;
            options.SupportedUICultures = supportedCultures;
            options.RequestCultureProviders.Insert(0, new CustomerCultureProvider()); // Insert on top of the list so it's the first one to be executed
            options.RequestCultureProviders.Insert(1, new WebAuthenticationBrokerCultureProvider()); // Insert as second of the list so it's the second one to be executed
        });

我的资源文件名为“SharedView.en.resx”、“SharedView.fr.resx”和“SharedView.de.resx”。

所以在阅读了documentation中的这一部分之后:

"When searching for a resource, localization engages in "culture fallback". Starting from the requested culture, if not found, it reverts to the parent culture of that culture. As an aside, the CultureInfo.Parent property represents the parent culture. This usually (but not always) means removing the national signifier from the ISO. For example, the dialect of Spanish spoken in Mexico is "es-MX". It has the parent "es"—Spanish non-specific to any country.

Imagine your site receives a request for a "Welcome" resource using culture "fr-CA". The localization system looks for the following resources, in order, and selects the first match:

Welcome.fr-CA.resx
Welcome.fr.resx
Welcome.resx (if the NeutralResourcesLanguage is "fr-CA")"

我在想,当我提出像 https://localhost:xxxxx/Account/Login?cultur=en-US 这样的请求时,我会得到英文翻译,但它不起作用并退回到 fr-CH。

如果我使用https://localhost:xxxxx/Account/Login?cultur=en-GB,它就可以工作,而且我的显示器是英文的。

所以我看不到我缺少什么,我是否以错误的方式理解文档?

提前感谢您的帮助

【问题讨论】:

    标签: c# .net asp.net-core asp.net-core-mvc-2.1


    【解决方案1】:

    如果未找到,则恢复为该文化的父文化

    en-GB 不是 en-US 的父文化,这就是它回退到默认文化 fr-CH 的原因.

    当所选的文化是 en-US 而您在受支持的文化中没有它时,它将恢复为父文化,即 en 这意味着所请求的资源将是 welcome.en.resx

    en文化也一样,如果没有定义,选择的资源会是welcome.resx

    最后,如果没有找到资源,将选择默认定义的文化。

    同时,每种文化都有一个父文化,您可以通过 CultureInfo.Parent 找到它

    在此处查找更多信息:The resource fallback process

    【讨论】:

    • 感谢您的回答,这似乎合乎逻辑,但是在搜索 en 时,它不应该与我的文件 'SharedView.en.resx' 匹配吗?
    • 是的,但您必须将en添加到支持的文化列表中
    猜你喜欢
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 1970-01-01
    • 2010-10-14
    相关资源
    最近更新 更多