您好,登錄后才能下訂單哦!
本篇內容主要講解“ASP.NET Core WebAPI如何實現本地化”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“ASP.NET Core WebAPI如何實現本地化”吧!
在Startup ConfigureServices
注冊本地化所需要的服務AddLocalization
和 Configure<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
它使用簡單的委托來確定當前的本地化區域性,當然我們還可以通過RequestCultureProvider
自定義源的請求區域信息比如說配置文件或者數據庫都是可以的.或者說我們可以選用默認的一些方式讓我們去獲取到當前區域.
ASP.NET Core 本地化默認向我們提供了四個方式,可用于確定正在執行的請求的當前區域性:
QueryStringRequestCultureProvider
CookieRequestCultureProvider
AcceptLanguageHeaderRequestCultureProvider
CustomRequestCultureProvider
如下所示我將通過路由的方式,去確定當前區域
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); } }
通過如下代碼片段實現IRouteConstraint
對路由做相應的約束
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"; } }
添加區域資源文件
注入IStringLocalizer<T>
,StringLocalizer
將通過傳遞的共享資源(T)的值映射到資源文件,然后將本地化根據字符串的名稱從資源文件返回響應的值
[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如何實現本地化”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。