. NET software configuration multi-language environment

Source: Internet
Author: User

1 Configuration Webconfig

<add key= "Defaultculture" value= "ZH-CN"/>
<add key= "Cnculture" value= "ZH-CN"/>
<add key= "enculture" value= "en-US"/>

2 Add Resource File

Text.en-us.resx; Text.zh-cn.resx

3 Fill in Resouce documents

4 Configuration Global.asax.cs

protected void Application_BeginRequest (Object sender, EventArgs e)
{
Try
{
if (request.cookies["Cultureresource"]!=null)
Thread.currentthread.currentculture=new CultureInfo (request.cookies["Cultureresource"). Value);
Else
Thread.currentthread.currentculture=new CultureInfo (configurationsettings.appsettings["Defaultculture"). ToString ());
Thread.currentthread.currentuiculture=thread.currentthread.currentculture;
}
catch (Exception)
{
Thread.currentthread.currentculture=new CultureInfo (configurationsettings.appsettings["Defaultculture"). ToString ());
}
}
5 adding ASPX pages

The background code is as follows:

Using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Threading;
using System.Resources;
using System.Globalization;
using System.Diagnostics;
using System.Reflection;


Namespace Searchname
{
 ///<summary>
 ///Summary description for Culture.
 ///</summary>
 public class Culture:System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Button Button1;
  protected System.Web.UI.WebControls.Label Label2;
  protected System.Web.UI.WebControls.Button Button2;
  protected System.Web.UI.WebControls.Label Label1;
 
  private void Page_Load (object sender, System.EventArgs e)
  {
    //put user code to initialize the page here
   label2.text = Thread.CurrentThread.CurrentCulture.EnglishName;
   label1.text = Resource ("Label1");
  }

#region Web Form Designer generated code
Override protected void OnInit (EventArgs e)
{
//
Codegen:this the call are required by the ASP.net Web Form Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Required to Designer support-do not modify
The contents is with the Code Editor.
</summary>
private void InitializeComponent ()
{
This. Button1.Click + = new System.EventHandler (this. Button1_Click);
This. Button2.click + = new System.EventHandler (this. button2_click);
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion


<summary>
Get resource value by key.
</summary>
<param name= "key" >resoure key</param>
<returns>string</returns>
public string Resource (string key)
{
string resourcevalue = null;

CultureInfo ci = cultureinfo.currentculture;
ResourceManager rm = new ResourceManager ("Searchname.text", assembly.getexecutingassembly ());

Try
{
Resourcevalue = rm. GetString (KEY,CI);
}
catch (Exception)
{
Return "";
}

return resourcevalue;
}

  private void Button1_Click (object sender, System.EventArgs e)
  {
    This. Updateculturecookie (configurationsettings.appsettings["Enculture"). ToString ());
   system.web.ui.page currentpage= (System.Web.UI.Page) this;
   response.redirect (currentPage.Request.Url.ToString ());
  }

  private void Updateculturecookie (string culture)
  {
   if ( request.cookies["Cultureresource"]!= null)
   {
    response.cookies[" Cultureresource "]. Value = culture;
    response.cookies["Cultureresource"]. Expires = System.DateTime.Now.AddDays (30);
   }
   else
   {
    httpcookie cultureCookie = new HttpCookie ("Cultureresource");
    culturecookie.value = culture;
    culturecookie.expires = System.DateTime.Now.AddDays (30);
    response.cookies.add (Culturecookie);
   } 
  }

private void Button2_Click (object sender, System.EventArgs e)
{
This. Updateculturecookie (configurationsettings.appsettings["Cnculture"). ToString ());
System.Web.UI.Page currentpage= (System.Web.UI.Page) this;
Response.Redirect (CurrentPage.Request.Url.ToString ());
}
}
}

Ok




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.