欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET Core WebAPI实现本地化(单资源文件)

程序员文章站 2022-06-17 14:33:26
在startup configureservices 注册本地化所需要的服务addlocalization和 configure p...

在startup configureservices 注册本地化所需要的服务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 它使用简单的委托来确定当前的本地化区域性,当然我们还可以通过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";
  }
 }

添加区域资源文件

ASP.NET Core WebAPI实现本地化(单资源文件)

注入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实现本地化(单资源文件)

ASP.NET Core WebAPI实现本地化(单资源文件)

reference

https://github.com/hueifeng/blogsample/tree/master/src/localizationsingleresx

到此这篇关于asp.net core webapi实现本地化(单资源文件)的文章就介绍到这了,更多相关asp.net core webapi本地化内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!