I want to program multi-language winform software with C# and xml. how can I do  
Author Message
derrickcui





PostPosted: XML and the .NET Framework, I want to program multi-language winform software with C# and xml. how can I do Top

the following is my code, use xml file as language package. but when I run it, button's text didn't change. what do I need to config

namespace mulLan
{
public partial class Demo : Form
{
public Demo()
{
InitializeComponent();
}
/*********************** derrick language *****************/
//read default language
public static string ReadDefaultLanguage()
{
XmlReader reader = new XmlTextReader("resources/LanguageDefine.xml");
XmlDocument doc = new XmlDocument();
doc.Load(reader);
XmlNode root = doc.DocumentElement;
//select DefaultLangugae node
XmlNode node = root.SelectSingleNode("DefaultLanguage");

string result = node.Value;
if (node != null)
//get node's context
result = node.InnerText;

reader.Close();
return result;
}

//modify default language
public static void WriteDefaultLanguage(string lang)
{
DataSet ds = new DataSet();
ds.ReadXml("resources/LanguageDefine.xml");
DataTable dt = ds.Tables["Language"];

dt.Rows[0]["DefaultLanguage"] = lang;
ds.AcceptChanges();
ds.WriteXml("resources/LanguageDefine.xml");
}

//read AppConfig.xml file
public static IList GetLanguageList(string lang)
{
IList result = new ArrayList();

XmlReader reader = new XmlTextReader("resources/AppConfig.xml");
XmlDocument doc = new XmlDocument();
doc.Load(reader);

XmlNode root = doc.DocumentElement;
XmlNodeList nodelist = root.SelectNodes("Area[Language='" + lang + "']/List/Item");
foreach (XmlNode node in nodelist)
{
result.Add(node.InnerText);
}
reader.Close();

return result;
}

/// <summary></summary>
/// read multi language file
/// </summary>
/// <param name="frmName">Form Name</param>
/// <param name="lang">will display language(zh or en)</param>
/// <returns></returns>
public static Hashtable ReadResource(string frmName, string lang)
{
Hashtable result = new Hashtable();

XmlReader reader = null;
FileInfo fi = new FileInfo("resources/AppResource_" + lang + ".xml");
if (!fi.Exists)
reader = new XmlTextReader("resources/AppResource.xml");
else
reader = new XmlTextReader("resources/AppResource_" + lang + ".xml");

XmlDocument doc = new XmlDocument();
doc.Load(reader);

XmlNode root = doc.DocumentElement;
XmlNodeList nodelist = root.SelectNodes("Form[Name='" + frmName + "']/Controls/Control");
foreach (XmlNode node in nodelist)
{
try
{


if (node1 != null)
{
result.Add(node1.InnerText.ToLower(), node2.InnerText);
}
}
catch (FormatException fe)
{
Console.WriteLine(fe.ToString());
}
}
reader.Close();
return result;
}


public static void getNames(Form form)
{
//Hashtable table = AppConfig.ReadResource(form.Name,Global.GetValue("lang").ToString());
Hashtable table = ReadResource(form.Name, "ZH");
Control.ControlCollection controlNames = form.Controls;
try
{
foreach (Control control in controlNames)
{
if (control.GetType() == typeof(System.Windows.Forms.Panel))
GetSubControls(control.Controls, table);

if (control.GetType() == typeof(System.Windows.Forms.GroupBox))
GetSubControls(control.Controls, table);

if (table.Contains(control.Name.ToLower()))
control.Text = (string)table[control.Name.ToLower()];

}
if (table.Contains(form.Name.ToLower()))
form.Text = (string)table[form.Name.ToLower()];
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}

private static void GetSubControls(Control.ControlCollection controls, Hashtable table)
{
foreach (Control control in controls)
{
if (control.GetType() == typeof(System.Windows.Forms.Panel))
GetSubControls(control.Controls, table);

if (control.GetType() == typeof(System.Windows.Forms.GroupBox))
GetSubControls(control.Controls, table);

if (table.Contains(control.Name.ToLower()))
control.Text = (string)table[control.Name.ToLower()];
}
}

/*********************** end derrick language *****************/
private void Demo_Load(object sender, EventArgs e)
{
IList list;
//Hashtable resourceFile;
Demo form = new Demo();

string Language;

Language = ReadDefaultLanguage();
list = Demo.GetLanguageList(Language);
comLanguage.DataSource = list;
comLanguage.Text = Language;

getNames(form);
}

private void Default_Click(object sender, EventArgs e)
{
int strB;
string Language;
Language = comLanguage.Text;
strB = Language.IndexOf('[');
Language = Language.Substring(strB + 1, 2);
Demo.WriteDefaultLanguage(Language);
}
}
}




.NET Development12  
 
 
Sergey Dubinets - MSFT





PostPosted: XML and the .NET Framework, I want to program multi-language winform software with C# and xml. how can I do Top

Can you be more concrete

Use de**** to narrow down the problem.