The Asp. Net Core MVC project implements multi-language (Globalization/Localization) and mvclocalization.
Recently, we have implemented a multi-language function for a Razor MVC project, such as Globalization, Localization, and whatever. The final result is to switch the full-site language with one click, and you only need to write a set of pages during development.
Enter the subject
First, we need to create a culturecycler class to manage local resources and complete the "Translation" process:
Here I use static classes and execute the Init () method when the MVC project StartUp. It is actually a bit silly. Of course you can also write an interface and inject it into a singleton with dependencies.
1 using System.Collections.Generic; 2 using System.IO; 3 using System.Reflection; 4 using Newtonsoft.Json; 5 6 namespace Localization 7 { 8 public enum Culture 9 {10 Cn,11 En12 }13 14 public static class CultureConfigurer15 {16 private static Dictionary<string, string> _enDictionary;17 private static Dictionary<string, string> _cnDictionary;18 19 public static void Init()20 {21 var assembly = Assembly.Load(new AssemblyName("Localization"));22 23 var resourceNames = assembly.GetManifestResourceNames();24 foreach (var resourceName in resourceNames)25 {26 if (resourceName.EndsWith("en-US.json") || resourceName.EndsWith("zh-CN.json"))27 {28 using (var stream = assembly.GetManifestResourceStream(resourceName))29 {30 if (stream != null)31 {32 using (StreamReader reader = new StreamReader(stream))33 {34 var content = reader.ReadToEnd();35 Dictionary<string, string> localizationDictionary =36 JsonConvert.DeserializeObject<Dictionary<string, string>>(content);37 if (resourceName.EndsWith("en-US.json"))38 {39 _enDictionary = localizationDictionary;40 }41 else42 {43 _cnDictionary = localizationDictionary;44 }45 }46 }47 }48 }49 }50 }51 52 public static string GetValue(string key, Culture culture)53 {54 switch (culture)55 {56 case (Culture.Cn):57 {58 if (_cnDictionary.ContainsKey(key))59 {60 return _cnDictionary[key];61 }62 else63 {64 return $"[{key}]";65 }66 }67 case (Culture.En):68 {69 if (_enDictionary.ContainsKey(key))70 {71 return _enDictionary[key];72 }73 else74 {75 return $"[{key}]";76 }77 }78 default:79 {80 return $"[{key}]";81 }82 }83 }84 }85 }
View Code
Note the following points:
1. The enum class Culture is used to represent the language to be implemented. Here I simply implement both Chinese and English (I do not understand other languages). The corresponding culturecycler class has two dictionaries: Chinese and English.
2. I used Assembly. Load to Load the Assembly. The parameter is your own Assembly name. Here I just wrote
3. I chose a json file for resource files to facilitate js calls. Of course, you can also use xml or any format you want to use. You only need to adjust the resolution method, load the file content to the corresponding Dictionary.
4. When we see the GetValue method, I believe everyone understands it. In fact, multiple languages use a word as the key no matter what language they use, and then call this method to "translate" the words in the current language. For example, if "Open" is used as the Key, a KeyValuePair in the Chinese Dictionary is "Open": "Open", and an "Open" in the corresponding English should be displayed ": "Open", the "Open" and "Open" are displayed in the "Culture" text ".
5. The resource file can be created anywhere in the Assembly. If your project has a project. json file, add it in buildOptions. Note that you can modify the path based on your file location.
"embed": { "include": [ "Localization/SourceFiles/*.json" ] }
If VS2017 is a csproj file, right-click the resource file to be added, select "attribute", and change the configuration to "All configurations ", in the advanced configuration attributes, modify "generate operation" to "embedded resource", for example:
At this point, we have written the core class for localization. The following describes how to display the content on the page:
Create a new class MyRazorPage In the MVC Project
1 using System; 2 using Microsoft.AspNetCore.Mvc.Razor; 3 using Localization; 4 5 namespace MVC.Views 6 { 7 public abstract class MyRazorPage<TModel> : RazorPage<TModel> 8 { 9 public virtual string L(string source)10 {11 var value = Context.Request.Cookies["__culture"];12 Culture c;13 if (string.IsNullOrEmpty(value) || !Enum.TryParse(value, out c))14 {15 c = Culture.Cn;16 }17 return CultureConfigurer.GetValue(source, c);18 }19 }20 }
Note that this class is an abstract class that inherits RazorPage <TModel>. Find _ ViewImports in the Views folder. add a line "@ inherits MVC" to the cshtml file. views. myRazorPage <TModel> ", so that all your razorpages will inherit the MyRazorPage class. That is to say, you can write the method you want in MyRazorPage and call it directly in cshtml. Here I wrote an L method and called the GetValue METHOD OF THE CultureConfigurer. The text to be translated on the page can be written as @ L ("Open.
As you can see, I saved the user language in the Cookie. Here you can have their own implementation methods. My implementation method is very simple. When you switch the language, you access an interface, modify the Cookie representing the language, and then refresh the page.
If you have any questions, please leave a message below. This is my first blog and I hope it will be useful to you!