1
1
using System ;
2
2
using System . Collections . Generic ;
3
- using System . Diagnostics . CodeAnalysis ;
3
+ using System . Net ;
4
4
using System . Net . Http ;
5
5
using System . Net . Http . Json ;
6
- using System . Text . Json . Serialization ;
7
6
using System . Threading . Tasks ;
8
7
9
8
namespace tweetz . core . Services
10
9
{
11
10
internal static class TranslateService
12
11
{
13
- private const string endpoint = "https://libretranslate.com/translate " ;
12
+ private const string endpoint = "https://api.mymemory.translated.net/get " ;
14
13
15
- public static async ValueTask < string > Translate ( string ? text , string fromLanguage , string toLanguage , string translateApiKey )
14
+ public static async ValueTask < string > Translate ( string ? text , string fromLanguage , string toLanguage , string ? translateApiKey )
16
15
{
17
16
if ( string . IsNullOrEmpty ( text ) )
18
17
{
@@ -21,33 +20,51 @@ public static async ValueTask<string> Translate(string? text, string fromLanguag
21
20
22
21
try
23
22
{
24
- var request = new HttpRequestMessage {
25
- Method = HttpMethod . Post ,
26
- RequestUri = new Uri ( endpoint ) ,
27
- Content = new FormUrlEncodedContent ( new [ ] {
28
- new KeyValuePair < string ? , string ? > ( "q" , text ) ,
29
- new KeyValuePair < string ? , string ? > ( "source" , fromLanguage ) ,
30
- new KeyValuePair < string ? , string ? > ( "target" , toLanguage ) ,
31
- new KeyValuePair < string ? , string ? > ( "api_key" , translateApiKey )
32
- } )
33
- } ;
34
-
35
- using var response = await App . MyHttpClient . SendAsync ( request ) . ConfigureAwait ( false ) ;
36
- var result = await response . Content . ReadFromJsonAsync < TranslatorResult > ( ) . ConfigureAwait ( false ) ;
37
- return result ? . TranslatedText ?? result ? . ErrorText ?? "no response" ;
23
+ var parameters = await BuildParameters ( text , fromLanguage , toLanguage , translateApiKey ) ;
24
+ var requestUri = new Uri ( endpoint + "?" + parameters ) ;
25
+ using var response = await App . MyHttpClient . GetAsync ( requestUri ) . ConfigureAwait ( false ) ;
26
+ var result = await response . Content . ReadFromJsonAsync < TranslatorResult > ( ) . ConfigureAwait ( false ) ;
27
+ var translatedText = result ? . ResponseData ? . TranslatedText ?? "no response" ;
28
+ var html = WebUtility . HtmlDecode ( WebUtility . HtmlDecode ( translatedText ) ) ; // Twice to handle sequences like: "&mdash;"
29
+ return html ;
38
30
}
39
31
catch ( Exception ex )
40
32
{
41
33
return ex . Message ;
42
34
}
43
35
}
36
+
37
+ private static async Task < string > BuildParameters ( string ? text , string fromLanguage , string toLanguage , string ? translateApiKey )
38
+ {
39
+ var pars = new List < KeyValuePair < string ? , string ? > > {
40
+ new ( "q" , text ) ,
41
+ new ( "langpair" , $ "{ fromLanguage } |{ toLanguage } ")
42
+ } ;
43
+
44
+ // The translateApiKey is your email.
45
+ // Add it to your settings file.
46
+ // Use your own real email please.
47
+ // Don't abuse the MyMemoryService.
48
+ // https://mymemory.translated.net/doc/usagelimits.php
49
+
50
+ if ( ! string . IsNullOrWhiteSpace ( translateApiKey ) ) pars . Add ( new KeyValuePair < string ? , string ? > ( "de" , translateApiKey ) ) ;
51
+
52
+ var content = new FormUrlEncodedContent ( pars ) ;
53
+ var parameters = await content . ReadAsStringAsync ( ) . ConfigureAwait ( false ) ;
54
+ return parameters ;
55
+ }
44
56
}
45
57
46
- [ SuppressMessage ( "ReSharper" , "ClassNeverInstantiated.Global" ) ]
58
+ // ReSharper disable UnusedAutoPropertyAccessor.Global
59
+ // ReSharper disable ClassNeverInstantiated.Global
60
+
47
61
public class TranslatorResult
48
62
{
49
- // ReSharper disable once UnusedAutoPropertyAccessor.Global
50
- [ JsonPropertyName ( "translatedText" ) ] public string ? TranslatedText { get ; set ; }
51
- [ JsonPropertyName ( "error" ) ] public string ? ErrorText { get ; set ; }
63
+ public ResponseData ? ResponseData { get ; set ; }
64
+ }
65
+
66
+ public class ResponseData
67
+ {
68
+ public string ? TranslatedText { get ; set ; }
52
69
}
53
70
}
0 commit comments