Asp.net core Globalization and localization of use 2.x / 3.x of (a) using the method

Because Api interface needs to return multiple languages, and therefore reference to a lot of online articles ,, some articles written by theory too, looks more strenuous afternoon to engage in an afternoon, summed up the experience ,,

When doing this function, the main reference of the two articles:

https://blog.johnwu.cc/article/ironman-day21-asp-net-core-localization.html

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.1

You can see the shining

 

There are two cases: one is the need to return to multi-language web project ,, one is dll, the need to implement a separate multi-language, such as plug-in

The premise of the project is necessary to introduce Microsoft.Extensions.Localization package

A .Web project

  If Asp.net core project created by default, as well as references Microsoft.Extensions.Localization complete package, so no additional introduction

  1. The location of the resource file problem

  Online tutorials, reference to a SharedResource usage, and is on the Resources folder, and today tried many times, found to be so used,

  For example, in a different area in:

  Create a Resources folder,

  Then add a class called SharedResource, content and consequently will not need an empty class only

  Add the corresponding language resource files, such as: SharedResource.en.resx or SharedResource.th.resx

  structure:

  

 

 

   SharedResource.cs :

  namespace ZKXT.Devices.Api.Areas.AppApi.Resources
  {
    public class SharedResource{}

  }

 

  2.Start.cs in:

  

public void ConfigureServices(IServiceCollection services)

  {

    services.Configure<RequestLocalizationOptions>(options =>
    {
      options.DefaultRequestCulture = new RequestCulture("zn-cn");  //默认的语言
    });

 

    services.AddLocalization(); //注册相应Service  

  }

 

  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

   {

    

    var support = new List<CultureInfo>()
    {
      new CultureInfo("CN-zh " ),   // registered in multiple languages, you can view specific HTTP: // www.lingoes.net/zh/translator/langcode.htm find the corresponding 
      new new CultureInfo ( " EN " ),
      new new CultureInfo ( " TH " ) 
    }; 


    app.UseRequestLocalization (X => 
    { 
      x.SetDefaultCulture ( " ZH-CN " ); 
      x.SupportedCultures = support;   // set the supported languages 
      x.SupportedUICultures = support;   //Set UI language, there is a big pit, if you do not set this property in the Action, CultureInfo.CurrentCulture returns the correct language, but returned in CultureInfo.CurrentUICulture is the default language 
      x.AddInitialRequestCultureProvider ( new new AcceptLanguageHeaderRequestCultureProvider () );   // set up to determine the current language the way I project is to use the Accept-language header value as a judge 
    }); 

  }

 

  2.Controller in:

  Injection in the constructor

       

public DevicesController(IStringLocalizer<SharedResource> localize=null):ControllerBase 

 

  Note: This SharedResource, must be in the area corresponding to the frame based SharedResource converted into a corresponding namespace corresponding to a path, and read the corresponding resource file, the more important.

 

Two. Dll in

  1. The need to manually package incorporated Microsoft.Extensions.Localization 

  2. need to be injected in a web project, the current thread automatically set the language in the request

  3. Resource file directory structure shown in Figure:

  

 

public class Base

  {

    private static IStringLocalizerFactory _factory = null;

    private string _name="";

    static Base()
    {
      _factory = new ResourceManagerStringLocalizerFactory(Options.Create(new LocalizationOptions(){ ResourcesPath = "Resources" }),
NullLoggerFactory.Instance);

      _name=typeof(Base).Assembly.GetName().Name;
    }

 

    protected virtual IStringLocalizer GetLocalizer()   // subsequent use of this function, corresponding to return the Localizer 
    {
     return _factory.Create ( " SharedResource " , the _name);   // Here you can specify a name, it is not necessarily a new class SharedResource, read factory source, the Create the realization comes with caching feature, so not every time a new class, direct Create like, do not do their own cache 
    } 

  }

 

Guess you like

Origin www.cnblogs.com/kugar/p/12302100.html