Sunday 15 February 2015

Consolidate multiple resource files in single excel file

Recently I ran into an interesting requirement. We are working on a large code base (more than 100 projects in single solution...yeah i know what are going to say but lets save that for another day). Most of the projects have their own set of resource strings that are used for storing localization-ready string messages. During the course of the development, the count of resource files grew big (50+) and we decided to consolidate it. In order to achieve that, we needed to identify and root out the duplicate messages. As many of my colleagues believe, I too believe in the power of Excel to do stuff like this. So i wrote a simple function that does following: 

1. Consolidate all the resource file messages into a data table
2. Export the data table to a XML file 
3. Manual (can be automated) Open the XML file in Excel 
4. Save the Excel file to native excel format.

That's it!! You can now slice and dice the messages however you want.

Here is the simple function:

static void Main(string[] args)
{
    string dropLocation = @"D:\Dump\";

    DirectoryInfo dirInfo = new DirectoryInfo(@"D:\Application\");
    var resourceFiles = dirInfo.EnumerateFiles("*.resx", SearchOption.AllDirectories);

    DataTable dt = new DataTable("resources");
    dt.Columns.Add(new DataColumn("FilePath", typeof(string)));
    dt.Columns.Add(new DataColumn("Key", typeof(string)));
    dt.Columns.Add(new DataColumn("Value", typeof(string)));

    foreach (var resourceFile in resourceFiles)
    {
        XDocument xDoc = XDocument.Load(resourceFile.FullName);
        xDoc.Document.Descendants()
                    .Where(x => x.Name == "data")
                    .Each(x =>
                    {
                        var row = dt.NewRow();
                        row.ItemArray = new object[3] {resourceFile.FullName, x.Attributes().ToList()[0].Value, x.Descendants().ToList()[0].Value }; 
                        dt.Rows.Add(row);
                    });
    }

    dt.WriteXml(dropLocation + "excel.xml");

    Console.ReadLine();
}