【问题标题】:Localization ASP.NET Core 2.2 not working for viewmodels and views本地化 ASP.NET Core 2.2 不适用于视图模型和视图
【发布时间】:2019-04-03 15:08:06
【问题描述】:

我正在尝试本地化我的 asp.net-core 2.2 应用程序。我已经尝试了很多教程/博客,甚至还有一个示例项目来展示如何做到这一点。我完全匹配我的应用程序,但我从来没有从基于“键”打印的资源文件中获得“值”。

这是startup.cs:

public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        #region snippet1
        services.AddLocalization(options => options.ResourcesPath = "Resources");

        services.AddMvc()
            .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
            .AddDataAnnotationsLocalization();
        #endregion

        services.Configure<RequestLocalizationOptions>(options =>
        {
            var supportedCultures = new List<CultureInfo>
                {
                    new CultureInfo("en-US"),
                    new CultureInfo("nl-NL")
                };

            options.DefaultRequestCulture = new RequestCulture("en");
            options.SupportedCultures = supportedCultures;
            options.SupportedUICultures = supportedCultures;
        });

        //services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        #region snippet2
        var supportedCultures = new[]
        {
            new CultureInfo("en-US"),
            new CultureInfo("nl-NL")
        };

        app.UseRequestLocalization(new RequestLocalizationOptions
        {
            DefaultRequestCulture = new RequestCulture("en"),
            // Formatting numbers, dates, etc.
            SupportedCultures = supportedCultures,
            // UI strings that we have localized.
            SupportedUICultures = supportedCultures
        });

        app.UseStaticFiles();
        // To configure external authentication, 
        // see: http://go.microsoft.com/fwlink/?LinkID=532715
        app.UseAuthentication();
        app.UseMvcWithDefaultRoute();
        #endregion

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

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

家庭控制器:

public class HomeController : Controller
{
    private readonly IStringLocalizer<HomeController> _localizer;

    public HomeController(IStringLocalizer<HomeController> localizer)
    {
        _localizer = localizer;
    }

    public IActionResult Index()
    {
        return View();
    }

    public IActionResult Test()
    {
        var ttt = new TestViewModel();
        ttt.Name = "bbbb";
        ttt.Type = "ggggg";

        return View(ttt);
    }

    public IActionResult Privacy()
    {
        return View();
    }

    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
}

观点:

@using Microsoft.AspNetCore.Mvc.Localization


@inject IViewLocalizer Localizer
@inject IHtmlLocalizer<LocalizationTest.Resources.SharedResource> SharedLocalizer

@model LocalizationTest.ViewModels.TestViewModel


<h1>Test</h1>

<div>@Localizer["Name"]</div>
<div>@Localizer["Type"]</div>

<br />

CurrentCulture: @System.Globalization.CultureInfo.CurrentCulture.ToString()


<form asp-controller="Home" asp-action="Test" method="get" class="form-horizontal" role="form">
    <hr>
    <div asp-validation-summary="All" class="text-danger"></div>
    <div class="form-group">
        <label asp-for="Name" class="col-md-2 control-label"></label>
    </div>
    <div class="form-group">
        <label asp-for="Type" class="col-md-2 control-label"></label>
    </div>

</form>

这是文件夹结构:

【问题讨论】:

  • 你得到这个工作了吗?我已经按照建议安装了标签助手库,但仍然不高兴我希望你得到这个排序你的问题是什么?

标签: c# localization asp.net-core-2.2


【解决方案1】:

您错过了控制器名称。

  1. 将您的 TestViewModel.cs 文件放入 ViewModels/Home/ 文件夹。
  2. 重命名ViewModels.TestViewModel.resx -> ViewModels.Home.TestViewModel.resx

【讨论】:

    猜你喜欢
    • 2019-01-28
    • 1970-01-01
    • 2023-04-06
    • 2017-09-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    相关资源
    最近更新 更多