Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Multi-lingual Resource file generator using the Google Translation API

0.00/5 (No votes)
23 Apr 2009 1  
This is a command line utility for generating Japanese and French resource files from English using the Google Translation API.

Introduction

This utility is developed to generate Japanese and French resource files from default resource files of an ASP.NET web page or user control. This utility is for translating resource files using the Google translation API.

Before running this utility, create a folder "conversion" in your c: drive and put all your default resource files (.resx) within the folder.

Background

Couple of days back, my team was working on converting my applications for multiple languages (English, French, and Japanese) to address audiences of different regions. Enabling an ASPX page and user control (.ascx) for multiple languages is very easy, and for doing so, we need to maintain resource files (*.resx) for each and every language, and easily generate resource file(s).

To generate a resource file, open the ASPX (or ASCX) page in Design mode, then go to Tools-->Generate Local resource. Now, we will have a default resource file, like Webform1.aspx.resx. Now we need to create Webform1.aspx.fr.resx for French and Webform1.aspx.ja.resx for Japanese.

For translating any string, go to the Google language translation page: http://translate.google.com/translate_t#.

I own around 40 applications, a lot of dashboards (user controls), and a huge portal application. For me, this would be a very tiresome method for language translation. So, I have developed a console utility for it. You have to create a folder "conversion" under your c drive and put all your default RESX files in it.

Code Discussion

I search for all RESX files in c:\Converion and create Japanese and French RESX files, and each file is translated by calling the Dotranslation function.

foreach (string file in files)
{
    Program _p = new Program();
    Console.WriteLine("Conversion of file started: {0} {1}", 
                      file, " to Japanese");
    _p.DoTranslation(Google.API.Language.Japanese, file);
    Console.WriteLine("Conversion of file completed.");
    Console.WriteLine("Conversion of file started: {0} {1}", 
                      file, " to French");
    _p.DoTranslation(Google.API.Language.French, file);
    Console.WriteLine("Conversion of file completed.");
}

In the DoTranslation function, I pass the language of type Google.API.Language and the default resource file. I open a .resx file by using XmlDocument and read all data nodes. Each data node again gets iterated to get the child nodes. A node value is translated using the Translate function of the Translate class of the Google API. I have attached GoogleTranslateAPI.dll, provided by Google-api-for-dotnet.

protected void DoTranslation(Google.API.Language language, string resxFilePath)
{
    XmlDocument doc = new XmlDocument();
    doc.Load(resxFilePath);
    XmlNodeList dataNodes = doc.SelectNodes("//root/data");

    using (ResXResourceReader rr = new ResXResourceReader(resxFilePath))
    {
       //New resx file
        string outputFilePath = resxFilePath.Replace("resx", "") + 
               language.ToString().Substring(0, 2).ToLower() + ".resx";

        using (ResXResourceWriter rw = new ResXResourceWriter(outputFilePath))
        {
            IDictionaryEnumerator di = rr.GetEnumerator();

            foreach (DictionaryEntry de in rr)
            {
                string key = de.Key as string;
                string value = de.Value as string;
                   //ignore emply keys
                if (!String.IsNullOrEmpty(key) && 
                    !String.IsNullOrEmpty(value))
                {

                    //translation using google translation api
                    string translatedValue = Translator.Translate(value, 
                           Google.API.Language.English, language);
                    rw.AddResource(key, translatedValue);
                }
            }
            rw.Generate();
        }
    }
}
Utility screenshot:

Points of Interest

This is a very small utility, but saves a lot of time when a large enterprise web application needs to be converted for multi-lingual support.

History

  • Uploaded on April 23, 2009.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here