-
Notifications
You must be signed in to change notification settings - Fork 0
/
Home.cs
133 lines (109 loc) · 3.65 KB
/
Home.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace translator
{
public partial class Home : Form
{
public string inLang { get; set; }
public string outLang { get; set; }
public string text { get; set; }
public Home()
{
InitializeComponent();
}
private void Home_Load(object sender, EventArgs e)
{
this.CenterToScreen();
this.SetControls();
}
private void SetControls()
{
//Form
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
}
private void inputLangInputTxt_TextChanged(object sender, EventArgs e)
{
inLang = this.inputLangInputTxt.Text;
}
private void OutputLangInputTxt_TextChanged(object sender, EventArgs e)
{
outLang = this.OutputLangInputTxt.Text;
}
private void TextInputTxt_TextChanged(object sender, EventArgs e)
{
text = this.TextInputTxt.Text;
}
public async void API(string lang1, string lang2, string text)
{
string inputLang = lang1;
string outputLang = lang2;
string inputText = text;
var client = new HttpClient();
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("https://text-translator2.p.rapidapi.com/translate"),
Headers =
{
{ "X-RapidAPI-Key", "113a32bba8msh01a2e5808733a20p1a28d1jsn1c402fdabd00" },
{ "X-RapidAPI-Host", "text-translator2.p.rapidapi.com" },
},
Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{ "source_language", inputLang },
{ "target_language", outputLang },
{ "text", inputText },
}),
};
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
string body = await response.Content.ReadAsStringAsync();
JObject obj = JObject.Parse(body);
translatedTxt.Text = (obj["data"]["translatedText"]).ToString();
}
private void translateBtn_Click(object sender, EventArgs e)
{
API(inLang, outLang, text);
}
private void LanguagesBtn_Click(object sender, EventArgs e)
{
this.Close();
Thread t = new Thread(new ThreadStart(ThreadLanguagesPage));
t.Start();
}
private void ThreadLanguagesPage()
{
//RUNs a NEW application with the desired form
Application.Run(new LanguageCodes());
}
private void DocumentBtn_Click(object sender, EventArgs e)
{
this.Close();
Thread t = new Thread(new ThreadStart(ThreadFileTranslator));
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
//t.Start();
}
private void ThreadFileTranslator()
{
//RUNs a NEW application with the desired form
Application.Run(new FileTranslator());
}
}
}