-
Notifications
You must be signed in to change notification settings - Fork 2
/
GoogleJavaScript.php
193 lines (173 loc) · 8.88 KB
/
GoogleJavaScript.php
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
class GoogleJavaScript{
private const DEFAULT = 1;
private const BOOTSTRAP = 2;
private $siteLang = "en";
private $element = "demo_element2";
public static function printScript($provider, $lng = "en", $element = "demo_element2"){
$this->siteLang = $lng;
$this->element = $element;
$JSScript = "<script>
var GTranslator = {
siteLang: \"{$this->siteLang}\",
googleElement: \"{$this->element}\",
OPTION_ACTIVE: false,
Languages: " . json_encode($this->languages) . ",
GButton: function(){
return document.getElementById('php-g-translator');
},
Current: function() {
var keyValue = document['cookie'].match('(^|;) ?googtrans=([^;]*)(;|$)'); return keyValue ? keyValue[2].split('/')[2] : GTranslator.siteLang;
},
Event: function(element,event){
try{
if(document.createEventObject){
var evt=document.createEventObject();element.fireEvent('on'+event,evt)
}else{
var evt=document.createEvent('HTMLEvents');
evt.initEvent(event,true,true);
element.dispatchEvent(evt);
}
}catch(e){
console.log('GTranslator: ' + e);
}
},
GoogleInit: function() {
new google.translate.TranslateElement({pageLanguage: \"{$this->siteLang}\", autoDisplay: false}, GTranslator.googleElement);
},
GoogleScript: function(){
var s1=document.createElement('script');
s1.async=true;
s1.type = 'text/javascript';
s1.src='https://translate.google.com/translate_a/element.js?cb=GTranslator.GoogleInit';
var s0 = document.getElementsByTagName('script')[0];
s0.parentNode.insertBefore(s1, s0);
},
runTranslate: function(from, to) {
if (GTranslator.Current() == null && lang == from){
return;
}
var teCombo;
var sel = document.getElementsByTagName('select');
for (var i = 0; i < sel.length; i++){
if (/goog-te-combo/.test(sel[i].className)) {
teCombo = sel[i];
break;
}
}
if (document.getElementById(GTranslator.googleElement) == null || document.getElementById(GTranslator.googleElement).innerHTML.length == 0 || teCombo.length == 0 || teCombo.innerHTML.length == 0) {
setTimeout(function() {
GTranslator.runTranslate(from, to)
}, 500)
} else {
teCombo.value = to;
GTranslator.Event(teCombo, 'change');
}
},
Translate: function(self, lang_pair) {
if (typeof lang_pair != 'undefined'){
if(lang_pair.value){
lang_pair = lang_pair.value;
}
}
if (lang_pair == '' || lang_pair.length < 1){
return;
}
var langs = lang_pair.split('|');
var from = langs[0];
var lang = langs[1];
GTranslator.runTranslate(from, lang);
if(GTranslator.GButton() != null){";
if($this->provider == self::DEFAULT){
$JSScript .= "GTranslator.GButton().innerHTML = '<img alt=\"' + lang + '\" src=\"{$this->iconPath}' + lang + '{$this->iconType}\"> ' + GTranslator.Languages[lang] + '<span class=\"toggle-cert\"></span>';";
}else if($this->provider == self::BOOTSTRAP){
$JSScript .= "GTranslator.GButton().innerHTML = '<img alt=\"' + lang + '\" src=\"{$this->iconPath}' + GTranslator.Current() + '{$this->iconType}\"> ' + GTranslator.Languages[lang];";
}
$JSScript .= "}
},";
if($this->provider == self::DEFAULT){
$JSScript .= "
toggle: function() {
var x = document.getElementById('php-gt-languages');
if (x.style.display === 'none') {
x.style.display = 'block';
setTimeout(function(){
GTranslator.OPTION_ACTIVE = true;
}, 500);
} else {
x.style.display = 'none';
GTranslator.OPTION_ACTIVE = false;
}
},
toggleClass: function() {
if(GTranslator.GButton() != null){
GTranslator.GButton().classList.toggle('open');
}
},
Init: function(){
GTranslator.GoogleScript();
if(typeof document.getElementsByClassName('toggle-languages')[0] != 'undefined'){
document.getElementsByClassName('toggle-languages')[0].onclick = function(event) {
event.preventDefault();
GTranslator.toggle();
GTranslator.toggleClass();
};
}
if(wheel = document.getElementById('php-gt-languages')){
wheel.addEventListener('wheel', function(event){
if (window.getComputedStyle(wheel).display === 'block') {
wheel.scrollTo({
top: wheel.scrollTop - (event.wheelDelta || -event.detail)
});
}
return false;
});
document.querySelectorAll('body').forEach(function(ele, i){
ele.addEventListener('click', function(event){
if(window.getComputedStyle(wheel).display === 'block' && GTranslator.OPTION_ACTIVE){
console.log('Is Open');
GTranslator.toggle();
GTranslator.toggleClass();
}
});
});
}
if(GTranslator.GButton() != null && GTranslator.Current() != null){
document.querySelectorAll('.drop-li').forEach(function(ele, i){
if(GTranslator.Current() == ele.firstChild.getAttribute('lang')){
GTranslator.GButton().innerHTML = '<img alt=\"' + GTranslator.Current() + '\" src=\"{$this->iconPath}' + GTranslator.Current() + '{$this->iconType}\"> ' + ele.firstChild.textContent + '<span class=\"toggle-cert\"></span>';
}
});
}
}
};";
}else if($this->provider == self::BOOTSTRAP){
$JSScript .= "
Init: function(){
GTranslator.GoogleScript();if(GTranslator.Current() != null){
document.querySelectorAll('.drop-li').forEach(function(ele, i){
if(GTranslator.GButton() != null && GTranslator.Current() == ele.firstChild.getAttribute('lang')){
GTranslator.GButton().innerHTML = '<img alt=\"' + GTranslator.Current() + '\" src=\"{$this->iconPath}' + GTranslator.Current() + '{$this->iconType}\"> ' + ele.firstChild.textContent;
}
});
}
}
};";
}else if($this->provider == self::SELECT){
$JSScript .= "}},
trigger: function(self){
GTranslator.Translate(null, '{$this->siteLang}|' + self.value);return false;
},
Init:function(){
GTranslator.GoogleScript();
}
};";
}
$JSScript .= "
(function(){
GTranslator.Init();
})();
</script>";
return $JSScript;
}
}