Detailed graphic and text explanation of multi-language support in ASP.NET Core

黄舟
Release: 2017-09-05 15:14:35
Original
2314 people have browsed it

This article mainly introduces the multi-language support (Localization) in ASP.NET Core, which has certain reference value. Those who are interested can learn about it

First add AddLocalization and AddViewLocalization in ConfigureServices of Startup And configure RequestLocalizationOptions (assuming English and Chinese are used here):


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;
    });
}
Copy after login

Apply RequestLocalizationOptions in the Configure() method of Startup:


var requestLocalizationOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>().Value;
app.UseRequestLocalization(requestLocalizationOptions);
Copy after login

Then display the suffix of the page title in a multi-lingual manner 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>
Copy after login

Then in ASP.NET Core Web Create the Resources folder in the project, add the Views.Shared._Layout.en-Us.resx and Views.Shared._Layout.zh-CN.resx files, the Views.Shared._Layout.resx file respectively, and add "SiteTitle" The corresponding statement text:

1) Views.Shared._Layout.en-Us.resx

##2) Views.Shared._Layout.zh- CN.resx

#When running the ASP.NET Core site, it will be based on the browser's language setting (Accept-Language header), or culture query parameters, or .AspNetCore .Culture Cookie value displays the text of the corresponding language:

# Things to note: Never add Views without a language name. Shared._Layout.en-Us.resx, otherwise you will encounter "Custom tool ResXFileCodeGenerator failed to produce an output for input file ... but did not log a specific error." when adding a .resx file with a code language name.

The above is the detailed content of Detailed graphic and text explanation of multi-language support in ASP.NET Core. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!