我想要一个工具,可以将源语言中的单词或句子翻译成各种可选择的语言,并将其组合成某个字符串。 为此我想使用 DeepL API。 当我尝试使用该工具时,我将收到 HTTP 响应 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);
}
}
}
}
}
我通过 Fiddler Everywhere 进行跟踪。请求看起来像这样:
我尝试了各种 DeepL-API-URL,结果相同。
当前版本的 DeepL API (v2) 使用
POST v2/translate
并期望主体中的 JSON
对象至少提供 text
和 target_lang
参数。
您的
Auth Key
应在请求的 Authorization
标头中提供。
POST /v2/translate HTTP/2
Host: api-free.deepl.com
Authorization: DeepL-Auth-Key [yourAuthKey]
User-Agent: YourApp/1.2.3
Content-Length: 45
Content-Type: application/json
{"text":["Hello, world!"],"target_lang":"DE"}
Translate Text
的文档。