Click here to Skip to main content
Click here to Skip to main content
Go to top

Multi-lingual Resource file generator using the Google Translation API

, 23 Apr 2009
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Aneesur Rehman Khan
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

 
QuestionReally very useful ...but How to use this for other languages PinmemberMember 102776468-Feb-14 4:54 
GeneralGoogle.Api.Translate option is not coming PinmemberRachna latiyan19-May-10 21:17 
Generalits giving English output for Spanish laguage PinmembervijendraAver12325-Sep-09 1:43 
GeneralRe: its giving English output for Spanish laguage PinmemberAneesur Rehman Khan25-Sep-09 1:54 
GeneralMy vote of 2 PinmemberPerttilä30-Apr-09 1:38 
GeneralRe: My vote of 2 PinmemberTroy Tamas7-May-12 19:10 
Generalvery interesting PinmemberJeffCirceo27-Apr-09 21:39 
Generalcool Pinmemberradioman.lt23-Apr-09 20:05 
nice idea Thumbs Up | :thumbsup:
 
peace & serenity

GeneralAgainst Google TOS PinmemberMichael Teper23-Apr-09 16:06 
GeneralWarning: quality PinmemberMihai Nita23-Apr-09 6:35 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 23 Apr 2009
Article Copyright 2009 by Aneesur Rehman Khan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid