using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel.Design;
using System.IO;
using System.Linq;
using System.Resources;
using System.Text;
using System.Text.RegularExpressions;
using ClosedXML.Excel;
namespace ResxConv
{
class Program
{
staticvoid Main(string[] args)
{
string path = @".";
string pattern = "message";
ConvResxToExcel(path, pattern);
ConvExcelToResx(@"FixedMessage.xlsx");
}
publicclass ResxStrings
{
publicstring Key;
publicstring Comment;
public Dictionary<string, string> Strings =
new Dictionary<string, string>();
}
//REF: http://msdn.microsoft.com/en-us/library/system.resources.resxdatanode.aspx
privatestaticvoid ConvExcelToResx(string xlsxPath)
{
var list = new List<ResxStrings>();
var langs = new List<string>();
using (XLWorkbook wb = new XLWorkbook(xlsxPath))
{
var sht = wb.Worksheets.First();
int col = 3;
while (!sht.Cell(1, col).IsEmpty())
{
langs.Add(sht.Cell(1, col).Value.ToString());
col++;
}
int row = 2;
while (!sht.Cell(row, 1).IsEmpty())
{
ResxStrings data = new ResxStrings()
{
Key = sht.Cell(row, 1).Value.ToString(),
Comment = sht.Cell(row, 2).Value.ToString()
};
for (int i = 0; i < langs.Count; i++)
data.Strings.Add(langs[i],
sht.Cell(row, i + 3).Value.ToString());
list.Add(data);
row++;
}
}
//Gen resx
string path = Path.GetDirectoryName(xlsxPath);
string pattern = Path.GetFileNameWithoutExtension(xlsxPath);
foreach (string lang in langs)
{
string resxPath = Path.Combine(path,
pattern + (lang != "DEFAULT" ? "." + lang : string.Empty) + ".resx");
using (ResXResourceWriter rsxw = new ResXResourceWriter(resxPath))
{
foreach (var data in list)
{
ResXDataNode node = new ResXDataNode(data.Key, data.Strings[lang]);
node.Comment = data.Comment;
rsxw.AddResource(node);
}
rsxw.Generate();
rsxw.Close();
}
}
}
privatestaticvoid ConvResxToExcel(string path, string pattern)
{
OrderedDictionary dict = new OrderedDictionary();
List<string> langs = new List<string>();
foreach (string file in Directory.GetFiles(path, pattern + ".*resx"))
{
string lang = "DEFAULT";
var m = Regex.Match(file, pattern + "[.](?<l>.+)[.]resx", RegexOptions.IgnoreCase);
if (m.Success) lang = m.Groups["l"].Value;
langs.Add(lang);
using (ResXResourceReader rsxr = new ResXResourceReader(file))
{
rsxr.UseResXDataNodes = true;
ResourceSet rs = new ResourceSet(rsxr);
foreach (DictionaryEntry entry in rs)
{
string key = (string)entry.Key;
var node = (ResXDataNode)entry.Value;
if (!dict.Contains(key))
{
dict.Add(key,
new ResxStrings() { Key = key, Comment = node.Comment });
}
var data = (ResxStrings)dict[key];
stringvalue = (string)node.GetValue((ITypeResolutionService)null);
data.Strings.Add(lang, value);
}
}
}
//Export to Excel
using (XLWorkbook wb = new XLWorkbook())
{
//Excel
var sht = wb.Worksheets.Add("Resx List");
sht.Cell(1, 1).Value = "Key";
sht.Cell(1, 2).Value = "Comment";
for (int i = 0; i < langs.Count; i++)
sht.Cell(1, i + 3).Value = langs[i];
//HTML
StringBuilder sb = new StringBuilder();
sb.AppendFormat(@"
<html>
<head>
<title>{0} RESX</title>
<style>
table {{ border-collapse:collapse; }}
td,th {{ border: 1px solid gray; padding: 6px; font-size: 9pt; }}
</style>
</head>
", pattern);
sb.Append("<body><table><tr><th>Key</th><th>Comment</th>");
foreach (string lang in langs)
sb.AppendFormat("<th>{0}</th>", lang);
sb.AppendLine("</tr>");
int row = 2;
foreach (DictionaryEntry de in dict)
{
//Excel
sht.Cell(row, 1).Value = de.Key;
var data = (ResxStrings)de.Value;
sht.Cell(row, 2).Value = data.Comment;
for (int i = 0; i < langs.Count; i++)
{
string lang = langs[i];
if (data.Strings.ContainsKey(lang))
sht.Cell(row, i + 3).Value = data.Strings[lang];
}
row++;
//HTML
sb.AppendFormat("<tr><td>{0}</td>", de.Key);
sb.AppendFormat("<td>{0}</td>", data.Comment);
for (int i = 0; i < langs.Count; i++)
{
string lang = langs[i];
sb.AppendFormat("<td>{0}</td>",
data.Strings.ContainsKey(lang) ? data.Strings[lang] : string.Empty);
}
sb.AppendLine("</tr>");
}
//Excel
sht.Column(1).AdjustToContents();
sht.Column(1).Width += 2;
wb.SaveAs(Path.Combine(path, pattern + ".xlsx"));
//HTML
sb.AppendLine("</table></html>");
File.WriteAllText(Path.Combine(path, pattern + ".html"), sb.ToString());
}
}
}
}