受欢迎的博客标签

step 1: view

src\Presentation\Nop.Web\Views\Shared\Components\LanguageSelector\Default.cshtml

<div title="@Localizer["Request culture provider:"] @requestCulture?.Provider?.GetType().Name">
    <form id="selectLanguage" asp-controller="common"
          asp-action="SetLanguage" asp-route-returnUrl="@returnUrl"
          method="get" class="form-horizontal" role="form">
        <input name="returnUrl" type="hidden" value="@returnUrl">
        <select name="culture" class="form-control" onchange="this.form.submit();"
                asp-for="@requestCulture.RequestCulture.UICulture.Name" asp-items="cultureItems"></select>
    </form>
</div>

step 2: Controller

\src\Presentation\Nop.Web\Controllers\CommonController.cs

  public virtual IActionResult SetLanguage(string culture, string returnUrl = "")
        {
            
            var language = _languageService.GetLanguageByUniqueSeoCode(culture);
#if DEBUG
            this._logger.LogInformation($"SetLanguage:{culture}");
            this._logger.LogInformation($" CookieRequestCultureProvider.DefaultCookieName:{ CookieRequestCultureProvider.DefaultCookieName}");
#endif

            Response.Cookies.Append(
                 CookieRequestCultureProvider.DefaultCookieName,
                 CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
                 new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
            );

            
           

           
           

            _workContext.WorkingLanguage = language;

           // return LocalRedirect(returnUrl);
            return Redirect(Uri.EscapeUriString(returnUrl));

           
        }

 

 

 

1.

 here is the order in which the correct culture gets determined (in order of priority):

  1. From the query string
  2. From a cookie
  3. From the Accept-Language HTTP header
  4. From the DefaultRequestCulture property of the RequestLocalizationOptions class
  5. From the thread culture

Now we able to detect language by providers already exists. To use them just add the providers to RequestCultureProviders

  1. QueryStringRequestCultureProvider
  2. CookieRequestCultureProvider
  3. AcceptLanguageHeaderRequestCultureProvider
  4. CustomRequestCultureProvider