Translation
In this article we are going to show you how to use Google's Translation API. The process of embedding the functionality in your .NET application is very easy once you have enabled the feature from your GCP console.
Step 1: Create the WPF Application
Create a standard WPF application and add 2 RadComboBoxes, 2 RadWaterMarkTextBoxes and a RadButton. The RadComboBoxes will provide a choice for source and target language of the translation, the RadWaterMarkTextBoxes will hold the text that has to be translated and its translation in the chosen language. The RadButton will make a call to Google's Translation API through a Command.
Example 1: Defining the view
Step 2: Install the NuGet package
Open the NuGet Package Manager and install the Google.Cloud.Translation.V2 package.
Step 3: Define the ViewModel
The next step is to create the ViewModel. It will need a TranslationClient object which will be used for calling the Translation API. We also need to implement the command that will call the Translation API via the TranslationClient.
Example 2: Defining the ViewModel
Note that the ItemsSource of the RadComboBoxes is a list of Language objects returned from the ListLanguages method of the TranslationClient. The Language objects have two important properties: Name and Code. The Name property is used as the value for the DisplayMemberPath property of the RadComboBoxes. The Code property on the other hand is stored in the SourceLanguageCode and TargetLanguageCode properties which are passed as parameters to the TranslateText method of the TranslationClient.
In order for the GoogleCredential.GetApplicationDefault method to get your credentials, you need to have a GOOGLE_APPLICATION_CREDENTIALS environment variable set pointing to the JSON file downloaded when creating a service account.
All that is left is to set the DataContext to the ViewModel.