This article mainly introduces the multi-language support (Localization) in ASP., which has a certain reference value, and is interested to understand
First, add addlocalization and addviewlocalization in the Startup configureservices and configure Requestlocalizationoptions (this assumes English and Chinese):
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 the Configure () method of Startup:
var requestlocalizationoptions = app. Applicationservices.getservice<ioptions<requestlocalizationoptions>> (). Value;app. Userequestlocalization (requestlocalizationoptions);
The suffix of the page title is then displayed in a multi-lingual way through the Iviewlocalizer interface in the _layout.cshtml view:
@using Microsoft.aspnetcore.mvc.localization@inject Iviewlocalizer localizer<! DOCTYPE html>
Then create the Resources folder in the ASP. NET Core Web project, where you add the Views.shared._layout.en-us.resx and Views.shared._layout.zh-cn.resx files separately, Vi Ews. 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 you run an ASP. NET Core site, it is based on the browser's language setting (Accept-language header), or the culture query parameter, or. Aspnetcore.culture Cookie Value Displays the text for the corresponding language:
Note: Do not add views.shared._layout.en-us.resx without a language name, or you will encounter the Custom tool Resxfilecodegenerator when adding a. resx file for the code language name. Failed to produce an output for input file ... but do not log a specific error. "Ask.