2023-04-28 12:21:24 +08:00

200 lines
8.4 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace LanguageExtractTool
{
public partial class LanguageMerge : Form
{
public LanguageMerge()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (ofd.ShowDialog() == DialogResult.OK)
{
txt_src.Text = ofd.FileName;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txt_src.Text))
{
MessageBox.Show("请选择源文件");
return;
}
XmlDocument doc = new XmlDocument();
doc.Load(txt_src.Text);
foreach (XmlNode node in doc.DocumentElement.ChildNodes)
{
if (string.Equals(node.Name, "Category", StringComparison.OrdinalIgnoreCase))
{
foreach (XmlNode pcode in node.ChildNodes)
{
MergeLanuageItem li = new MergeLanuageItem();
li.PageID = new NodeValueItem() { Name = pcode.Name, Text = pcode.InnerText };
foreach (XmlNode lancode in pcode.ChildNodes)
{
if (string.IsNullOrWhiteSpace(lancode.InnerText))
{
continue;
}
string lgid = lancode.Name;
string lgvalue = lancode.InnerText;
if (string.Compare(lgid, "en-us", true) == 0)
{
li.English = new NodeValueItem() { Name = "en", Text = lgvalue };
li.EnglishUS = new NodeValueItem() { Name = lgid, Text = lgvalue };
}
else if (string.Compare(lgid, "en-au", true) == 0)
li.EnglishAU = new NodeValueItem() { Name = lgid, Text = lgvalue };
else if (string.Compare(lgid, "en-ca", true) == 0)
li.EnglishCA = new NodeValueItem() { Name = lgid, Text = lgvalue };
else if (string.Compare(lgid, "fr-fr", true) == 0)
{
li.FranceNew = new NodeValueItem() { Name = "fr", Text = lgvalue };
li.France = new NodeValueItem() { Name = lgid, Text = lgvalue };
}
else if (string.Compare(lgid, "fr-ca", true) == 0)
li.FranceCA = new NodeValueItem() { Name = lgid, Text = lgvalue };
else if (string.Compare(lgid, "zh-cn", true) == 0)
{
li.Chinese = new NodeValueItem() { Name = lgid, Text = lgvalue };
}
}
XmlNode pagenode = doc.GetElementsByTagName(li.PageID.Name)[0];
XmlElement frel = doc.CreateElement("fr");
frel.InnerText = li.FranceNew.Text;
pagenode.PrependChild(frel);
XmlElement enel = doc.CreateElement("en");
enel.InnerText = li.English.Text;
pagenode.PrependChild(enel);
try
{
string rst = doc.InnerXml;
var url = "../" + li.PageID.Name + "/";
if (li.EnglishUS != null && string.Compare(li.English.Text, li.EnglishUS.Text, true) == 0)
{
var lnode = pagenode.SelectSingleNode(url + li.EnglishUS.Name);
pagenode.RemoveChild(lnode);
}
if (li.EnglishAU != null && string.Compare(li.English.Text, li.EnglishAU.Text, true) == 0)
{
var lnode = pagenode.SelectSingleNode(url + li.EnglishAU.Name);
pagenode.RemoveChild(lnode);
}
if (li.EnglishCA != null && string.Compare(li.English.Text, li.EnglishCA.Text, true) == 0)
{
var lnode = pagenode.SelectSingleNode(url + li.EnglishCA.Name);
pagenode.RemoveChild(lnode);
}
if (li.France != null)
{
var lnode = pagenode.SelectSingleNode(url + li.France.Name);
pagenode.RemoveChild(lnode);
}
if (li.FranceCA != null)
{
var lnode = pagenode.SelectSingleNode(url + li.FranceCA.Name);
pagenode.RemoveChild(lnode);
}
}
catch (Exception ex)
{
var name = li.PageID.Name;
}
}
}
}
doc.Save(txt_src.Text);
MessageBox.Show("合并完成。");
}
private void button4_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txt_src.Text))
{
MessageBox.Show("请选择源文件");
return;
}
XmlDocument doc = new XmlDocument();
doc.Load(txt_src.Text);
foreach (XmlNode node in doc.DocumentElement.ChildNodes)
{
if (string.Equals(node.Name, "Category", StringComparison.OrdinalIgnoreCase))
{
foreach (XmlNode pcode in node.ChildNodes)
{
MergeLanuageItem li = new MergeLanuageItem();
li.PageID = new NodeValueItem() { Name = pcode.Name, Text = pcode.InnerText };
foreach (XmlNode lancode in pcode.ChildNodes)
{
if (string.IsNullOrWhiteSpace(lancode.InnerText))
{
continue;
}
string lgid = lancode.Name;
string lgvalue = lancode.InnerText;
//if (string.Compare(lgid, "en", true) == 0)
//{
// li.English = new NodeValueItem() { Name = lgid, Text = lgvalue };
//}
if (string.Compare(lgid, "en-Components", true) == 0)
{
pcode.RemoveChild(lancode);
break;
}
}
//XmlNode pagenode = doc.GetElementsByTagName(li.PageID.Name)[0];
//XmlElement enel = doc.CreateElement("en-Components");
//enel.InnerText = li.English.Text;
//pagenode.AppendChild(enel);
}
}
}
doc.Save(txt_src.Text);
MessageBox.Show("添加完成。");
}
}
class MergeLanuageItem
{
public NodeValueItem PageID { get; set; }
public NodeValueItem English { get; set; }
public NodeValueItem EnglishUS { get; set; }
public NodeValueItem EnglishCA { get; set; }
public NodeValueItem EnglishAU { get; set; }
public NodeValueItem EnglishCOM { get; set; }
public NodeValueItem FranceNew { get; set; }
public NodeValueItem France { get; set; }
public NodeValueItem FranceCA { get; set; }
public NodeValueItem Chinese { get; set; }
}
class NodeValueItem
{
public string Name { get; set; }
public string Text { get; set; }
}
}