. NET resource file for multi-language switching

Source: Internet
Author: User

1. Create the corresponding resource file

Lang.en.resx English

Lang.resx Chinese, default

Lang.zh-tw.resx Traditional

First of all, these three files in front of the name needs to be the same, just dot behind the language code is not the same (En, empty, ZH-TW).

Language code should not be written in the corresponding system of the corresponding language code, the following will be used.

After the creation is complete, only the default, Lang.resx contains lang. Designer.cs file, after you create the Lang.resx, then create the other two, no longer generate Designer.cs files.

When you add data to a resource file, remember that the keys for the data in the three files are the same, and the values are different.

For example:

Otherwise, when the switch, error.

2. If you want to use the resource file in the page, Lang. The internal in the Designer.cs file needs to be replaced with public,

Note that every time you modify the resource file, he will automatically become internal, need to replace the batch to public, otherwise there is no smart hint, and the compilation passed, but the operation will be error.

Cases:

Give him bulk replacement to public

If you add another email, the internal you just replaced appears again.

After the replacement, the front page to write again, then prompted, also do not error.

3. Code settings

When switching languages, pass values to this page and set cookies

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;namespacewebapplication1{ Public Partial classChangeLan:System.Web.UI.Page {/// <summary>        ///         /// </summary>        /// <param name= "Sender" ></param>        /// <param name= "E" ></param>        protected voidPage_Load (Objectsender, EventArgs e) {            //Toggle language null: In default language, ZH-TW: traditional en: English            stringLAN = request.querystring["LAN"] +""; //Write CookieHttpCookie HC =NewHttpCookie ("language"); Hc. Value=lan; Hc. Expires= DateTime.Now.AddDays (1);            Response.appendcookie (HC); //return to the source page, where all pages inherit the Multilanguagebase class, implementing multiple LanguagesResponse.Redirect (Request.UrlReferrer.ToString ()); }    }}

SOURCE page:

<%@ Page language="C #"autoeventwireup="true"Codebehind="WebForm1.aspx.cs"inherits="Webapplication1.webform1"%><! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Server"> <meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/> <title></title>"Form1"runat="Server"> <div> <%=webapplication1.lang.name%> <br/> <%=webappli Cation1.lang.Address%> </div> <a href="changelan.aspx?lan=en">english</a> <a href="changelan.aspx">chinese</a> <a href="CHANGELAN.ASPX?LAN=ZH-TW">TW</a> </form></body>
 using   System.Web.Script.Serialization;  using   System.Data;  using   System.Configuration;  using   System.Globalization;  using   System.Threading;  namespace  //inheritance  multilanguagebase 
   Public Partial class

protected void Page_Load (object

}

Base class:

usingSystem;usingSystem.Collections.Generic;usingSystem.Globalization;usingSystem.Linq;usingSystem.Threading;usingsystem.web;namespacewebapplication1{ Public classMultiLanguageBase:System.Web.UI.Page {/// <summary>        ///All pages inherit this class before loading;/// </summary>        /// <param name= "E" ></param>        protected Override voidonpreload (EventArgs e) {Base.            Onpreload (e); stringLAN =""; if(request.cookies["language"] !=NULL) {LAN= request.cookies["language"]. Value +""; }            varCulture =NewCultureInfo (LAN); Thread.CurrentThread.CurrentUICulture=culture; Thread.CurrentThread.CurrentCulture=culture; }    }}

Effect:

. NET resource file for multi-language switching

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.