-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathApp.js
83 lines (75 loc) · 2.32 KB
/
App.js
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
import logo from './logo.svg';
import './App.css';
import { useEffect, useState } from 'react';
const axios = require('axios').default;
function App() {
const [options, setOptions] = useState([]);
const [to, setTo] = useState('en');
const [from, setFrom] = useState('en');
const [input, setInput] = useState('');
const [output, setOutput] = useState('');
const translate = () => {
// curl -X POST "https://libretranslate.de/translate" -H "accept: application/json" -H "Content-Type: application/x-www-form-urlencoded" -d "q=hello&source=en&target=es&api_key=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
const params = new URLSearchParams();
params.append('q', input);
params.append('source', from);
params.append('target', to);
params.append('api_key', 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx');
axios.post('https://libretranslate.de/translate',params, {
headers: {
'accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
},
}).then(res=>{
console.log(res.data)
setOutput(res.data.translatedText)
})
};
useEffect(() => {
axios
.get('https://libretranslate.de/languages', {
headers: { accept: 'application/json' },
})
.then((res) => {
console.log(res.data);
setOptions(res.data);
});
}, []);
// curl -X GET "https://libretranslate.de/languages" -H "accept: application/json"
return (
<div className="App">
<div>
From ({from}) :
<select onChange={(e) => setFrom(e.target.value)}>
{options.map((opt) => (
<option key={opt.code} value={opt.code}>
{opt.name}
</option>
))}
</select>
To ({to}) :
<select onChange={(e) => setTo(e.target.value)}>
{options.map((opt) => (
<option key={opt.code} value={opt.code}>
{opt.name}
</option>
))}
</select>
</div>
<div>
<textarea
cols="50"
rows="8"
onInput={(e) => setInput(e.target.value)}
></textarea>
</div>
<div>
<textarea cols="50" rows="8" value={output}></textarea>
</div>
<div>
<button onClick={e=>translate()}>Translate</button>
</div>
</div>
);
}
export default App;