c# - ASP.NET Core 本地化 - 不从资源文件返回值,仅返回名称

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

我正在尝试实现本地化,但运行时仅返回名称。

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddLocalization(options => options.ResourcesPath = "Resources");
    services.AddMvc()
        .AddViewLocalization()
        .AddDataAnnotationsLocalization();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    .....

    List<CultureInfo> supportedCultures = new List<CultureInfo>
    {
        new CultureInfo("no"),
        new CultureInfo("en")
    };

    app.UseRequestLocalization(new RequestLocalizationOptions
    {
        DefaultRequestCulture = new RequestCulture("no"),
        SupportedCultures = supportedCultures,
        SupportedUICultures = supportedCultures
    });

    app.UseStaticFiles();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

该项目包含一个名为 Index.cshtml 的 View ,位于“Views\Home”中。 Resources 文件夹包含两个资源文件: Views.Home.Index.en.resxViews.Home.Index.no.resx

Index.cshtml:

@inject IOptions<RequestLocalizationOptions> LocalizerOptions
@inject IViewLocalizer Localizer

@{
    var requestCulture = Context.Features.Get<IRequestCultureFeature>();
    var cultureItems = LocalizerOptions.Value.SupportedUICultures
        .Select(c => new SelectListItem { Value = c.Name, Text = c.DisplayName })
        .ToList();
}

@{
    ViewData["Title"] = Localizer["Title"];
}

<div id="section_box_about" class="section_box_about" style="margin-top:0px;">
    @Localizer["HeaderAbout"]
</div>

<div class="container" style="width:96%;margin:auto;">
    @Localizer["About"]

    <br />
    <br />
    <br />
    @requestCulture.RequestCulture.Culture.Name
</div>

@Localizer["About"] 返回“关于”

@requestCulture.RequestCulture.Culture.Name 返回“否”

最佳答案

您能否添加 Localization.AspNetCore.TagHelpers nuget 包。添加此内容后,我进行了本地化工作。

另请参阅:Localization in ASP.Net core MVC not working - unable to locate resource file有类似的问题。

关于c# - ASP.NET Core 本地化 - 不从资源文件返回值,仅返回名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43485192/

相关文章:

c# - Is Equals(Object,Guid.Empty) 与 Object == Guid.Empty 相同

c# - 如何使用MVVM从Restful Service向Xamarin.Forms View 显示图像

iis - URL 重定向失败

c# - PartialViewResult Form 将不会清除 ajax 结果上的值 - ASP.NET Core Razor c#

asp.net - 我迷路了。 ASP.NET MVC 5发生了什么?

c# - 支持编译依赖的多个版本 (vNext)

c# - 调试 Windows 服务 - 防止无法启动服务

c# - 强制循环迭代

c# - 以编程方式注入(inject)依赖asp.net core

asp.net-core - 无法加载类型 'Microsoft.EntityFrameworkCore.Infrastructure.DesignTimeProviderServicesAttribute'