This article mainly introduces the ASP. NET Core MVC project implementation of multi-language instances (globalization/localization), with a certain reference value, interested can understand
Just recently in the hands of a razor MVC project to achieve a multi-lingual function, called globalization or localization, whatever. The ultimate effect is to switch the entire station language with one click, and only write a set of pages when developing.
Get down to the chase.
First, we will create a Cultureconfigurer class for managing localized resources and completing the "translation" process:
I use static classes here, and then execute the init () method when the MVC project is started, which is a bit silly, but you can also write an interface and then inject the dependency into 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 Dictiona Ry<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 (stream Reader 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}]"; } } } }}
Here are a few things to note:
1. The Enum class culture is used to represent the language to be implemented, here I simply implemented the Chinese and English (other I do not understand), the corresponding Cultureconfigurer class will have Chinese and English two dictionary
2. The assembly was loaded with Assembly.Load, the parameter is your own assembly name, and I've written a random
3. resource file I chose the JSON file, also to facilitate the invocation of JS, of course, you can also use XML or any format you want to use, only need to adjust the parsing method, the contents of the file loaded into the corresponding dictionary can be
4. See the GetValue method, I believe we have already understood, in fact, the multilingual language, regardless of what language, all use a word to do key, and then call this method "translated" into the current language of the word. For example, "open" as key, then the Chinese dictionary should have a KeyValuePair is "open": "Opened", and the corresponding English should have an "open": "Open", then the culture is Chinese, the display is "open", English is "Open".
5. The resource file can be created anywhere in the assembly, and if your project has a Project.json file, add it in the buildoptions, and note that the path is modified according to its file location
"Embed": { "include": [ "Localization/sourcefiles/*.json" ] }
If it is VS2017, is the csproj file, then right-click the resource file to add, select "Properties", configure Change to "all configuration", "Build Action" in the Advanced configuration property to "embedded resources", such as:
Here, we've written the core classes for localizing, and here's how to address the issues that appear on the page:
Create a new class in the MVC project Myrazorpage
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);}} }
Note that this class is an abstract class that inherits the Razorpage<tmodel>. Then find the _viewimports.cshtml file under the Views folder and add a line "@inherits MVC. Views.myrazorpage<tmodel> "so that all your razorpage will inherit the Myrazorpage class, which means you can write the method you want to use in Myrazorpage, It can be called directly in the cshtml. Here I write an L method that invokes the GetValue method of the Cultureconfigurer. So, the text that needs to be translated on the page is just written as @l ("Open").
As you can see, I have saved the user's language in a cookie, and here you can have their own implementation methods. My implementation method is very simple, the user switches the language when the access to an interface, modify the representative language of the cookie, and then refresh the page.