Integrating DeepL Library into C# .NET Core 7

Technology - 8/9/2023
Integrating DeepL Library into C# .NET Core 7

Introduction

In the world of natural language processing (NLP) and machine translation, a plethora of tools and services are available. Among them, DeepL stands out as one of the most advanced and high-quality options. In this article, we'll discuss the reasons to integrate DeepL into your C# .NET Core 7 projects, and we'll guide you through the process step by step.

Why Choose DeepL Over Other Translators?

1. Accuracy and Naturalness

DeepL, when compared with its competitors, consistently ranks as the most accurate and natural-sounding machine translator, especially for European languages. Leveraging the power of advanced neural networks, it has often been praised for translations that come close to human-like fluency.

2. Rich Features and Options

DeepL's API offers a variety of features, including handling of formal vs. informal tone, detecting source language automatically, and more. These features make it a flexible solution for developers.

3. Data Security

For those concerned about data security, DeepL assures users that all texts are deleted immediately after the translation is completed, ensuring data privacy.

4. Integration and Compatibility

DeepL offers a robust API, making it adaptable and integrable into a multitude of platforms, including .NET Core.

Integration Steps

1. Sign Up & Get Your API Key

Before starting the integration, you need to have an API key from DeepL. Register for a developer account on the DeepL website, opt for the plan that suits your needs, and obtain your unique API key.

2. Create a New .NET Core 7 Project

    
dotnet new console -n DeepLIntegration
cd DeepLIntegration
    
    

3. Install Necessary Packages

To send HTTP requests, we will utilize HttpClient, which is part of the System.Net.Http namespace. Ensure you have this package installed.

    
dotnet add package System.Net.Http
    
    

4. Create the Translation Function

Create a new static method named TranslateWithDeepL. This function will make an HTTP POST request to the DeepL API endpoint.

    
using System;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;

class Program
{
    private static readonly string API_KEY = "YOUR_DEEPL_API_KEY";
    private static readonly string DEEPL_API_ENDPOINT = "https://api.deepl.com/v2/translate";

    static async Task Main(string[] args)
    {
        string sourceText = "Hello World!";
        string translatedText = await TranslateWithDeepL(sourceText, "DE");
        Console.WriteLine(translatedText);
    }

    static async Task<string> TranslateWithDeepL(string text, string targetLang)
    {
        using (HttpClient client = new HttpClient())
        {
            var content = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("auth_key", API_KEY),
                new KeyValuePair<string, string>("text", text),
                new KeyValuePair<string, string>("target_lang", targetLang),
            });

            HttpResponseMessage response = await client.PostAsync(DEEPL_API_ENDPOINT, content);
            string responseBody = await response.Content.ReadAsStringAsync();

            var jsonResponse = JsonDocument.Parse(responseBody);
            string translatedText = jsonResponse.RootElement.GetProperty("translations")[0].GetProperty("text").GetString();

            return translatedText;
        }
    }
}
    
    

Conclusion

DeepL's unique combination of accuracy, feature-richness, and emphasis on security makes it a prime choice for developers. Integrating it into your C# .NET Core 7 applications is a straightforward process that will undeniably boost the multilingual capabilities of your software. Remember always to adhere to usage guidelines and be aware of API request limits based on your chosen plan.

Happy coding, and enjoy the power of DeepL in your applications!

do together

Do you have an idea? Let's get together.

Our mission is to help people who want to create exceptional websites. If you have an idea for an exceptional project, please contact us.

Contact Us