Click here to Skip to main content
13,898,599 members
Click here to Skip to main content
Add your own
alternative version


96 bookmarked
Posted 26 Dec 2005

Automatically Translate your .NET resource files with Google Translate

, 27 Oct 2006
Rate this:
Please Sign up or sign in to vote.
This article talks about building an application to translate .NET resource files like .resx and .js using Google Translate, for internationalization testing.


With many more applications coming up with localized versions, it is important to test if the application is ready for that after moving the resources out of the code. We have easy ways of taking strings out of the code, using resx files, satellite DLLs, and external string files (a client side js file). A quick test would be to see if all the strings are picked up from an external file. One step further would be to make sure strings in other languages (the localized ones) are displayed properly in our application. As many point out, it is important to test with European and Asian languages that use multi-byte encoding. I started off with this testing, and thought that I will copy-paste some of the strings from the web on these languages to carry out the testing. On looking around the web, I got to know about Google Translate and I was excited, and I found a way to test my application in all languages that Google supports! As I started with it, oh, it was a pain... picking up strings one by one, translating them into the different languages .... I thought it would have been good if I had an automatic tool (there is Google for that to search and tell me!).

Google returned me with a result, a way of calling Google Translate from our application, a good article and accompanying source code by Peter A. Bromberg. I thought I got what I wanted and with two lines of code I could make it work for me, but it was not so, it made me explore so many other stuff and finally made me post here so that many others could benefit and also help me refine my first C# application.


I fully agree with Peter on "don't reinvent the wheel", having been with application development for over 7 years. I have gained most part of my knowledge through the web with those who had shared their two cents. I started with his code and ran it on my machine. Oops, I didn't get the result. On getting into debug mode, I found that the web scraper he was using, WebWagon, did not support proxies. Once again, it was Google time and I added the code below to use the default proxy authentication to get his code working:

//Set Proxy settings to the  HttpWebRequest object  
hrqURL.Proxy = (WebProxy) GlobalProxySelection.Select;
hrqURL.Proxy.Credentials = CredentialCache.DefaultCredentials;

Then I started off with my project, translating my resx file, and upgrading it with version after versions. The first version used direct XML parsing of the resx file as I was not aware of ResXResourceReader. The second version included support for strings in a js file also as some string resources were part of the client side script. The next version removed the XML parsing and used ResXResourceReader and ResXResourceWriter. Then, to avoid the screen hanging, I added thread support and a progress bar to the application, and I have attached that version here. I am still thinking of working on a version supporting Altavista's Babelfish also. (I am very well aware that this code will break once the Google Translate page or the Babelfish page changes, but I hope it won't be in the near future and always there will be some one to break it through and get it working!)

Using the code

I have used Peter's WebWagon project as it is other than the few tweaks to get it working for me. The first one was the proxy support as I said earlier. Asian characters were not getting returned properly in his code and that was the main requirement for me to go for this application. I did a lot of trial and error to get this piece of code into WebWagon so that it supported UTF-8 encoding:

//Set content type for the HTTPWebRequest object
hrqURL.Method = "GET";
hrqURL.ContentType = "application/x-www-form-urlencoded";
hrqURL.UserAgent = "Mozilla/4.0 (compatible;" + 
               " MSIE 6.0; Windows NT 5.1)";

Also, the encoding of the return data from the webpage was also missing. I added that to get the data encoded properly.

//Set content type for the Response Stream reader
StreamReader srdrInput = new 
  StreamReader(hrspURL.GetResponseStream(), Encoding.UTF8);

The project I have added is straightforward; for resx files, loop through the resource strings and call Translate using the ResxResourceReader, and for js, read line by line and call Translate.

// Create a ResXResourceReader for the file items.resx.
ResXResourceReader rsxr = new ResXResourceReader(fileName);
ResXResourceWriter rsxTranslated = new ResXResourceWriter(outputFileName);
// Create an IDictionaryEnumerator to iterate through the resources.
IDictionaryEnumerator id = rsxr.GetEnumerator(); 
long count=0;
object[] pct;
pct = new object[1];

// Iterate through the resources 
foreach (DictionaryEntry d in rsxr) 
    int percentTranslated = (int)(count*100/totalStrings);

    string strval = d.Value.ToString();
    string translatedtxt = TranslateString(strval);
    //Add translated string to the output resource files
    rsxTranslated.AddResource(d.Key.ToString(),translatedtxt ); 
//store back the resx file under the chosen language option

For getting the progress bar working, I had to do some additional coding, to get the total number of strings to be translated. For js files, I went through the file till the end once, to get all the strings. For resx, I thought using XML parsing would be much faster and used a piece of code I already had in my first version:

