First, add addlocalization and addviewlocalization in the Startup configureservices and configure Requestlocalizationoptions (this assumes English and Chinese):
Public voidconfigureservices (iservicecollection services) {services. Addlocalization (Options= Options. Resourcespath ="Resources"); Services. Addmvc (). Addviewlocalization (Languageviewlocationexpanderformat.suffix); Services. Configure<RequestLocalizationOptions>(OPTs= { varSupportedCultures =NewList<cultureinfo> { NewCultureInfo ("en -US"), NewCultureInfo ("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><HTML><Head> <title>@viewdata["Title"]-@localizer["Sitetitle"]</title></Head><Body></Body></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.
Be sure to see the reference Document: Globalization and Localization
Experience multi-language support in ASP. NET Core (Localization)