Excel – Convert CSV file or Excel spreadsheet to RESX File

asp.netcsvexcelglobalization

I am looking for a solution or recommendation to a problem I am having. I have a bunch of ASPX pages that will be localized and have a bunch of text that needs to be supported in 6 languages.

The people doing the translation will not have access to Visual Studio and the likely easiest tool is Excel. If we use Excel or even export to CSV, we need to be able to import to move to .resx files. So, what is the best method for this?

I am aware of this question, Convert a Visual Studio resource file to a text file? already and the use of Resx Editor but an easier solution would be preferred.

Best Answer

I'm not sure how comprehensive an answer you're looking for, but if you're really just using [string, string] pairs for your localization, and you're just looking for a quick way to load resource (.resx) files with the results of your translations, then the following will work as a fairly quick, low-tech solution.

The thing to remember is that .resx files are just XML documents, so it should be possible to manually load your data into the resource from an external piece of code. The following example worked for me in VS2005 and VS2008:

namespace SampleResourceImport
{
    class Program
    {
        static void Main(string[] args)
        {

            XmlDocument doc = new XmlDocument();
            string filePath = @"[file path to your resx file]";
            doc.Load(filePath);
            XmlElement root = doc.DocumentElement;

            XmlElement datum = null;
            XmlElement value = null;
            XmlAttribute datumName = null;
            XmlAttribute datumSpace = doc.CreateAttribute("xml:space");
            datumSpace.Value = "preserve";

            // The following mocks the actual retrieval of your localized text
            // from a CSV or ?? document...
            // CSV parsers are common enough that it shouldn't be too difficult
            // to find one if that's the direction you go.
            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add("Label1", "First Name");
            d.Add("Label2", "Last Name");
            d.Add("Label3", "Date of Birth");

            foreach (KeyValuePair<string, string> pair in d)
            {
                datum = doc.CreateElement("data");
                datumName = doc.CreateAttribute("name");
                datumName.Value = pair.Key;
                value = doc.CreateElement("value");
                value.InnerText = pair.Value;

                datum.Attributes.Append(datumName);
                datum.Attributes.Append(datumSpace);
                datum.AppendChild(value);
                root.AppendChild(datum);
            }

            doc.Save(filePath);
        }
    }
}

Obviously, the preceding method won't generate the code-behind for your resource, however opening the resource file in Visual Studio and toggling the accessibility modifier for the resource will (re)generate the static properties for you.

If you're looking for a completely XML-based solution (vs. CSV or Excel interop), you could also instruct your translators to store their translated content in Excel, saved as XML, then use XPath to retrieve your localization info. The only caveat being the file sizes tend to become pretty bloated.

Best of luck.