Skip to content

Commit 31ab3a8

Browse files
authored
Update GoogleJavaScript.php
1 parent 7ace06d commit 31ab3a8

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

GoogleJavaScript.php

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
<?php
22
class GoogleJavaScript{
3-
public static function printScript($provider, $siteLang = "en", $element = "demo_element2"){
4-
$JSScript = "<script>
3+
private const DEFAULT = 1;
4+
private const BOOTSTRAP = 2;
5+
private $siteLang = "en";
6+
private $element = "demo_element2";
7+
8+
public static function printScript($provider, $lng = "en", $element = "demo_element2"){
9+
$this->siteLang = $lng;
10+
$this->element = $element;
11+
$JSScript = "<script>
512
var GTranslator = {
6-
siteLang: \"{$siteLang}\",
7-
googleElement: \"{$element}\",
13+
siteLang: \"{$this->siteLang}\",
14+
googleElement: \"{$this->element}\",
815
OPTION_ACTIVE: false,
16+
Languages: " . json_encode($this->languages) . ",
17+
18+
GButton: function(){
19+
return document.getElementById('php-g-translator');
20+
},
21+
922
Current: function() {
1023
var keyValue = document['cookie'].match('(^|;) ?googtrans=([^;]*)(;|$)'); return keyValue ? keyValue[2].split('/')[2] : GTranslator.siteLang;
1124
},
12-
25+
1326
Event: function(element,event){
1427
try{
1528
if(document.createEventObject){
@@ -25,7 +38,7 @@ public static function printScript($provider, $siteLang = "en", $element = "demo
2538
},
2639
2740
GoogleInit: function() {
28-
new google.translate.TranslateElement({pageLanguage: \"{$siteLang}\", autoDisplay: false}, GTranslator.googleElement);
41+
new google.translate.TranslateElement({pageLanguage: \"{$this->siteLang}\", autoDisplay: false}, GTranslator.googleElement);
2942
},
3043
3144
GoogleScript: function(){
@@ -77,13 +90,14 @@ public static function printScript($provider, $siteLang = "en", $element = "demo
7790
var from = langs[0];
7891
var lang = langs[1];
7992
GTranslator.runTranslate(from, lang);";
80-
if($provider == 1){
81-
$JSScript .= "document.getElementById('php-g-translator').innerHTML = '<img alt=\"' + lang + '\" src=\"{$iconPath}' + lang + '{$iconType}\"> ' + self.getAttribute('title') + '<span class=\"toggle-cert\"></span>';";
82-
}else if($provider == 2){
83-
$JSScript .= "document.getElementById('php-g-translator').innerHTML = '<img alt=\"' + lang + '\" src=\"{$iconPath}' + GTranslator.Current() + '{$iconType}\"> ' + self.getAttribute('title');";
93+
if($this->provider == self::DEFAULT){
94+
$JSScript .= "GTranslator.GButton().innerHTML = '<img alt=\"' + lang + '\" src=\"{$this->iconPath}' + lang + '{$this->iconType}\"> ' + GTranslator.Languages[lang] + '<span class=\"toggle-cert\"></span>';";
95+
}else if($this->provider == self::BOOTSTRAP){
96+
$JSScript .= "GTranslator.GButton().innerHTML = '<img alt=\"' + lang + '\" src=\"{$this->iconPath}' + GTranslator.Current() + '{$this->iconType}\"> ' + GTranslator.Languages[lang];";
8497
}
8598
$JSScript .= "},";
86-
if($provider == 1){
99+
100+
if($this->provider == self::DEFAULT){
87101
$JSScript .= "
88102
toggle: function() {
89103
var x = document.getElementById('php-gt-languages');
@@ -99,8 +113,7 @@ public static function printScript($provider, $siteLang = "en", $element = "demo
99113
},
100114
101115
toggleClass: function() {
102-
var element = document.getElementById('php-g-translator');
103-
element.classList.toggle('open');
116+
GTranslator.GButton().classList.toggle('open');
104117
},
105118
106119
Init: function(){
@@ -131,19 +144,19 @@ public static function printScript($provider, $siteLang = "en", $element = "demo
131144
});if(GTranslator.Current() != null){
132145
document.querySelectorAll('.drop-li').forEach(function(ele, i){
133146
if(GTranslator.Current() == ele.firstChild.getAttribute('lang')){
134-
document.getElementById('php-g-translator').innerHTML = '<img alt=\"' + GTranslator.Current() + '\" src=\"{$iconPath}' + GTranslator.Current() + '{$iconType}\"> ' + ele.firstChild.textContent + '<span class=\"toggle-cert\"></span>';
147+
GTranslator.GButton().innerHTML = '<img alt=\"' + GTranslator.Current() + '\" src=\"{$this->iconPath}' + GTranslator.Current() + '{$this->iconType}\"> ' + ele.firstChild.textContent + '<span class=\"toggle-cert\"></span>';
135148
}
136149
});
137150
}
138151
}
139152
};";
140-
}else if($provider == 2){
153+
}else if($this->provider == self::BOOTSTRAP){
141154
$JSScript .= "
142155
Init: function(){
143156
GTranslator.GoogleScript();if(GTranslator.Current() != null){
144157
document.querySelectorAll('.drop-li').forEach(function(ele, i){
145158
if(GTranslator.Current() == ele.firstChild.getAttribute('lang')){
146-
document.getElementById('php-g-translator').innerHTML = '<img alt=\"' + GTranslator.Current() + '\" src=\"{$iconPath}' + GTranslator.Current() + '{$iconType}\"> ' + ele.firstChild.textContent;
159+
GTranslator.GButton().innerHTML = '<img alt=\"' + GTranslator.Current() + '\" src=\"{$this->iconPath}' + GTranslator.Current() + '{$this->iconType}\"> ' + ele.firstChild.textContent;
147160
}
148161
});
149162
}

0 commit comments

Comments
 (0)