ASP.NET Core本地化-不从资源文件返回值,仅返回名称 [英] ASP.NET Core Localization - Doesn't return Value from resource file, only Name

查看:106
本文介绍了ASP.NET Core本地化-不从资源文件返回值,仅返回名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现本地化,但是当运行时,只有Name返回.

I'm trying to implement Localization, but when run only the Name returns.

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 的视图,位于"Views \ Home"中. Resources文件夹包含两个资源文件: Views.Home.Index.en.resx Views.Home.Index.no.resx

The project contains one View named Index.cshtml, located in "Views\Home". The Resources folder contains two resource files: Views.Home.Index.en.resx and Views.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 [关于"] 返回关于"

@ requestCulture.RequestCulture.Culture.Name 返回否"

推荐答案

是否可以添加 Localization.AspNetCore.TagHelpers nuget程序包.添加完之后,我开始进行本地化工作.

Could you add Localization.AspNetCore.TagHelpers nuget package. After I added this I got localization working.

另请参阅:在ASP.Net核心MVC中的本地化无法正常工作-找不到具有类似问题的资源文件.

这篇关于ASP.NET Core本地化-不从资源文件返回值,仅返回名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