Click here to Skip to main content
13,352,280 members (74,500 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 16 Oct 2012

Microsoft Translator API with Spy++ logic

, 18 Oct 2012
Rate this:
Please Sign up or sign in to vote.
Using the Microsoft Translator API with Spy++ logic.


With the help of Microsoft Translator API with Spy++ logic, you can just drag the Spy++ finder control on the control's label whose text you want to translate. Use translate functionality and view the change immediately. This is helpful while you are coding in one language and want to test the label text at runtime in some other language


Here Microsoft Translator API is used which is free if usage is very low. The user has to obtain his own Id and secret key from Microsoft as per his needs. Below is the link where you will get these things. Don't worry you will get it free of cost (remember to choose free subscription): 

Using the code 

For capturing current window handle from Mouse Pointer location, below code is used:

// capture the window under the cursor's position
IntPtr hWnd = Win32.WindowFromPoint(Cursor.Position);

From the selected control, control text is extracted which will be our text input to translate. 

As mentioned above, obtain your Id and Secret Key from Microsoft and add it in App config file.

<?xml version="1.0"?>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
    <add key="MicrosoftTranslatorClientID" value="" />
    <add key="MicrosoftTranslatorSecretID" value="" />

Choose languages to translate to and translate from and click on Translate.

Below is the main function which translates the text: 

private static string Translate(string authToken, 
               string strTextToConvert, string strLanFrom, string strLangTo)
    string languageDetected = string.Empty;
    //Keep appId parameter blank as we are sending access token in authorization header.
    string uri = "" + 
       strTextToConvert + "&from=" + strLanFrom + "&to=" + strLangTo;
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
    httpWebRequest.Headers.Add("Authorization", authToken);
    WebResponse response = null;
        response = httpWebRequest.GetResponse();
        using (Stream stream = response.GetResponseStream())
            System.Runtime.Serialization.DataContractSerializer dcs = 
              new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));
            languageDetected = (string)dcs.ReadObject(stream);
        if (response != null)
            response = null;
    return languageDetected;

Now just click on view the change and your translated text will be appeared on control at runtime: 

static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);
public static extern int SendMessage(IntPtr hWnd, int msg, int Param, string s);

private void SetTextt(IntPtr hWnd, string text)
   // IntPtr boxHwnd = GetDlgItem(hWnd, 114);
   // hWnd=(IntPtr) Int64.Parse(textBox1.Text, System.Globalization.NumberStyles.HexNumber);
    SendMessage(hWnd, WM_SETTEXT, 0, text);


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


About the Author

Software Developer
India India
Hi! This is Tahir Shaikh. I am working as a software developer having experience of around 4 years in India. Interested in Microsoft technologies.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 18 Oct 2012
Article Copyright 2012 by tahirshaikh23Dec
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid