Home > Backend Development > C#.Net Tutorial > Detailed introduction to the sample code for multi-language implementation of Asp.Net Core MVC project

Detailed introduction to the sample code for multi-language implementation of Asp.Net Core MVC project

黄舟
Release: 2017-06-04 09:48:17
Original
2705 people have browsed it

This article mainly introduces the Asp.Net Core MVC project implementation multi-language instance (Globalization/Localization), with It has a certain reference value. If you are interested, you can check it out. I just recently implemented a multi-language function for a

Razor

MVC project, called Globalization, Localization, whatever. The final effect to be achieved is to switch the language of the entire site with one click, and only one set of pages needs to be written during development. Let’s get to the point

First, we need to create a CultureConfigurer class to manage localization resources and complete the "translation" link:

Here I used

Static

class, and then execute the Init() method when the MVC project StartUp is actually a bit stupid. Of course, you can also write an interface first and then use dependency injection to create a singleton .

There are a few points to note here:

1. The enum class Culture is used to represent the language to be implemented. Here I just simply implemented Chinese and English (I don’t understand the others), The corresponding CultureConfigurer class has two Dictionary

in Chinese and English. 2. Use Assembly.Load to load the assembly. The parameter is your own assembly name. I just wrote a

# here. ## 3. I chose the json file for the resource file, also for the convenience of calling in js. Of course, you can also use

xml

or any format you want to use. You only need to adjust the parsing method and load the file content. Just go to the corresponding Dictionary

 4. When you see the GetValue method

, I believe everyone has understood it. In fact, it is multi-language, no matter what language it is, it uses a certain word. key, and then call this method to "translate" into words in the current language. For example, if "Open" is used as the Key, then there should be a KeyValuePair in the Chinese Dictionary that is "Open": "Open", and the corresponding English should have an "Open": "Open", then when Culture is in Chinese, the display will be "Open" means "Open" in English.

 5. Resource files can be created anywhere in the assembly. If your project has a project.json file, then add it in buildOptions. Be careful to modify the path according to your own file location

"embed": {
   "include": [
    "Localization/SourceFiles/*.json"
   ]
  }
Copy after login

If it is VS2017, it is a csproj file, then right-click the resource file to be added, select "

Properties

", change the configuration to "All configurations", and change the "Generate operation" in the advanced configuration properties to " Embedded resources", as shown below:

At this point, we have written the core class to implement localization, and now we need to solve the problem of how to display it on the page:

Create a new class MyRazorPage in the MVC project

using System;
using Microsoft.AspNetCore.Mvc.Razor;
using Localization;

namespace MVC.Views
{
  public abstract class MyRazorPage<TModel> : RazorPage<TModel>
  {
    public virtual string L(string source)
    {
      var value = Context.Request.Cookies["culture"];
      Culture c;
      if (string.IsNullOrEmpty(value) || !Enum.TryParse(value, out c))
      {
        c = Culture.Cn;
      }
      return CultureConfigurer.GetValue(source, c);
    }
  }
}
Copy after login

Note that this class is an

abstract class

,

inheritsRazorPage. Then find the _ViewImports.cshtml file in the Views folder, and add a line "@inherits MVC.Views.MyRazorPage" in it, so that all your RazorPages will inherit the MyRazorPage class, which means you can Write the method you want to use and you can call it directly in cshtml. Here I wrote an L method and called the GetValue method of CultureConfigurer. Then, the text that needs to be translated on the page just needs to be written like @L("Open"). As you can see, I save the user language in Cookie, and everyone can have their own implementation methods here. My implementation method is very simple. When the user switches languages, he accesses an interface, modifies the cookie representing the language, and then refreshes the page.

The above is the detailed content of Detailed introduction to the sample code for multi-language implementation of Asp.Net Core MVC project. 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