Mvc2.0 localization (alternative solution)

Source: Internet
Author: User

This article is the end of the enhanced and refined mvc2.0 localization function introduced in this article. If there are any deficiencies, you can point them out.

How to enhance and refine the previous article? It mainly starts from the following three points.

1. It is determined based on the user's browser's automatic language and can also be customized by the user.
 

2. Add multiple languages. Similar to resource files, you can have multiple language resource libraries.

3. Global Localization: Non-page content can be localized, for example, JSON data localization from Asynchronous to foreground.

             

            The main steps are as follows:

             

            1. Automatic language judgment based on the user's browser, and the user can also customize the site language

            The solution is to determine the cookie that is automatically set by the user stored on the client. If this value is null, the language is automatically recognized based on the user's browser. If not empty, the website language is obtained based on the cookie value set by the user. Then, how can we identify the language based on the customer's browser. <system. web> Add <globalization enableclientbasedculture = "true" Culture = "Auto" uiculture = "Auto"/> Settings to a node. Based on this setting, the server can use system. Threading. thread. currentthread. currentculture to identify the language of the user's browser. Paste some code in global for your reference:

             protected void Application_BeginRequest(Object sender, EventArgs e)        {            HttpCookie lang = Request.Cookies["Lang"];            if (lang != null)            {          if (Response.ContentType == "text/html" || Response.ContentType == "application/json")                                    Response.Filter = new LocalizationHandler(Response.Filter, lang.Value);              return;            }            string langFromBrowser = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();            string strLang = string.Empty;            if (string.Compare("zh-CN", langFromBrowser, true) == 0)            {                strLang = "SimplifiedChinese";            }            else if (string.Compare("zh-Hant", langFromBrowser, true) == 0)            {                strLang = "TraditionalChinese";            }            else if (langFromBrowser.Contains("en"))            {                strLang = "English";            }            else            {                strLang = "English";            }            if (Response.ContentType == "text/html" || Response.ContentType == "application/json")                Response.Filter = new LocalizationHandler(Response.Filter, strLang);    }

            With the preceding code block, the server can intelligently identify whether the site language is displayed based on the cookie of the client or the browser language.

             

            2. Add multiple languages. Similar to resource files, you can have multiple language resource libraries.

            This extension is very simple. In the previous article, only one XML file corresponds to the. net4.0 cache of the XML file. For multi-language packages, we only need to create multiple XML files. And the cache mechanism corresponding to each XML file. As follows:

            The language packs in the resources file are divided into English, simplified Chinese, and traditional Chinese. For the XML format, see mvc2.0 localization (alternative solution) <top>.

            How to obtain the translated text in XML is clearly written in the previous article. The only thing to note is to create an independent cache mechanism for each XML file (Language Pack.

             

            3. Global Localization: Non-page content can be localized, for example, JSON data localization from Asynchronous to foreground

            First, let's take a look at the first part of the implementation steps in this article and paste the code 6th lines,if (Response.ContentType == "text/html" || Response.ContentType == "application/json") Here, the localization of JSON format is controlled. To put it bluntly, after the background Asynchronization to the front-end localization resources in JSON format, we can also write them in the XML file, as long as the response. the filter mechanism can be localized without adding additional localized code.

            The specific implementation is as follows:

                  MVC2.0 JsonResult:

              

            Jquery call:

            "<= Language is applied>" is displayed in three languages based on users in different countries:

            Simplified: the language is set.

            Traditional Chinese: The statement has been set.

            Language is applied.

             

            4. program running

             

            V. Summary

            After the above introduction, a localized alternative solution has been basically completed. This solution also has its disadvantages. However, the advantage is that the localized content can be dynamically changed. Instead of modifying the source code, you only need to modify the content of the corresponding XML file.

             

            I hope this article will help you. If you have any shortcomings, thank you!

            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.