-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
100 lines (97 loc) · 3.7 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace TranslateApiDemo
{
class Program
{
private const string apiKey = "_yourapikey_";
static async Task Main(string[] args)
{
await LanguageTranslate();
await MicrosoftTranslate();
await GoogleTranslate();
}
private static async Task LanguageTranslate()
{
var client = new HttpClient();
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("https://language-translation.p.rapidapi.com/translateLanguage/translate"),
Headers = {
{ "x-rapidapi-host", "language-translation.p.rapidapi.com" },
{ "x-rapidapi-key", apiKey }
},
Content = new StringContent("{\r\n \"target\": \"tr\",\r\n \"text\": \"About Us\",\r\n \"type\": \"plain\"\r\n}")
{
Headers =
{
ContentType = new MediaTypeHeaderValue("application/json")
}
}
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
Console.WriteLine(body);
}
}
private static async Task MicrosoftTranslate()
{
var client = new HttpClient();
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("https://microsoft-translator-text.p.rapidapi.com/translate?to=tr&api-version=3.0&profanityAction=NoAction&textType=plain"),
Headers = {
{ "x-rapidapi-host", "microsoft-translator-text.p.rapidapi.com" },
{ "x-rapidapi-key", apiKey }
},
Content = new StringContent("[{\"Text\": \"About Us\"}]")
{
Headers =
{
ContentType = new MediaTypeHeaderValue("application/json")
}
}
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
dynamic result = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
Console.WriteLine(result[0].translations[0].text);
}
}
private static async Task GoogleTranslate()
{
var client = new HttpClient();
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("https://google-translate1.p.rapidapi.com/language/translate/v2"),
Headers =
{
{ "x-rapidapi-host", "google-translate1.p.rapidapi.com" },
{ "x-rapidapi-key", apiKey }
},
Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{ "q", "About Us!" },
{ "target", "tr" },
{ "source", "en" }
})
};
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var body = await response.Content.ReadAsStringAsync();
Console.WriteLine(body);
}
}
}
}