-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibreTranslate.cs
More file actions
122 lines (109 loc) · 5.36 KB
/
Copy pathLibreTranslate.cs
File metadata and controls
122 lines (109 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
namespace AdiIRC_LibreTranslate_plugin
{
/** LibreTranslate API client for AdiIRC plugin
* Given an API URL and a user language, this class can translate text using the LibreTranslate API.
* The response includes a boolean indicating succes, the translated text, detected language and confidence level.
*/
internal class LibreTranslate
{
string apiUrl;
string userLanguage = "en";
JavaScriptSerializer serializer = new JavaScriptSerializer();
public LibreTranslate(string apiUrl, string userLanguage)
{
this.apiUrl = apiUrl;
this.userLanguage = userLanguage;
}
public class TranslationResponse
{
public string translatedText { get; set; }
public List<string> alternatives { get; set; }
public DetectedLanguage detectedLanguage { get; set; }
public String printableResponse { get; set; }
public Boolean success { get; set; } = false;
}
public class DetectedLanguage
{
public float confidence { get; set; }
public string language { get; set; }
}
/*
* Translates the given text to the user language set in the constructor.
* If the user language is not set, it defaults to English.
* Returns a TranslationResponse object containing the translated text and other details.
*/
public async Task<TranslationResponse> translate(string text)
{
return await _translate(text, userLanguage);
}
/*
* Translates the given text to the specified target language.
* Returns a TranslationResponse object containing the translated text and other details.
*/
public async Task<TranslationResponse> translate(string text, string targetLanguage)
{
return await _translate(text, targetLanguage);
}
/*
* Internal method to perform the actual translation using the LibreTranslate API.
* Handles HTTP requests, response parsing, and error handling.
* Returns a TranslationResponse object containing the translated text and other details.
*/
private async Task<TranslationResponse> _translate(string text, string targetLanguage)
{
using (var client = new HttpClient())
{
// Set the timeout duration (for example, 30 seconds)
client.Timeout = TimeSpan.FromSeconds(20);
var requestBody = new
{
q = text,
source = "auto", // Auto-detect language
target = targetLanguage,
format = "text"
};
var jsonRequest = serializer.Serialize(requestBody);
var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json");
try
{
// Send the request
HttpResponseMessage response = await client.PostAsync(apiUrl, content);
response.EnsureSuccessStatusCode();
string jsonResponse = await response.Content.ReadAsStringAsync();
var translationResult = serializer.Deserialize<TranslationResponse>(jsonResponse);
string translatedMessage = translationResult?.translatedText;
string sourceLanguage = translationResult?.detectedLanguage?.language;
string confidence = translationResult?.detectedLanguage?.confidence.ToString("0") ?? "N/A";
translationResult.printableResponse = $"[Translated {sourceLanguage.ToUpper()}|{targetLanguage.ToUpper()} {confidence}%]: {translatedMessage}";
//Make sure translated message is set and it's not the same as the original text.
if (!string.IsNullOrEmpty(translatedMessage) && !string.Equals(translatedMessage, text, StringComparison.OrdinalIgnoreCase))
{
string output = $"[Translated {sourceLanguage.ToUpper()}|{targetLanguage.ToUpper()} {confidence}%]: {translatedMessage}";
//Only consider it a success if it was actually translated to a different language.
//This is to avoid English being translated to English and shown as a message.
translationResult.success = !sourceLanguage.ToUpper().Equals(targetLanguage.ToUpper());
return translationResult;
}
return translationResult;
}
catch (TaskCanceledException ex) when (ex.CancellationToken == CancellationToken.None)
{
// Handle timeout specifically
throw new TimeoutException("The translation request timed out.", ex);
}
catch (Exception ex)
{
// Handle other exceptions (e.g., network errors)
throw new ApplicationException("An error occurred while making the translation request." + ex.ToString(), ex);
}
}
}
}
}