Talk:Using Google Translate on Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search


Croozeus - Some comments

Hi molbal

Thanks for the article. I've tidied up the article and sub-edited it.

As a comment in general, I think it's worth a mention why YQL was used here, and Google translate APIs weren't used directly - perhaps because the Google translate APIs doesn't support both JSON and XML output? If there's no reason in particular, we can mention the possibility of using directly google APIs too! I think the introduction is the right place to mention this.

I did couple of edits, please check if OK and here are few comments:

  • I've changed the structure of the article to show the class implementation first and then the usage.
  • Added abstract and article metadata to the article. Could you upload a buildable code example as a zip file? This is so that developers can just download and see the code in action.
  • I understand that you've tested the code on WP8 ,but do you see any show stoppers for WP7? If not, I assume the code would work on WP7 too, and we can rename the article to "Using Google Translate in Windows Phone". What do you think?



croozeus 07:48, 12 February 2013 (EET)

Molbal - Thanks for tidying it up


Thank you for making the article more professional. I'm going to check the code on WP7, too. I'l also add, that "Newtonsoft.Json" is requied.

I choosed YQL, because it provides free access too the Google translate service, while Google's own APIs are paid only. Of course I'm going to pack it up and upload an example.

Thanks, Regards,


molbal 16:07, 12 February 2013 (EET)

Croozeus - Cool!

Hi Balint,

Thanks for the explanation re Google translate vs YQL APIs. I've added a note to the introduction to reflect this. Please check if OK.

Let me know after you've tested it on WP7, I'll rename the article. And would be great to have the buildable example uploaded for others to try.



croozeus 11:05, 13 February 2013 (EET)

Molbal - Added requied changes


I have added an exmaple, that shows the usage on Windows Phone 7. It has a minor modification, "System.Threading.Tasks" is no longer used, since it's only supported in WP8. I've also uploaded a WP7 project, and it works well. I forgot I used Json.Net (A free package available), so I placed a similar note block as You did with the YQL explanation. Could you please move the download link in an appropriate place and rename the article?

Thank you! Regards,


molbal 12:00, 13 February 2013 (EET)

Croozeus - Thank you!

Thanks for checking it for WP7. The Json.Net information is useful, thanks for adding. Just moved it to implementation section where we mention what files are required. I renamed the article to "Using Google Translate on Windows Phone".

Regarding the source code, I checked your Skydrive link - it has only a Speech Example. Did you upload the sample code there yet?

You can directly upload the zip to our wiki, so that you don't have to maintain it on your skydrive forever. Here's the upload link, http://www.developer.nokia.com/Community/Wiki/Special:Upload



croozeus 12:20, 13 February 2013 (EET)

Molbal - My bad!

I screwed it up. I am working on a speech-to-speech translation and made this class for that project. It was a reflex I gave that name insted of Translation example. That's the corrent file, I'll rename everything in it and then upload it to the wiki. (I didn't know about it yet)

Thanks for the link and taking care of my clumsiness. Regards,


molbal 12:27, 13 February 2013 (EET)

Molbal - Fixed

Hi Pankaj,

I have fixed the errors :) Regards,


molbal 16:43, 13 February 2013 (EET)

Croozeus - Awesome!

Hi Bálint,

That's great. Thanks very much for adding it. I added source-code link it to the ArticleMetaData as well.

Looking forward to your other contributions to the Wiki!



croozeus 08:26, 14 February 2013 (EET)

Takacs.albert - Code snippet for more than one sentences

Hi All,

Please find below the snippet for the case when multiple sentence translation is needed. Please replace the following line found in the original post: tsea.translatedString = ((((((((((((((((Newtonsoft.Json.Linq.JContainer)(obj)).First).First).Last).First).First).First).First).First).First).First).First).First).First).First).ToString();

To these lines: var sentences = ((((((((((((Newtonsoft.Json.Linq.JContainer)(obj)).First).First).Last).First).First).First).First).First).First).First).First; StringBuilder sb = new StringBuilder(); foreach (var item in sentences.Children()) {

   if (item != null)
       JToken sentence = null;
       if (item.First.First is JValue)
           // one sentence translated
           sentence = item.First.First;
           // multiple sentences translated
           sentence = item.First.First.First;
       if (sentence != null)

} tsea.translatedString = sb.ToString();

Hope this helps,

takacs.albert 21:46, 5 June 2013 (EEST)

Molbal - It helps for sure

Hi Albert,

It surely helps a lot! Thanks, I'll upgrade the article snippet soon.

molbal 21:54, 5 June 2013 (EEST)

