Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#3.5

Microsoft Translator API with Spy++ logic

0.00/5 (No votes)
18 Oct 2012CPOL1 min read 8.6K   242  
Using the Microsoft Translator API with Spy++ logic.

Introduction 

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

Background 

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):  

http://msdn.microsoft.com/en-us/library/hh454950.aspx 

Using the code 

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

C#
// 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
<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
<appSettings>
    <add key="MicrosoftTranslatorClientID" value="" />
    <add key="MicrosoftTranslatorSecretID" value="" />
</appSettings>
</configuration> 

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

Below is the main function which translates the text: 

C#
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 = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + 
       strTextToConvert + "&from=" + strLanFrom + "&to=" + strLangTo;
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
    httpWebRequest.Headers.Add("Authorization", authToken);
    WebResponse response = null;
    try
    {
        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);
            
        }
    }
    catch
    {
        throw;
    }
    finally
    {
        if (response != null)
        {
            response.Close();
            response = null;
        }
    }
    return languageDetected;
}

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

C#
[DllImport("user32.dll")]
static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, int Param, string s);

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

License

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