LanguageManager.cs
上传用户:clqclyc
上传日期:2022-04-15
资源大小:2k
文件大小:8k
源码类别:

多国语言处理

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Reflection;
  8. using System.Data.SqlClient;
  9. using System.Data;
  10. using System.Configuration;
  11. using System.Resources;
  12. namespace Clubhouse
  13. {
  14.     public static class LanguageManager
  15.     {
  16.         private enum Language
  17.         {
  18.             TradChi = 0,
  19.             English = 1            
  20.         }
  21.         //=====================================================================
  22.         // generate resource
  23.         //=====================================================================        
  24.         public static void getTextList(Control master, List<string> TextList,string pageCode)
  25.         {
  26.             PropertyInfo pText;
  27.             PropertyInfo pID;
  28.             object text;
  29.             Type t;
  30.             foreach (Control ctrl in master.Controls)
  31.             {
  32.                 try
  33.                 {
  34.                     if (ctrl is DropDownList)
  35.                     {
  36.                         foreach (ListItem item in ((DropDownList)ctrl).Items)
  37.                         {
  38.                             pText = item.GetType().GetProperty("Text");
  39.                             pID = item.GetType().GetProperty("UniqueID");                            
  40.                             text = pageCode + pID.GetValue(item, null).ToString() + "," + pText.GetValue(item, null).ToString().Trim();
  41.                             TextList.Add((string)text);
  42.                         }
  43.                     }
  44.                     else if (
  45.                         ctrl is DataControlFieldHeaderCell ||
  46.                         ctrl is DataControlFieldCell ||
  47.                         ctrl is Label ||
  48.                         ctrl is Button ||
  49.                         ctrl is LinkButton  ||
  50.                         ctrl is TextBox
  51.                         //|| ctrl is TableCell                    
  52.                         //|| ctrl is LiteralControl
  53.                         )
  54.                     {
  55.                         t = ctrl.GetType();
  56.                         pText = t.GetProperty("Text");
  57.                         text = pText.GetValue(ctrl, null).ToString().Trim();
  58.                         if (text != null)
  59.                         {
  60.                             TextList.Add(pageCode + "_" +  ctrl.UniqueID.Replace("$","_") + "," + (string)text);                         
  61.                         }
  62.                     }
  63.                 }
  64.                 catch { }
  65.                 if (ctrl.HasControls()) getTextList(ctrl, TextList, pageCode);
  66.             }
  67.         }
  68.         
  69.         //     DataTable Required
  70.         //     LanguageSource
  71.         //     {
  72.         //         controlID   text,
  73.         //         content      text
  74.         //     }
  75.         public static void updateToDataBase(List<string> textList, string connStr)
  76.         {
  77.             string cmdStr = "";
  78.             foreach (string s in textList)
  79.             {
  80.                 string[] texts = s.Split(',');
  81.                 cmdStr += " IF not exists ( Select ID From LanguageSource Where controlID = '" + texts[0] + "') BEGIN"
  82.                     + " Insert into LanguageSource(controlID, content) Values ('" + texts[0] + "','" + texts[1].Replace("'", "''") + "'" + ") END";
  83.             }
  84.             using (SqlConnection conn = new SqlConnection(connStr))
  85.             {
  86.                 using (SqlCommand cmd = new SqlCommand())
  87.                 {
  88.                     cmd.Connection = conn;
  89.                     conn.Open();
  90.                     cmd.CommandType = CommandType.Text;
  91.                     cmd.CommandText = cmdStr;
  92.                     cmd.ExecuteNonQuery();
  93.                 }
  94.             }            
  95.         }
  96.         public static void exportResource(Page page,string connStr)
  97.         {
  98.             page.Response.Clear();
  99.             page.Response.Buffer = true;
  100.             page.Response.ContentType = "application/octet-stream";
  101.             page.Response.AddHeader("Content-Disposition", "attachment; filename= " + page.Title + DateTime.Now.ToString("HHss") + ".csv");
  102.             page.Response.Charset = "UTF-8";
  103.             string content = "";            
  104.             using (SqlConnection conn = new SqlConnection(connStr))
  105.             {
  106.                 using (SqlCommand cmd = new SqlCommand())
  107.                 {
  108.                     cmd.Connection = conn;
  109.                     conn.Open();
  110.                     cmd.CommandType = CommandType.Text;
  111.                     cmd.CommandText = " Select controlID, content From LanguageSource";
  112.                     SqlDataReader sdr = cmd.ExecuteReader();
  113.                     if (sdr.HasRows)
  114.                     {
  115.                         while (sdr.Read())
  116.                         {
  117.                             content += sdr["controlID"].ToString() + "," + sdr["content"].ToString() + "rn";
  118.                         }
  119.                     }
  120.                 }
  121.             }           
  122.             page.Response.Write(content);
  123.             page.Response.End();
  124.         }
  125.         //=====================================================================
  126.         // Load Language
  127.         //=====================================================================
  128.         public static string ReadStaffTerminalResource(string key, int language)
  129.         {
  130.             //value for our return value
  131.             string resourceValue = string.Empty;
  132.             try
  133.             {                
  134.                 ResourceManager resourceManager;
  135.                 if (language == (int)Language.English)
  136.                     resourceManager = Resources.Resource.English.ResourceManager;
  137.                 else
  138.                     resourceManager = Resources.Resource.TradChi.ResourceManager;
  139.                 resourceValue = resourceManager.GetString(key);
  140.             }
  141.             catch (Exception ex)
  142.             {
  143.                 return null;
  144.             }
  145.             return resourceValue;
  146.         }
  147.         public static void LoadLanguage(Control master, int lang, string pageCode)
  148.         {
  149.             string text = null;
  150.             PropertyInfo pText;
  151.             PropertyInfo pID;
  152.             Type t;
  153.             foreach (Control ctrl in master.Controls)
  154.             {
  155.                 try
  156.                 {
  157.                     if (ctrl is DropDownList)
  158.                     {
  159.                         foreach (ListItem item in ((DropDownList)ctrl).Items)
  160.                         {
  161.                             pText = item.GetType().GetProperty("Text");
  162.                             pID = item.GetType().GetProperty("UniqueID");
  163.                             text = ReadStaffTerminalResource(pageCode + pID.GetValue(item, null).ToString(), lang);
  164.                             if (text != null)
  165.                             {
  166.                                 pText.SetValue(item, text, null);
  167.                             }
  168.                         }
  169.                     }
  170.                     else if (
  171.                         ctrl is DataControlFieldHeaderCell ||
  172.                         ctrl is DataControlFieldCell ||
  173.                         ctrl is Label ||
  174.                         ctrl is Button ||
  175.                         ctrl is LinkButton
  176.                         //|| ctrl is TableCell                    
  177.                         //|| ctrl is LiteralControl
  178.                         )
  179.                     {
  180.                         t = ctrl.GetType();
  181.                         pText = t.GetProperty("Text");
  182.                         text = pText.GetValue(ctrl, null).ToString().Trim();
  183.                         if (text != null)
  184.                         {
  185.                             text = ReadStaffTerminalResource(pageCode + ctrl.UniqueID, lang);
  186.                         }
  187.                     }
  188.                 }
  189.                 catch { }
  190.                 if (ctrl.HasControls()) LoadLanguage(ctrl, lang, pageCode);                     
  191.             }
  192.         }
  193.     }
  194. }