Now ASP. net mvc has come out of version 4, and many more features are available. But how can I conveniently implement multilingual features under ASP. NET MVC?
As for the Multilingual features of a website, I think there are two aspects:
1. The text displayed on the HTML interface must be in multiple languages.
2. multiple languages are required for JS output on the HTML interface.
The text directly written in HTML cannot directly write the text to be output, but must be replaced by a tag. The same is true for JavaScript.
How can we implement multiple languages transparently under MVC? The so-called transparent implementation means that programmers do not need to think too much about the problem of multiple languages in developing programs. They can simply call a method to implement multiple languages, in addition, the language file to be used is enough for each language, and the concentrated translation of this language completes the multi-language function.
For example
<HTML>
<Head>
</Head>
<Body>
Multi-language output text // you cannot directly write Chinese characters here. A good way is to directly use <% = html. Lang ("string1") %> for output.
</Body>
</Html>
Here, <% = html. Lang ("clickme") %> extends htmlhelper and adds a Lang method. The parameter is the name of the resource string to be translated.
How can I scale htmlhelper? The following class is the added extension class.
Code
Using system;
Using system. Data;
Using system. configuration;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. xml. LINQ;
Using system. Globalization;
Using system. Web. compilation;
Using system. Web. MVC;
Using system. Web. Routing;
Namespace system. Web. MVC {
Public static class localizationhelpers {
/// <Summary>
/// Use it directly in the external html
/// </Summary>
/// <Param name = "htmlhelper"> </param>
/// <Param name = "key"> </param>
/// <Returns> </returns>
Public static string Lang (this htmlhelper, string key ){
String filepath = htmlhelper. viewcontext. httpcontext. server. mappath ("/") + "resource \\";
Return getlangstring (htmlhelper. viewcontext. httpcontext, key, filepath );
}
/// <Summary>
/// Used directly in external HTML to output a string to JS
/// </Summary>
/// <Param name = "htmlhelper"> </param>
/// <Param name = "key"> </param>
/// <Returns> </returns>
Public static string langoutjsvar (this htmlhelper, string key ){
String filepath = htmlhelper. viewcontext. httpcontext. server. mappath ("/") + "resource \\";
String langstr = getlangstring (htmlhelper. viewcontext. httpcontext, key, filepath );
Return string. Format ("Var {0} = '{1}'", key, langstr );
}
/// <Summary>
/// Used in C #
/// </Summary>
/// <Param name = "httpcontext"> </param>
/// <Param name = "key"> </param>
/// <Returns> </returns>
Public static string innerlang (httpcontextbase httpcontext, string key ){
String filepath = httpcontext. server. mappath ("/") + "resource \\";
Return getlangstring (httpcontext, key, filepath );
}
Private Static string getlangstring (httpcontextbase httpcontext, string key, string filepath ){
Langtype = langtype.cn;
If (httpcontext. session ["Lang"]! = NULL ){
Langtype = (langtype) httpcontext. session ["Lang"];
}
Return langresourcefileprovider. getlangstring (Key, langtype, filepath );
}
}
Public static class langresourcefileprovider {
Public static string getlangstring (string key, langtype, string filepath ){
String filename;
Switch (langtype ){
Case langtype.cn: filename = "zh-cn.resources"; break;
Case langtype. En: filename = "en-us.resources"; break;
Default: filename = "zh-cn.resources"; break;
}
System. Resources. resourcereader reader = new system. Resources. resourcereader (filepath + filename );
String resourcetype;
Byte [] resourcedata;
String result = string. empty;
Try {
Reader. getresourcedata (Key, out resourcetype, out resourcedata );
// Remove the first byte, useless
Byte [] arr = new byte [resourcedata. Length-1];
For (INT I = 0; I <arr. length; I ++ ){
Arr [I] = resourcedata [I + 1];
}
Result = system. Text. encoding. utf8.getstring (ARR );
}
Catch (exception ex ){
}
Finally {
Reader. Close ();
}
Return result;
}
}
Public Enum langtype {
CN,
En
}
}
This class is called localizationhelpers and exposes the Lang, langoutjsvar, and innerlang methods. Among them, Lang and langoutjsvar can be called directly on the HTML interface, and innerlang can be used in the C # background.
The. resx resource file is used here. Note that this file must be compiled before it can be used. Otherwise, the added items cannot be found. To compile this file, you can use resgen.exe that comes with. net.
The above class is very simple. It is determined based on the language type in the passed session ["Lang"]. The read resource file must be in the Resource Directory ), then read the required name and return the corresponding string value, which is the final text to be output.
In front-end. aspx, you can directly use <% = html. Lang ("string1") %> to output. For JS output, see the following example.
<Script language = "JavaScript" type = "text/JavaScript">
<% = Html. langoutjsvar ("MSG") %>
Function show ()
{
Alert (MSG );
}
</SCRIPT>
In this case, OK.
You can use
Viewdata ["message"] = localizationhelpers. innerlang (this. controllercontext. httpcontext, "welcome.
I made a demo Based on ASP. NET MVC 4, as shown below:
Click to download specific code