Quickstart: Use the Translator Text API to detect text language using C#
In this quickstart, you'll learn how to detect the language of provided text using .NET Core and the Translator Text REST API.
This quickstart requires an Azure Cognitive Services account with a Translator Text resource. If you don't have an account, you can use the free trial to get a subscription key.
Prerequisites
- .NET SDK
- Json.NET NuGet Package
- Visual Studio, Visual Studio Code, or your favorite text editor
- An Azure subscription key for Translator Text
Create a .NET Core project
Open a new command prompt (or terminal session) and run these commands:
dotnet new console -o detect-sample
cd detect-sample
The first command does two things. It creates a new .NET console application, and creates a directory named detect-sample
. The second command changes to the directory for your project.
Next, you'll need to install Json.Net. From your project's directory, run:
dotnet add package Newtonsoft.Json --version 11.0.2
Add required namespaces to your project
The dotnet new console
command that you ran earlier created a project, including Program.cs
. This file is where you'll put your application code. Open Program.cs
, and replace the existing using statements. These statements ensure that you have access to all the types required to build and run the sample app.
using System;
using System.Net.Http;
using System.Text;
using Newtonsoft.Json;
Create a function to detect the source text's language
Within the Program
class, create a function called Detect
. This class encapsulates the code used to call the Detect resource and prints the result to console.
static void Detect()
{
/*
* The code for your call to the translation service will be added to this
* function in the next few sections.
*/
}
Set the subscription key, host name, and path
Add these lines to the Detect
function.
string host = "https://api.cognitive.microsofttranslator.com";
string route = "/detect?api-version=3.0";
string subscriptionKey = "YOUR_SUBSCRIPTION_KEY";
Next, we need to create and serialize the JSON object that includes the text that will undergo language detection.
System.Object[] body = new System.Object[] { new { Text = @"Salve mondo!" } };
var requestBody = JsonConvert.SerializeObject(body);
Instantiate the client and make a request
These lines instantiate the HttpClient
and the HttpRequestMessage
:
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
// In the next few sections you'll add code to construct the request.
}
Construct the request and print the response
Inside the HttpRequestMessage
you'll:
- Declare the HTTP method
- Construct the request URI
- Insert the request body (serialized JSON object)
- Add required headers
- Make an asynchronous request
- Print the response
Add this code to the HttpRequestMessage
:
// Set the method to POST
request.Method = HttpMethod.Post;
// Construct the full URI
request.RequestUri = new Uri(host + route);
// Add the serialized JSON object to your request
request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
// Add the authorization header
request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
// Send request, get response
var response = client.SendAsync(request).Result;
var jsonResponse = response.Content.ReadAsStringAsync().Result;
// Print the response
Console.WriteLine(jsonResponse);
Console.WriteLine("Press any key to continue.");
Put it all together
The last step is to call Detect()
in the Main
function. Locate static void Main(string[] args)
and add these lines:
Detect();
Console.ReadLine();
Run the sample app
That's it, you're ready to run your sample app. From the command line (or terminal session), navigate to your project directory and run:
dotnet run
Sample response
[
{
"language": "it",
"score": 1.0,
"isTranslationSupported": true,
"isTransliterationSupported": false,
"alternatives": [
{
"language": "pt",
"score": 1.0,
"isTranslationSupported": true,
"isTransliterationSupported": false
},
{
"language": "en",
"score": 1.0,
"isTranslationSupported": true,
"isTransliterationSupported": false
}
]
}
]
Clean up resources
Make sure to remove any confidential information from your sample app's source code, like subscription keys.
Next steps
Explore the sample code for this quickstart and others, including translation and transliteration, as well as other sample Translator Text projects on GitHub.
See also
Feedback
We'd love to hear your thoughts. Choose the type you'd like to provide:
Our feedback system is built on GitHub Issues. Read more on our blog.
Loading feedback...