//To get the total no of resource strings to be translated (for
//progress bar) fastest way is to read resx as xml!
XmlDocument rootNode = new XmlDocument();
XmlNodeList dataNodes = rootNode.SelectNodes("//root/data"); 
int totalStrings = dataNodes.Count;

Points of interest

As my first application in C#, I got a chance to know more stuff about: web scraping, multithreading, XML parsing, and using different UI controls, and lot more!



  • V1.0 - Initial update on 26 Dec 2005.
  • 27 Oct 2006 - Fixed code to work with the new changes in the Google Translate page.


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


About the Author

Satish is an application developer working on many different technologies, VC6, VBA, CORBA, XML, ASP, ASP.NET and recently experimenting with C#. He has started looking into building rich UIs using flash, php, asp & javascript too.

You may also be interested in...

Comments and Discussions

SuggestionA better translator Pin
SalarSoft24-Apr-13 18:04
memberSalarSoft24-Apr-13 18:04 
QuestionMisc corrections and updates Pin
NinjaCross26-Sep-12 22:54
memberNinjaCross26-Sep-12 22:54 
QuestionCorrected code Pin
JeffSinNHUSA22-Dec-11 9:43
memberJeffSinNHUSA22-Dec-11 9:43 
Question2011 Fix ... Using Google mobile translate site instead. Pin
JanBorup30-Jun-11 11:52
memberJanBorup30-Jun-11 11:52 
Generalhi Pin
blnd_pirlo22-Nov-10 3:31
memberblnd_pirlo22-Nov-10 3:31 
GeneralThis method still works with minor revisions. Pin
Brian Gibson15-Nov-09 14:01
memberBrian Gibson15-Nov-09 14:01 
GeneralRe: This method still works with minor revisions. Pin
Sudheer Kumar K29-Nov-10 20:43
memberSudheer Kumar K29-Nov-10 20:43 
General[Bug Fix] because of changes in Pin
Sayed Mohamed4-Jun-09 6:10
memberSayed Mohamed4-Jun-09 6:10 
GeneralVery interesting. Pin
David Catriel16-Jan-09 9:48
memberDavid Catriel16-Jan-09 9:48 
GeneralC# 2008 version Pin
munk121-Dec-08 3:04
membermunk121-Dec-08 3:04 
QuestionHow to get This to Work (28-03-2008) Pin
Helder Almeida @ Apond28-Mar-08 2:18
memberHelder Almeida @ Apond28-Mar-08 2:18 
AnswerRe: How to get This to Work (28-03-2008) Pin
Alexander@AWSoft.nl27-May-08 5:07
memberAlexander@AWSoft.nl27-May-08 5:07 
QuestionFix is not working Pin
kleinmk24-Feb-08 12:47
memberkleinmk24-Feb-08 12:47 
GeneralRe: Fix is not working Pin
Brenda Lowe18-Mar-08 12:45
memberBrenda Lowe18-Mar-08 12:45 
GeneralTemporary Fix Pin
pat_mc_loughlin@yahoo.com7-Oct-07 23:59
memberpat_mc_loughlin@yahoo.com7-Oct-07 23:59 
GeneralRe: Temporary Fix Pin
j.channon1-Nov-07 6:44
memberj.channon1-Nov-07 6:44 
GeneralIt Stopped working Pin
HPKS29-Jul-07 23:01
memberHPKS29-Jul-07 23:01 
GeneralRe: It Stopped working Pin
Blachlock9-Aug-07 18:24
memberBlachlock9-Aug-07 18:24 
GeneralStopped Working Again Pin
Blachlock27-Jul-07 18:39
memberBlachlock27-Jul-07 18:39 
GeneralSupport for file structures and master database of techinical terms Pin
Jonathan Palmer23-Feb-07 7:23
memberJonathan Palmer23-Feb-07 7:23 
GeneralRe: Support for file structures and master database of techinical terms Pin
Satishkumar.B28-Feb-07 3:12
memberSatishkumar.B28-Feb-07 3:12 
QuestionQuit working ??? Pin
S.Badri15-Feb-07 20:20
memberS.Badri15-Feb-07 20:20 
AnswerRe: Quit working ??? Pin
Satishkumar.B19-Feb-07 2:45
memberSatishkumar.B19-Feb-07 2:45 
GeneralRe: Quit working ??? Pin
S.Badri21-Feb-07 16:40
memberS.Badri21-Feb-07 16:40 
GeneralRe: Quit working ??? Pin
Satishkumar.B28-Feb-07 3:10
memberSatishkumar.B28-Feb-07 3:10 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190306.1 | Last Updated 27 Oct 2006
Article Copyright 2005 by Satishkumar.B
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid