Use Google Cloud Translation with C#

12/14/2019

Use Google Cloud Translation with C#

  1. Follow the guide to set up an account.
  2. Download the private key as JSON and save it under folder Assets (e.g. API Project-xxxxxxx.json).  IMPORTANT: set its Build Action as Embedded resource.
  3. Install NuGet package Google.Cloud.Translation.V2.
  4. Create TranslationClient 

TranslationClient _tc;

GoogleCredential gc = GoogleCredential.FromStream(Utility.GetStreamFromResourceFile("API Project-xxxxxxx.json", GetType()));

_tc = TranslationClient.Create(gc);

You can traslate like the following example that translates "Text to be translated" from German to English:

 string sTranslatedText = _tc.TranslateText("Text to be translated", "en", "de").TranslatedText;