Takacs.albert - Cool!


takacs.albert 21:56, 5 June 2013 (EEST)

Hamishwillee - Let us know when done

Hi Bálint

That will be great. Please add a note when you're done and I'll remove the comments next time I come round.



hamishwillee 09:48, 7 June 2013 (EEST)

Molbal - On Monday

Hi Hamish,

I'll update the article on Monday evening. (I have an exam where I have to battle the Devil itself: Theory of computing) :)



molbal 14:14, 7 June 2013 (EEST)

Hamishwillee - Thank you.

There is no urgency - just want to know when I can clean up. Good luck with your battle with evil :-)

hamishwillee 08:08, 10 June 2013 (EEST)

Paulo.morgado - Ading async/await support

Hi Bálint,

As I had posted in your other article I’ve played with your code and added async/await capabilities to it.

Instead of using the WebClient class, I’ll be using the HttpClient class that you can find in the Microsoft HTTP Client Libraries NuGet package.

I won’t be changing the meaning of your code as I don’t know the API you’re using.

By using async/await for writing the code, the code becomes easier to understand and maintain, as you can see:

public async Task<string> TranslateStringAsync(string to, string text)
var url = string.Format(
"select * from google.translate where q=\"{0}\" and target=\"{1}\";",
var client = new HttpClient();
var response = await client.GetStringAsync(url).ConfigureAwait(continueOnCapturedContext: false);
var json = JsonConvert.DeserializeObject(response) as JContainer;
var result = json.First.First.Last.First.First.First.First.First.First.First.First.First.First.First.ToString();
return result;

Noticed the .ConfigureAwait(continueOnCapturedContext: false)? This will instruct the compiler generated state machine to not post the continuation into the current SynchronizationContext, if one exists. This way, if the calling code is on the UI thread

And it becomes also easier to consume:

var t = new Translator();
var r = t.TranslateStringAsync("pt", "Good morning!").Result;

One of the reasons I used the HttpClient class instead of the WebClient class is because it’s easier to handle cancellation:

public async Task<string> TranslateStringAsync(string to, string text, CancellationToken cancellationToken)
var url = string.Format(
"select * from google.translate where q=\"{0}\" and target=\"{1}\";",
var client = new HttpClient();
var response = await client.GetAsync(url, cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
var responseText = await response.Content.ReadAsStringAsync();
var responseJson = JsonConvert.DeserializeObject(responseText) as JContainer;
var result = responseJson.First.First.Last.First.First.First.First.First.First.First.First.First.First.First.ToString();
return result;
public Task<string> TranslateStringAsync(string to, string text)
return TranslateStringAsync(to, text, CancellationToken.None);

And if you’d like to set a timeout for the call:

var c = new CancellationTokenSource(5000);
var t = new Translator();
var r = t.TranslateStringAsync("pt", "Good morning!", c.Token).Result;

paulo.morgado (talk) 12:47, 21 August 2013 (EEST)

Hamishwillee - Great comment

Hi Balint, Paulo

Paulo's comment looks like a good approach to me. Only issue I guess would be that for this still to work on WP7 you'd need to add the libraries (can't remember which) that add await/async to WP7. If it were me (and I hope you will restructure this) I'd probably redo using structure suggested by Paulo, then have a WP7 section. In this section you could explain that the above code will work with addition of libraries, X/Y, but that if you don't want that dependency then you can use old-style event handling (and then just have your current text).




hamishwillee (talk) 02:49, 22 August 2013 (EEST)

Paulo.morgado - WP7

It should work with WP7.

The documentation states that the Microsoft HTTP Client Libraries package is supported for:

   * .NET Framework 4
   * .NET for Windows Store apps
   * Windows Phone 7.5 and 8
   * Silverlight 4 and 5
   * Portable Class Libraries

It doesn't declare a dependency on the Async for .NET Framework 4, Silverlight 4 and 5, and Windows Phone 7.5 and 8 package, but it can be added if needed.



paulo.morgado (talk) 02:59, 22 August 2013 (EEST)

Takacs.albert - Translation service is failing

Is it possible that this service is not working anymore? I keep getting the following response string everytime: "{"query":{"count":0,"created":"2013-09-13T21:55:35Z","lang":"en-US","results":null}}"

Please help me out as this feature is in production and my users are not able to translate...



takacs.albert (talk) 00:49, 14 September 2013 (EEST)

Molbal - the service changed


I've been inactive for a while, I'll read the comments soon and then edit this comment :)

molbal (talk) 10:42, 21 February 2014 (EET)