Multi-lingual support in ASP.

Source: Internet
Author: User
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.

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.