Click here to Skip to main content
15,392,399 members
Articles / General Programming / Localization
Posted 20 Aug 2012


43 bookmarked

Automatically localizing applications with Google Translate

Rate me:
Please Sign up or sign in to vote.
4.86/5 (15 votes)
21 Aug 2012CPOL4 min read
Localizing your apps with Google Translate


If you're like me, localizing your applications is a tedious task that is often skipped when possible and left to the last possible moment when needed. Once I get to the localization task, I open my browser on one screen and go to and begin to copy paste, copy paste, and copy paste some more. It's not that this is a difficult task, but boy it would be nice if there was a way to automatically do these translations; that is the purpose behind this project.

How the code works

As you are probably aware, most development languages use a simple file to hold the various words and phrases that are localized within the application. In the case of .NET these are within a .resx file which is just an XML file. In Java this is typically done via a .properties file.

The basics of how the application works are:

You provide:

  • The source resource file
  • The name of the desired resource file to create
  • The source and destination languages  

The application then:

  • Copies the source file to the new destination file
  • Determines if the file type is a .NET .resx or a Java .properties file based on the file extension (we need to know in order to determine how to iterate through the file)
  • We then iterate through each setting, translate it to the desired output language, and update the value in the file.
    • Note, in the case of the .NET .resx files I provided the option to only translate nodes where the name attribute ends with .Text. This helps avoid the translation of other things in the file such as control names and types that you may not want translated.

Although there are a bunch of moving parts here, the main piece I'd like to look at is the translatePhrase method in the Localizer class.

private string translatePhrase(string phrase, string sourceLangCode, string destLangCode)
    string translatedText = null;

        //create the web client
        WebClient client = new WebClient();

        //Open the page and get the results
        string url = c_GoogleTranslateUrl + sourceLangCode + "|" + destLangCode + "&text=" + phrase;
        string sPage = client.DownloadString(url);

        // Parse as the page as a string
        //  Page can have bad HTML causing problems if you try to parse as xml
        //  Find the span with the title of the original string
        int tagStart = sPage.IndexOf("<span title=\"" + phrase + "\"");
        int tagEnd = sPage.IndexOf("</span>", tagStart);
        string resultsTag = sPage.Substring(tagStart, (tagEnd - tagStart));
        //get rid of the start tag
        resultsTag = resultsTag.Substring(resultsTag.IndexOf(">") + 1);

        //You now have the translated text
        translatedText = resultsTag.Trim();                

        //dispose of the web client
    catch(Exception err)
        throw new Exception("Failed to download results from Google Translator.\r\n" + err.Message);

    return translatedText;

This method is where the magic happens. A web client object is created and opens a connection to a URL for Google translator for the specified phrase and languages. The returned page is then parsed as a string.

I originally was simply going to parse it as an XML document but found in many cases the tags can be malformed (missing ", etc.) causing parsing errors. Parsing the page as a string definitely isn't as pretty as dealing with it as XML, but it does the job.  

Using the code

If you'd like to try the code, either install the application or run it from source. I've provided a sample C# and Java application that you can use to try the app out. The steps below show you how translate the included C# sample application.

  1. Notice in the provided sample C# app, I have already enabled localization on the main form, and also provided a second resource file (ResourceFile.resx) that stores other message strings that might be translated.  You'll also notice in the Program.cs I am explicitly setting the language of the application to Spanish.
  2. Image 1

  3. Launch the sample application; notice that none of the strings are translated. This is because no translated resource files are provided.
  4. Select the Form1.resx as your source file. Set the output file as (this is the naming .NET is expecting for a Spanish version of the resource file).
  5. Since this resource file contains other strings we don't want to translated (object names, types, etc.), select the "For .resx files only translate .Text properties".
  6. Set the source language as English and the Output language as Spanish.
  7. Image 2

  8. After running the translation, you will notice the message "Completed translation".
  9. Translation complete

  10. Now change the source file to the provided ResourceFile.resx. The output file should be set to
  11. Because this file only contains resource strings we have added, uncheck the "For .resx files only translate .Text properties".
  12. Image 4

  13. Once you have translated both resource files you will need to add them into the project using the add existing item option.
  14. Image 5

    Image 6

  15. Compile and run the project again. Notice all of the strings and messages are now translated into Spanish.
  16. Translated application

Points of Interest

Google does offer a web service that provides that ability to programmatically translate strings, however this does require a purchase of a license where this application allows for the use of the web site for free. 

I have been using Google Translate and other similar services for years to manually translate resource files and as several of you pointed out this does have its flaws. Translating single words and phrases looses context and can result in an translation that is sometimes may not be contextually correct. For this reason as well as other possible UI adjustments (e.g. adjusting the size of controls to fit translated text) QA should be done on the translated text. This application just gives developers like myself a quick way of producing the first run string translations.


  • 08/20/2012:  
    • Initial creation and publishing of the code and article.
  • 08/21/2012: 
    • Minor updates to code (modified namespace, added image in installer)
    • Added section in Points of Interest about translation context


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Chip VonBurg
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

QuestionConversion problem - the localizer crashes Pin
Jawwad Muzaffar4-Aug-14 0:51
MemberJawwad Muzaffar4-Aug-14 0:51 
AnswerRe: Conversion problem - the localizer crashes Pin
Member 1133021922-Dec-14 22:20
MemberMember 1133021922-Dec-14 22:20 
Questiondoesn't work - waste of time Pin
Usha Pini22-Mar-14 0:14
MemberUsha Pini22-Mar-14 0:14 
AnswerRe: doesn't work - waste of time Pin
Chip VonBurg1-May-14 13:11
MemberChip VonBurg1-May-14 13:11 
QuestionRegarding complete translation of web site Pin
MukeshSagar28-Oct-13 19:41
MemberMukeshSagar28-Oct-13 19:41 
AnswerRe: Regarding complete translation of web site Pin
Chip VonBurg13-Nov-13 9:48
MemberChip VonBurg13-Nov-13 9:48 
QuestionTranslate whole solution Pin
pinx27-Aug-12 1:55
Memberpinx27-Aug-12 1:55 
QuestionWhy same work again? Pin
Kumar, Ravikant26-Aug-12 21:40
MemberKumar, Ravikant26-Aug-12 21:40 
GeneralA good idea but Google translation needs assistance... Pin
Matt T Heffron21-Aug-12 8:35
professionalMatt T Heffron21-Aug-12 8:35 
GeneralRe: A good idea but Google translation needs assistance... Pin
Gary Wheeler27-Aug-12 7:14
MemberGary Wheeler27-Aug-12 7:14 
GeneralMy vote of 5 Pin
Christian Amado21-Aug-12 5:04
professionalChristian Amado21-Aug-12 5:04 
QuestionAlso see... Pin
Ravi Bhavnani21-Aug-12 1:38
professionalRavi Bhavnani21-Aug-12 1:38 
AnswerRe: Also see... Pin
Tom Clement21-Aug-12 4:52
professionalTom Clement21-Aug-12 4:52 
Questiongood idea but won't work well with say Chinese language Pin
devvvy20-Aug-12 22:56
Memberdevvvy20-Aug-12 22:56 
AnswerRe: of course it will Pin
Axel Rietschin21-Aug-12 8:38
professionalAxel Rietschin21-Aug-12 8:38 
GeneralRe: of course it will Pin
devvvy21-Aug-12 14:37
Memberdevvvy21-Aug-12 14:37 

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.