Asp.Net Core 2多国语言

时间:2018-09-27 09:05:07

标签: asp.net razor asp.net-core

我做网站并进行本地化。 但是我有一个问题。

项目未显示在“选择”中

如何解决该错误?

请告诉我。

  

HomeController

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

        return LocalRedirect(returnUrl);
    }
  

Cshtml

@using Microsoft.AspNetCore.Builder
@using Microsoft.AspNetCore.Http.Features
@using Microsoft.AspNetCore.Localization
@using Microsoft.AspNetCore.Mvc.Localization
@using Microsoft.Extensions.Options

@inject IViewLocalizer Localizer
@inject IOptions<RequestLocalizationOptions> LocOptions

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

<form asp-controller="Home" asp-action="SetLanguage" style="margin-top:20px;"
      asp-route-returnUrl="@Context.Request.Path"
      method="post" class="form-horizontal">
    @Localizer["Language:"] <select name="culture" onchange="this.form.submit();"
                                    asp-for="@requestCulture.RequestCulture.UICulture.Name"
                                    asp-items="cultureItems"></select>
</form>

0 个答案:

没有答案