Cool MVC: another multi-language Solution

Source: Internet
Author: User

Hello, this articleArticleAnother solution called MVC multi-language is because there is already an article titled ASP. net mvc multi-language solution in the garden. 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. This means that after each update of the. resx resource file, you must use resgen to regenerate the. Resources file. Well, what are the advantages of converting files into. Resources in addition to reducing a little storage space? I have never tested this. I have to ask Microsoft to know it. The multi-language solution proposed in this article does not need to generate additional. Resources files, As long as. resx is enough .. In fact, the resx file refers to the XML file of the name-value set. Therefore, a multi-language solution for directly using. resx is generated:

1. Load all name-value to the memory by reading XML, and then you can query the corresponding language value like the dictionary.

2. Use filesystemwatcher to monitor language files. If there is an update, the corresponding language is automatically updated.

It is also very easy to use, as in the previous solution: <% = html. lang ("key") %>, if used in controller, directly: This. lang ("key ");

To facilitate expansion (your language file may not be. resx, but read directly from the database), the solution defines the language-provided interface:

  //    // language Provider Interface   ///    Public   interface  ilanguageprovider { //    /// query the corresponding language value   ///    ///  language name    //  key    // 
    
     string  getlanguage ( string  Lang,  string  key) ;}

As for implementation, I have introduced the principle in general. I will not post it here.CodeTo download the complete code at the end of the article.

Like many MVC examples, htmlhelper and controller extensions are also implemented. It seems that I can't think of anything better than extension.

     /// <Summary>      /// Author: Bruce     // Blog: http://coolcode.cnblogs.com      /// </Summary>      Public   Static   Class Localizationhelpers { /// <Summary>          /// Directly used in HTML          /// </Summary>          /// <Param name = "htmlhelper"> </param>          /// <Param name = "key"> </param>          /// <Returns> </returns>          Public   Static   String Lang ( This Htmlhelper,String Key ){ Return Getlangstring (key );} Public   Static   String Lang ( This Controller Control, String Key ){ Return Getlangstring (key );} Private   Static Ilanguageprovider langprovider = Null ; Private   Static   String Getlangstring (String Key) {httprequest request = httpcontext. Current. request; /* The default language is English */              String Lang = "En-us" ; /* Check whether cookies have user language settings */              If (Request. Cookies [ "Coolcode_lang" ]! = Null ) Lang = request. Cookies [ "Coolcode_lang" ]. Value; /* Obtain the client language if no cookies exist */              Else   If (Request. userages. length> 0) {lang = request. userages [0];} /* Language to load, only when used for the first time */              If (Langprovider = Null ) {Langprovider = New Languageprovider ();} Return Langprovider. getlanguage (Lang, key );}}
 
In addition, the method for reading the language from the resource file is attached:
         /// <Summary>          /// Load the language file (here, the language file is a general resource file, as long as it is an XML file conforming to this structure:          /// <Data name = "key"> <value> text </value> </data>)          /// </Summary>         /// <Param name = "FILENAME"> language file path </param>          Public   Void Read ( String Filename) {var Xe = xelement. Load (filename); var xes = Xe. Elements (). Where (C => C. Name = "Data" & C. Attribute ( "Name" )! = Null & C. element ( "Value" )! = Null ). Tolist (); Foreach (VAR x In Xes ){ This . Add (X. Attribute ( "Name" ). Value, X. element ( "Value" ). Value );}}
 
Is it easy?
 
Complete code: coolcode.multi‑age.zip

Postscript: The first time I posted it to the homepage, I hope it will be helpful. I don't mind even throwing bricks without Jade.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.