在Startup ConfigureServices 注册本地化所需要的服务AddLocalization和 Configure<RequestLocalizationO
在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"];
}
}
如下图所示
Reference
https://GitHub.com/hueifeng/BlogSample/tree/master/src/LocalizationSingleResx
到此这篇关于ASP.net core webapi实现本地化(单资源文件)的文章就介绍到这了,更多相关ASP.Net Core WEBAPI本地化内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
您可能感兴趣的文章:asp.net Core自定义本地化教程之从文本文件读取本地化字符串
--结束END--
本文标题: ASP.NET Core WebAPI实现本地化(单资源文件)
本文链接: https://lsjlt.com/news/30768.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2023-05-21
2023-05-21
2023-05-21
2023-05-21
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0