-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
120 lines (110 loc) · 3.18 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
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
import React from 'react';
import { StyleSheet, Text, View, TextInput, Button, Alert, Picker } from 'react-native';
import { Constants } from 'expo';
import { Bars} from 'react-native-loader';
import Header from './src/components/header';
import EditText from './src/components/editText';
const key = {FREE_API_KEY_FROM_YANDEX};
export default class App extends React.Component {
constructor() {
super();
this.state = {
word: '',
loading: false,
goForRequest: false,
translatedWord: '',
language: 'ru'
}
}
onWordChange = (word) => {
this.setState({word});
}
translate = () => {
this.setState({
loading: true
});
fetch('https://translate.yandex.net/api/v1.5/tr.json/translate?key='+key+'&lang='+this.state.language+'&text='+this.state.word)
.then((response) => response.json())
.then((responseJson) => {
var translatedWord = responseJson.text;
this.setState({translatedWord, loading: false});
})
.catch((error) => {
this.setState({loading: false});
Alert.alert('Something Went Wrong');
});
}
checkLoading = () => {
if(!this.state.loading) {
return <View style={styles.translateViewStyle}>
<Text style={styles.translatedWordHint}>English</Text>
<Text style={styles.translatedWord}>{this.state.word}</Text>
<Text style={styles.translatedWordHint}>Translated</Text>
<Text style={styles.translatedWord}>{this.state.translatedWord}</Text>
</View>;
} else {
return <View style={styles.progressStyle}>
<Bars size={20} color='green' />
</View>;
}
}
render() {
return (
<View style={styles.container}>
<Header title='Word Translator'/>
<View style={styles.content}>
<EditText onWordChange={this.onWordChange} />
<Text>Choose Langulage to Translate</Text>
<Picker
selectedValue={this.state.language}
onValueChange={(lang) => this.setState({language: lang})}>
<Picker.Item label="Russian" value="ru" />
<Picker.Item label="French" value="fr" />
<Picker.Item label="Spanish" value="es" />
<Picker.Item label="Chineese" value="zh" />
<Picker.Item label="Nepali" value="ne" />
</Picker>
<Button
onPress={this.translate}
title="Translate"
color="green"
disabled={(this.state.word.toString().trim().length == 0)? true: false}
/>
</View>
{this.checkLoading()}
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
paddingTop: Constants.statusBarHeight,
},
content: {
margin: 10
},
translateViewStyle: {
padding: 10,
alignItems: 'center',
justifyContent: 'center'
},
translatedWordHint: {
fontSize: 18,
color: 'grey',
textAlign: 'center',
fontWeight: 'bold',
marginTop: 20,
},
translatedWord: {
fontSize: 20,
color: 'green',
textAlign: 'center',
fontWeight: 'bold',
},
progressStyle: {
alignItems: 'center',
justifyContent: 'center'
}
});