-
Notifications
You must be signed in to change notification settings - Fork 4
/
x-translate.html
68 lines (68 loc) · 1.64 KB
/
x-translate.html
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
<link rel="import" href="bower_components/iron-ajax/iron-ajax.html">
<link rel="import" href="bower_components/iron-ajax/iron-ajax.html">
<dom-module id="x-translate">
<style>
:host {
display: inline;
}
.detected-language {
color: gray;
border: 1px solid gray;
font-size: 10pt;
padding: 3px;
}
</style>
<template>
<iron-ajax id="ajax"
url="https://www.googleapis.com/language/translate/v2"
handle-as="json"
on-response="handleResponse"></iron-ajax>
<span>
<span class="detected-language">[[detectedLanguage]]</span>
<span class="translated-text">[[translatedText]]</span>
</span>
</template>
</dom-module>
<script>
(function () {
Polymer({
is: 'x-translate',
properties: {
query: {
type: String,
value: '',
notify: true
},
target: {
type: String,
value: (navigator.browserLanguage || navigator.language ||
navigator.userLanguage).substr(0,2) || 'en',
notify: true
},
source: {
type: String,
value: '',
notify: true
}
},
ready: function () {
var params = {
"key": "AIzaSyCvmuME4nuAt6dsYwM6SKSvpYyn3a_DcnU",
"q": this.query,
"target": this.target
};
if (this.source != null && this.source.length > 0) {
params['source'] = this.source;
}
this.$.ajax.params = params;
this.$.ajax.generateRequest();
},
handleResponse: function (request) {
var result = request.detail.response.data.translations[0];
this.detectedLanguage = result.detectedSourceLanguage;
this.translatedText = result.translatedText;
}
}
);
})();
</script>