Click here to Skip to main content
15,882,017 members
Articles / Web Development / ASP.NET

Multi-lingual Resource file generator using the Google Translation API

Rate me:
Please Sign up or sign in to vote.
4.86/5 (16 votes)
23 Apr 2009CPOL2 min read 66.5K   2.1K   45   10
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.

C#
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.

C#
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:

Image 1

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
India India
Anees is working as Sr. Team Lead based in Delhi(India).He is Post graduated in Computer applications and science.

He is having around 11 years of design,analysis and coding experience in Sharepoint, ASP.NET, C#, VB.NET, SQL Server 2000/05, Reporting Services,Analysis Services,VB 6.0 and Crystal Reports.

Comments and Discussions

 
Generalvery interesting Pin
Jeff Circeo27-Apr-09 21:39
Jeff Circeo27-Apr-09 21:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.