Detailed introduction of Localization in ASP. NET Core ),
First, add AddLocalization and AddViewLocalization in ConfigureServices of Startup and configure RequestLocalizationOptions (Here we assume that both English and Chinese are used ):
public void ConfigureServices(IServiceCollection services){ services.AddLocalization(options => options.ResourcesPath = "Resources"); services.AddMvc() .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix); services.Configure<RequestLocalizationOptions>( opts => { var supportedCultures = new List<CultureInfo> { new CultureInfo("en-US"), new CultureInfo("zh-CN") }; opts.SupportedCultures = supportedCultures; opts.SupportedUICultures = supportedCultures; });}
Apply RequestLocalizationOptions in Configure () method of Startup:
var requestLocalizationOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value;app.UseRequestLocalization(requestLocalizationOptions);
In the _ Layout. cshtml view, the IViewLocalizer interface is used to display the suffix of the page title in multiple languages:
@using Microsoft.AspNetCore.Mvc.Localization@inject IViewLocalizer Localizer<!DOCTYPE html>
Then in ASP. create the Resources folder in the. NET Core Web project and add Views to the folder. shared. _ Layout. the en-Us.resx and Views. shared. _ Layout. zh-CN.resx file, Views. shared. _ Layout. resx file, and add the statement text corresponding to "SiteTitle:
1) Views. Shared. _ Layout. en-Us.resx
2) Views. Shared. _ Layout. zh-CN.resx
When the ASP. NET Core site is run, the text of the corresponding Language will be displayed based on the browser's Language settings (Accept-Language header), the culture query parameter, or. AspNetCore. Culture Cookie value:
Note: Do not add Views without language names. shared. _ Layout. en-Us.resx, otherwise add the name of the code language. in the resx file, "Custom tool ResXFileCodeGenerator failed to produce an output for input file... but did not log a specific error. "question.
Reference: Globalization and localization
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.