Detailed introduction of Localization in ASP. NET Core ),

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.