LanguageManager.cs
上传用户:clqclyc
上传日期:2022-04-15
资源大小:2k
文件大小:8k
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Reflection;
- using System.Data.SqlClient;
- using System.Data;
- using System.Configuration;
- using System.Resources;
- namespace Clubhouse
- {
- public static class LanguageManager
- {
- private enum Language
- {
- TradChi = 0,
- English = 1
- }
- //=====================================================================
- // generate resource
- //=====================================================================
- public static void getTextList(Control master, List<string> TextList,string pageCode)
- {
- PropertyInfo pText;
- PropertyInfo pID;
- object text;
- Type t;
- foreach (Control ctrl in master.Controls)
- {
- try
- {
- if (ctrl is DropDownList)
- {
- foreach (ListItem item in ((DropDownList)ctrl).Items)
- {
- pText = item.GetType().GetProperty("Text");
- pID = item.GetType().GetProperty("UniqueID");
- text = pageCode + pID.GetValue(item, null).ToString() + "," + pText.GetValue(item, null).ToString().Trim();
- TextList.Add((string)text);
- }
- }
- else if (
- ctrl is DataControlFieldHeaderCell ||
- ctrl is DataControlFieldCell ||
- ctrl is Label ||
- ctrl is Button ||
- ctrl is LinkButton ||
- ctrl is TextBox
- //|| ctrl is TableCell
- //|| ctrl is LiteralControl
- )
- {
- t = ctrl.GetType();
- pText = t.GetProperty("Text");
- text = pText.GetValue(ctrl, null).ToString().Trim();
- if (text != null)
- {
- TextList.Add(pageCode + "_" + ctrl.UniqueID.Replace("$","_") + "," + (string)text);
- }
- }
- }
- catch { }
- if (ctrl.HasControls()) getTextList(ctrl, TextList, pageCode);
- }
- }
-
- // DataTable Required
- // LanguageSource
- // {
- // controlID text,
- // content text
- // }
- public static void updateToDataBase(List<string> textList, string connStr)
- {
- string cmdStr = "";
- foreach (string s in textList)
- {
- string[] texts = s.Split(',');
- cmdStr += " IF not exists ( Select ID From LanguageSource Where controlID = '" + texts[0] + "') BEGIN"
- + " Insert into LanguageSource(controlID, content) Values ('" + texts[0] + "','" + texts[1].Replace("'", "''") + "'" + ") END";
- }
- using (SqlConnection conn = new SqlConnection(connStr))
- {
- using (SqlCommand cmd = new SqlCommand())
- {
- cmd.Connection = conn;
- conn.Open();
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = cmdStr;
- cmd.ExecuteNonQuery();
- }
- }
- }
- public static void exportResource(Page page,string connStr)
- {
- page.Response.Clear();
- page.Response.Buffer = true;
- page.Response.ContentType = "application/octet-stream";
- page.Response.AddHeader("Content-Disposition", "attachment; filename= " + page.Title + DateTime.Now.ToString("HHss") + ".csv");
- page.Response.Charset = "UTF-8";
- string content = "";
- using (SqlConnection conn = new SqlConnection(connStr))
- {
- using (SqlCommand cmd = new SqlCommand())
- {
- cmd.Connection = conn;
- conn.Open();
- cmd.CommandType = CommandType.Text;
- cmd.CommandText = " Select controlID, content From LanguageSource";
- SqlDataReader sdr = cmd.ExecuteReader();
- if (sdr.HasRows)
- {
- while (sdr.Read())
- {
- content += sdr["controlID"].ToString() + "," + sdr["content"].ToString() + "rn";
- }
- }
- }
- }
- page.Response.Write(content);
- page.Response.End();
- }
- //=====================================================================
- // Load Language
- //=====================================================================
- public static string ReadStaffTerminalResource(string key, int language)
- {
- //value for our return value
- string resourceValue = string.Empty;
- try
- {
- ResourceManager resourceManager;
- if (language == (int)Language.English)
- resourceManager = Resources.Resource.English.ResourceManager;
- else
- resourceManager = Resources.Resource.TradChi.ResourceManager;
- resourceValue = resourceManager.GetString(key);
- }
- catch (Exception ex)
- {
- return null;
- }
- return resourceValue;
- }
- public static void LoadLanguage(Control master, int lang, string pageCode)
- {
- string text = null;
- PropertyInfo pText;
- PropertyInfo pID;
- Type t;
- foreach (Control ctrl in master.Controls)
- {
- try
- {
- if (ctrl is DropDownList)
- {
- foreach (ListItem item in ((DropDownList)ctrl).Items)
- {
- pText = item.GetType().GetProperty("Text");
- pID = item.GetType().GetProperty("UniqueID");
- text = ReadStaffTerminalResource(pageCode + pID.GetValue(item, null).ToString(), lang);
- if (text != null)
- {
- pText.SetValue(item, text, null);
- }
- }
- }
- else if (
- ctrl is DataControlFieldHeaderCell ||
- ctrl is DataControlFieldCell ||
- ctrl is Label ||
- ctrl is Button ||
- ctrl is LinkButton
- //|| ctrl is TableCell
- //|| ctrl is LiteralControl
- )
- {
- t = ctrl.GetType();
- pText = t.GetProperty("Text");
- text = pText.GetValue(ctrl, null).ToString().Trim();
- if (text != null)
- {
- text = ReadStaffTerminalResource(pageCode + ctrl.UniqueID, lang);
- }
- }
- }
- catch { }
- if (ctrl.HasControls()) LoadLanguage(ctrl, lang, pageCode);
- }
- }
- }
- }