I want to have a tool, that can transalte Words or sentences from a source language into various selctable languages and put it together in a certain string. For this I wanted to use DeepL API. When I'm trying to use the tool, I will get the HTTP response 403-Forbidden.
XAML:
<Window x:Class="DeepL_Translator.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DeepL Translator" Height="450" Width="800">
<Grid>
<StackPanel Margin="10">
<Label Content="Text zu übersetzen:" />
<TextBox x:Name="txtInput" TextWrapping="Wrap" Height="100" />
<Label Content="Zielsprachen:" />
<StackPanel Orientation="Horizontal">
<CheckBox x:Name="chkEnglish" Content="Englisch (Britisch)" IsChecked="True" />
<CheckBox x:Name="chkCzech" Content="Tschechisch" />
<CheckBox x:Name="chkSpanish" Content="Spanisch" />
<CheckBox x:Name="chkPortuguese" Content="Portugiesisch (Brasilien)" />
<CheckBox x:Name="chkFrench" Content="Französisch" />
</StackPanel>
<Button x:Name="btnTranslate" Content="Übersetzen" Click="BtnTranslate_Click" />
<Label Content="Übersetzter Text:" />
<TextBox x:Name="txtOutput" TextWrapping="Wrap" Height="100" />
</StackPanel>
</Grid>
</Window>
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Newtonsoft.Json.Linq;
using DeepL_Translator.Languages;
using DeepL;
namespace DeepL_Translator
{
public enum LanguageCode
{
EN,
CS,
ES,
PT,
FR,
DE,
}
public partial class MainWindow : Window
{
private const string API_KEY = "f0798faa-..."; // Replace with actual key
private const string API_URL = "https://api-free.deepl.com/v2/translate";
public MainWindow()
{
InitializeComponent();
}
private void BtnTranslate_Click(object sender, RoutedEventArgs e)
{
string text = txtInput.Text;
List<string> targetLanguages = new List<string>();
// Get selected target languages
if (chkEnglish.IsChecked.Value) targetLanguages.Add("EN-GB");
if (chkCzech.IsChecked.Value) targetLanguages.Add("CS");
if (chkSpanish.IsChecked.Value) targetLanguages.Add("ES");
if (chkPortuguese.IsChecked.Value) targetLanguages.Add("PT-BR");
if (chkFrench.IsChecked.Value) targetLanguages.Add("FR");
// Create the HTTP request
using (HttpClient client = new HttpClient())
{
string targetLangString = "";
foreach (var langCode in targetLanguages)
{
targetLangString += langCode.ToString().ToUpper() + ",";
}
// Remove the trailing comma
targetLangString = targetLangString.TrimEnd(',');
string requestUrl = $"{API_URL}?auth_key={API_KEY}&text={text}&source_lang=DE&target_langs={targetLangString}";
try
{
var response = client.GetAsync(requestUrl).Result;
// Parse the JSON response
if (response.IsSuccessStatusCode)
{
string jsonResponse = response.Content.ReadAsStringAsync().Result;
JObject json = JObject.Parse(jsonResponse);
// Get the translated text
StringBuilder sbOutput = new StringBuilder();
sbOutput.AppendLine("{i18n de}").AppendLine(text).AppendLine("{/i18n}");
foreach (JObject translation in json["translations"])
{
sbOutput.AppendLine("{i18n " + translation["target_lang"].ToString().ToLower() + "}");
sbOutput.AppendLine(translation["text"].ToString());
sbOutput.AppendLine("{/i18n}");
}
txtOutput.Text = sbOutput.ToString();
}
else
{
MessageBox.Show($"An error occurred while translating the text. Status code: {response.StatusCode}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
catch (Exception ex)
{
// Handle other potential exceptions (e.g., network issues)
MessageBox.Show($"An error occurred: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
}
I tracked via Fiddler Everywhere. The Request looks like that:
I tried various kinds of DeepL-API-URLs, with the same result.
The current version of the DeepL API (v2) uses
POST v2/translateand expects aJSONobject in the body providing thetextandtarget_langparameters at minimum.Your
Auth Keyshould be provided in theAuthorizationheader of your request.I suggest you take a look at the documentation for
Translate Text.