欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

ASP.NETCoreWebAPI如何實(shí)現(xiàn)本地化-創(chuàng)新互聯(lián)

本篇內(nèi)容主要講解“ASP.NET Core WebAPI如何實(shí)現(xiàn)本地化”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“ASP.NET Core WebAPI如何實(shí)現(xiàn)本地化”吧!

10年積累的做網(wǎng)站、成都網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站策劃后付款的網(wǎng)站建設(shè)流程,更有秭歸免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

在StartupConfigureServices 注冊本地化所需要的服務(wù)AddLocalizationConfigure<RequestLocalizationOptions>

 public void ConfigureServices(IServiceCollection services)
  {
   services.AddLocalization();
   services.Configure<RequestLocalizationOptions>(options =>
   {
    var supportedCultures = new List<CultureInfo>
    {
     new CultureInfo("en-us"),
     new CultureInfo("zh-cn")
    };

    options.DefaultRequestCulture = new RequestCulture(culture: "en-us", uiCulture: "en-us");
    options.SupportedCultures = supportedCultures;
    options.SupportedUICultures = supportedCultures;
    options.RequestCultureProviders = new IRequestCultureProvider[] { new RouteDataRequestCultureProvider { IndexOfCulture = 1, IndexofUiCulture = 1 } };
   });
   services.Configure<RouteOptions>(options =>
   {
    options.ConstraintMap.Add("culture", typeof(LanguageRouteConstraint));
   });
   services.AddControllers();
  }

在Startup.cs類的Configure 方法中添加請求本地化中間件。

 var localizeOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
   app.UseRequestLocalization(localizeOptions.Value);

RequestCultureProvider 它使用簡單的委托來確定當(dāng)前的本地化區(qū)域性,當(dāng)然我們還可以通過RequestCultureProvider自定義源的請求區(qū)域信息比如說配置文件或者數(shù)據(jù)庫都是可以的.或者說我們可以選用默認(rèn)的一些方式讓我們?nèi)カ@取到當(dāng)前區(qū)域.

ASP.NET Core 本地化默認(rèn)向我們提供了四個(gè)方式,可用于確定正在執(zhí)行的請求的當(dāng)前區(qū)域性:

  • QueryStringRequestCultureProvider

  • CookieRequestCultureProvider

  • AcceptLanguageHeaderRequestCultureProvider

  • CustomRequestCultureProvider

如下所示我將通過路由的方式,去確定當(dāng)前區(qū)域

public class RouteDataRequestCultureProvider : RequestCultureProvider
 {
  public int IndexOfCulture;
  public int IndexofUiCulture;

  public override Task<ProviderCultureResult> DetermineProviderCultureResult(HttpContext httpContext)
  {
   if (httpContext == null)
    throw new ArgumentNullException(nameof(httpContext));
   string uiCulture;

   string culture = uiCulture = httpContext.Request.Path.Value.Split('/')[IndexOfCulture];

   var providerResultCulture = new ProviderCultureResult(culture, uiCulture);

   return Task.FromResult(providerResultCulture);
  }
 }

通過如下代碼片段實(shí)現(xiàn)IRouteConstraint對路由做相應(yīng)的約束

 public class LanguageRouteConstraint : IRouteConstraint
 {
  public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
  {

   if (!values.ContainsKey("culture"))
    return false;

   var culture = values["culture"].ToString();
   return culture == "en-us" || culture == "zh-cn";
  }
 }

添加區(qū)域資源文件

ASP.NET Core WebAPI如何實(shí)現(xiàn)本地化

注入IStringLocalizer<T>StringLocalizer將通過傳遞的共享資源(T)的值映射到資源文件,然后將本地化根據(jù)字符串的名稱從資源文件返回響應(yīng)的值

 [Route("{culture:culture}/[controller]")]
 [ApiController]
 public class HomeController : ControllerBase
 {
  private readonly IStringLocalizer<Resource> localizer;
  public HomeController(IStringLocalizer<Resource> localizer)
  {
   this.localizer = localizer;
  }
  public string Get()
  {
   return localizer["Home"];
  }
 }

如下圖所示


ASP.NET Core WebAPI如何實(shí)現(xiàn)本地化

ASP.NET Core WebAPI如何實(shí)現(xiàn)本地化

到此,相信大家對“ASP.NET Core WebAPI如何實(shí)現(xiàn)本地化”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)建站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

分享文章:ASP.NETCoreWebAPI如何實(shí)現(xiàn)本地化-創(chuàng)新互聯(lián)
本文地址:http://chinadenli.net/article8/edpip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)網(wǎng)站制作軟件開發(fā)動(dòng)態(tài)網(wǎng)站定制網(wǎng)站響應(yīng)式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)