Over a million developers have joined DZone.

Automatic Resource File Translation via Google Translate

One of the features that I support in WeBlog is localization. That means that all the labels, buttons and text within the application are stored in a resource file. So if you switch from English to Spanish, the text will be automatically displayed in the right language. For more information about how this magic happens, read this article.

Since I am only fluent in English I depend on Google translator to make the translations for me. After about 10 minutes of manually translating a spanish resource file, I decided that I needed a way to automate the process. I did a quick Google search and discovered that the translator service utilizes a JSON based API. A little more searching and I found a class library written by Alex Meyer-Gleaves which takes care of making the service calls to the translator API and parsing the results. So the only work I had to do was read the resource file, invoke the translator via Alex’s C# library and save the translated data to a new resource file.

Turns out that reading a resource file is extremely easy in C#. You just use the ResXResourceReader class, get an enumerator and iterate over the file. I put my results in a dictionary so it would be easy to iterate over the values later on.

private Dictionary<string, string> ReadResourceValues(string filename)
    var results = new Dictionary<string, string>();
    // Create a ResXResourceReader for the file items.resx.
    ResXResourceReader rsxr = new ResXResourceReader(filename);

    // Create an IDictionaryEnumerator to iterate through the resources.
    IDictionaryEnumerator id = rsxr.GetEnumerator();

    // Iterate through the resources and display the contents
    foreach (DictionaryEntry d in rsxr)
        results.Add(d.Key.ToString(), d.Value.ToString());

    return results;

Now that the resource file has been read I just need to translate the values. Again, thanks to Alex, The translation service calls are extremely simple. Here is the code that I used to invoke the translator service and add the results to a data table. Just for your information, the Translate method generates an Http request, de-serializes the results (a JSON object ) and populates an object so we can easily consume the results:

DataTable table = new DataTable();
table.Columns.Add("Source Key");
table.Columns.Add("Source Value");

var resource = ReadResourceValues(txtResourceFile.Text);

foreach (string key in resource.Keys){
    var row = table.NewRow();
    row[0] = key;
    row[1] = resource[key];

        TranslationResponse response = Google.Translate(resource[key], Language.Unknown,
        row[2] = response.ResponseData.TranslatedText;
    catch {
        row[2] = "?";

grid.DataSource = table;

Anyway, here is the end result. If you are fluent in Spanish then you will probably not completely agree with all of the translations shown in the screenshot below. In any case, this utility is a quick way to get the translation started. For English only speaking geeks like me, this utility is a life saver.


Download the Source Code - GoogleTranslator.zip (499.94 kb)


Published at DZone with permission of Michael Ceranski, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}