在 ASP.NET 核心中使用视图本地化



我想在使用区域 ASP.NET 核心应用程序中使用本地化。

我有一个部分视图 Areas\Admin\Views\People\GetPeopleStatistics.cshtml

在这里我想使用本地化:

...
@inject IViewLocalizer Localizer
<h3>@Localizer["People Statistics"]:</h3>
...

我为此创建了一个资源文件:Resources\Admin\Views\People\GetPeopleStatistics.en.resx

我在启动.cs中有以下配置:

services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
        services.AddMvc()
            .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix, opts => { opts.ResourcesPath = "Resources"; })
            .AddDataAnnotationsLocalization();
        services.AddAutoMapper();
        services.Configure<RequestLocalizationOptions>(
            opts =>
            {
                var supportedCultures = new List<CultureInfo>
                {
                    new CultureInfo("en"),
                    new CultureInfo("de")
                };
                opts.DefaultRequestCulture = new RequestCulture("en");
                // Formatting numbers, dates, etc.
                opts.SupportedCultures = supportedCultures;
                // UI strings that we have localized.
                opts.SupportedUICultures = supportedCultures;
            });

不幸的是,这行不通。应用程序不显示资源文件中的值。

我正在使用 Cookie 来存储区域性:

    [HttpPost]
    public async Task SetLanguage(string culture)
    {
        Response.Cookies.Append(
            CookieRequestCultureProvider.DefaultCookieName,
            CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
            new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
        );
    }

我还在Visual Studio中收到以下错误:

Custom tool PublicResXFileCodeGenerator failed to produce an output for input file 'ResourcesAdminViewsPeopleGetPeopleStatistics.en.resx' but did not log a specific error. WebApplication  D:SVN RepositoriesSRMStrunkPresentationLayerWebApplicationResourcesAdminViewsPeopleGetPeopleStatistics.en.resx    1   

我做错了什么?如何配置?

您可能需要尝试以下操作:右键单击您的解决方案,然后执行"清理解决方案"。当本地化不起作用时,这为我解决了问题,我 100% 确定一切都配置正确。

最新更新