How to detect the language of a string using C# in .NET Framework

Cloudmersive
2 min readSep 20, 2019

--

Any time your company, app, or website interacts with a client, there is a good chance that said client does not speak English. There are many automatic translation options available, but most of them require the language to be identified. With so many world languages in use, this becomes a daunting task. However, there is an easy way to quickly identify the language of text. Today we will look at how to use our NLP API to harness AI to accomplish this difficult goal for us.

First, let us install the NLP client using the Package Manager console:

Install-Package Cloudmersive.APIClient.NET.NLP -Version 1.2.4

Now, all we have to do is to call LanguageDetectionPost and provide it with a string of text.

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.NLP.Api;
using Cloudmersive.APIClient.NET.NLP.Client;
using Cloudmersive.APIClient.NET.NLP.Model;
namespace Example
{
public class LanguageDetectionPostExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");
var apiInstance = new LanguageDetectionApi();
var textToDetect = textToDetect_example; // string | Text to detect language of
try
{
// Detect language of text
LanguageDetectionResponse result = apiInstance.LanguageDetectionPost(textToDetect);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling LanguageDetectionApi.LanguageDetectionPost: " + e.Message );
}
}
}
}

And that’s it! This function works with all the most commonly used languages, including French, German, Italian, Portuguese, Chinese, and Spanish. Here is an example output for the text: “ Warum spielst du die beleidigte Leberwurst?”

{
"Successful": true,
"DetectedLanguage_ThreeLetterCode": "DEU",
"DetectedLanguage_FullName": "German"
}

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet