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
RazorMVC 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 . using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Newtonsoft.Json;
namespace Localization
{
public enum Culture
{
Cn,
En
}
public static class CultureConfigurer
{
private static Dictionary<string, string> _enDictionary;
private static Dictionary<string, string> _cnDictionary;
public static void Init()
{
var assembly = Assembly.Load(new AssemblyName("Localization"));
var resourceNames = assembly.GetManifestResourceNames();
foreach (var resourceName in resourceNames)
{
if (resourceName.EndsWith("en-US.json") || resourceName.EndsWith("zh-CN.json"))
{
using (var stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream != null)
{
using (StreamReader reader = new StreamReader(stream))
{
var content = reader.ReadToEnd();
Dictionary<string, string> localizationDictionary =
JsonConvert.DeserializeObject<Dictionary<string, string>>(content);
if (resourceName.EndsWith("en-US.json"))
{
_enDictionary = localizationDictionary;
}
else
{
_cnDictionary = localizationDictionary;
}
}
}
}
}
}
}
public static string GetValue(string key, Culture culture)
{
switch (culture)
{
case (Culture.Cn):
{
if (_cnDictionary.ContainsKey(key))
{
return _cnDictionary[key];
}
else
{
return $"[{key}]";
}
}
case (Culture.En):
{
if (_enDictionary.ContainsKey(key))
{
return _enDictionary[key];
}
else
{
return $"[{key}]";
}
}
default:
{
return $"[{key}]";
}
}
}
}
}
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 usexml
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 Dictionary4. 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."embed": { "include": [ "Localization/SourceFiles/*.json" ] }
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 projectusing 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); } } }
abstract class
,inheritsRazorPage
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!