2024-03-26 15:56:31 +08:00

166 lines
5.7 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;
using System.IO;
using Newtonsoft.Json;
namespace LanguageExtractTool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//this.txt_src.Text = @"E:\IronIntel\Contractor2.0\Language\contractor_res.xml";
//this.txt_destdir.Text = @"E:\IronIntel\Contractor2.0\Contractor\Site\Languages";
}
private void button1_Click(object sender, EventArgs e)
{
if (ofd.ShowDialog() == DialogResult.OK)
{
txt_src.Text = ofd.FileName;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (fbd.ShowDialog() == DialogResult.OK)
{
txt_destdir.Text = fbd.SelectedPath;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txt_src.Text))
{
MessageBox.Show("请选择源文件");
return;
}
if (string.IsNullOrWhiteSpace(txt_destdir.Text))
{
MessageBox.Show("请选择要存放资源文件的文件夹");
return;
}
string destpath = txt_destdir.Text.Trim();
XmlDocument doc = new XmlDocument();
doc.Load(txt_src.Text);
Dictionary<string, ResourceObject> docs = GetResources();
foreach (XmlNode node in doc.DocumentElement.ChildNodes)
{
if (string.Equals(node.Name, "Category", StringComparison.OrdinalIgnoreCase))
{
foreach (XmlNode codenode in node.ChildNodes)
{
string code = codenode.Name;
foreach (var d in docs)
{
string lgid = d.Key;
XmlNode node1 = codenode[lgid];
if (node1 == null)
{
var lgids = lgid.Split('-');
node1 = codenode[lgids[0]];
}
if (node1 != null)
d.Value.Values[code] = node1.InnerText;
}
}
}
}
foreach (var kv in docs)
{
string fn = Path.Combine(txt_destdir.Text.Trim(), kv.Key + ".json");
string s = JsonConvert.SerializeObject(kv.Value);
using (FileStream fs = new FileStream(fn, FileMode.Create, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
{
sw.Write(s);
}
}
}
MessageBox.Show("分解完成。");
}
private Dictionary<string, ResourceObject> GetResources()
{
Dictionary<string, ResourceObject> docs = new Dictionary<string, ResourceObject>(StringComparer.OrdinalIgnoreCase);
ResourceObject resen = new ResourceObject();
resen.Ver = Convert.ToInt64(DateTime.Now.ToString("yyMMddHHmm"));
resen.LanguageId = "en";
docs.Add("en", resen);
ResourceObject resfr = new ResourceObject();
resfr.Ver = Convert.ToInt64(DateTime.Now.ToString("yyMMddHHmm"));
resfr.LanguageId = "fr";
docs.Add("fr", resfr);
ResourceObject res1 = new ResourceObject();
res1.Ver = Convert.ToInt64(DateTime.Now.ToString("yyMMddHHmm"));
res1.LanguageId = "en-us";
docs.Add("en-us", res1);
ResourceObject res2 = new ResourceObject();
res2.Ver = Convert.ToInt64(DateTime.Now.ToString("yyMMddHHmm"));
res2.LanguageId = "en-ca";
docs.Add("en-ca", res2);
ResourceObject res3 = new ResourceObject();
res3.Ver = Convert.ToInt64(DateTime.Now.ToString("yyMMddHHmm"));
res3.LanguageId = "en-au";
docs.Add("en-au", res3);
ResourceObject res4 = new ResourceObject();
res4.Ver = Convert.ToInt64(DateTime.Now.ToString("yyMMddHHmm"));
res4.LanguageId = "en-Components";
docs.Add("en-Components", res4);
ResourceObject res5 = new ResourceObject();
res5.Ver = Convert.ToInt64(DateTime.Now.ToString("yyMMddHHmm"));
res5.LanguageId = "fr-fr";
docs.Add("fr-fr", res5);
ResourceObject res6 = new ResourceObject();
res6.Ver = Convert.ToInt64(DateTime.Now.ToString("yyMMddHHmm"));
res6.LanguageId = "fr-ca";
docs.Add("fr-ca", res6);
ResourceObject res7 = new ResourceObject();
res7.Ver = Convert.ToInt64(DateTime.Now.ToString("yyMMddHHmm"));
res7.LanguageId = "zh-cn";
docs.Add("zh-cn", res7);
ResourceObject res8 = new ResourceObject();
res8.Ver = Convert.ToInt64(DateTime.Now.ToString("yyMMddHHmm"));
res8.LanguageId = "pt";
docs.Add("pt", res8);
return docs;
}
public class ResourceObject
{
public long Ver { get; set; } = 100;
public string LanguageId { get; set; } = "en-us";
public Dictionary<string, string> Values { get; private set; } = new Dictionary<string, string>();
}
}
}