Skip to content

Commit 6b1a9f7

Browse files
authored
Update GTranslator.php
1 parent b395fb5 commit 6b1a9f7

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

src/GTranslator.php

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ class GTranslator{
7575
private $toggleClass;
7676

7777
/**
78-
* Hold button type
78+
* Hold bool to check if toggle button is a flag image only
7979
* @var bool
8080
*/
81-
private $jsTrigger = false;
81+
private $imageTrigger = false;
8282

8383
/**
8484
* Hold additional css width value selector container element
@@ -223,7 +223,8 @@ public function setIconType($ext){
223223
* @param string $path
224224
* @return GTranslator $this
225225
*/
226-
public function setIconPath($path){
226+
public function setIconPath($path, $ext = self::PNG){
227+
$this->iconType = $ext;
227228
$this->iconPath = $path;
228229
return $this;
229230
}
@@ -300,13 +301,13 @@ private function selectOptions(){
300301

301302
/**
302303
* Builds selector design for default ui
303-
* @param boolean $jsTrigger if the button is js
304+
* @param boolean $imageTrigger if the button is js
304305
* @return html|string $html
305306
*/
306-
private function selectorCustom($jsTrigger = false){
307-
$this->jsTrigger = $jsTrigger;
307+
private function selectorCustom($imageTrigger = false){
308+
$this->imageTrigger = $imageTrigger;
308309
$this->setLinkClass("selected-language-item");
309-
if($jsTrigger){
310+
if($imageTrigger){
310311
$html = '<div class="language-selector g-translator-custom g-custom-js">';
311312
$html .= '<a class="open-language-selector" href="#">';
312313
$html .= '<img alt="'.$this->siteLang.'" src="' . $this->iconPath . $this->siteLang . $this->iconType . '">';
@@ -316,7 +317,7 @@ private function selectorCustom($jsTrigger = false){
316317
}
317318
$html .= '<ul class="toggle-translator notranslate ' . $this->toggleClass . '">';
318319
$html .= '<li class="toggle-languages">';
319-
if(!$jsTrigger){
320+
if(!$imageTrigger){
320321
$html .= '<a class="" href="#" id="php-g-translator">';
321322
$html .= '<img alt="'.$this->siteLang.'" src="' . $this->iconPath . $this->siteLang . $this->iconType . '">' . $this->languages[$this->siteLang];
322323
$html .= '<span class="toggle-cert"></span>';
@@ -355,20 +356,20 @@ private function selectorBootstrap(){
355356
/**
356357
* Returns computed selector based on provider
357358
* @param string $width button width
358-
* @param boolean $jsTrigger if the button is js
359+
* @param boolean $imageTrigger if the button is js
359360
* @return html|string $this->selectorBootstrap() or $this->selectorCustom()
360361
*/
361-
public function button($width = "170px", $jsTrigger = false){
362-
$this->jsButton(false, $width = "170px");
362+
public function button($width = "170px", $imageTrigger = false){
363+
$this->imageButton(false, $width = "170px");
363364
}
364365

365366
/**
366367
* Returns computed button based on provider and js
367-
* @param boolean $jsTrigger if the button is js
368+
* @param boolean $imageTrigger if the button is js
368369
* @param string $width button width
369370
* @return html|string $this->selectorBootstrap() or $this->selectorCustom()
370371
*/
371-
public function jsButton($jsTrigger = true, $width = "170px"){
372+
public function imageButton($imageTrigger = true, $width = "170px"){
372373
$this->selectWidth = $width;
373374
if(empty($this->languages)){
374375
trigger_error("Error: make sure you add languages first");
@@ -379,7 +380,7 @@ public function jsButton($jsTrigger = true, $width = "170px"){
379380
}else if($this->provider == self::SELECT){
380381
echo $this->selectOptions();
381382
}else{
382-
echo $this->selectorCustom($jsTrigger);
383+
echo $this->selectorCustom($imageTrigger);
383384
}
384385
}
385386

@@ -400,7 +401,6 @@ public function addScript(){
400401
$JSScript = "<script id='php-g-translator-plugin'>var GTranslator = GTranslator || {
401402
siteLang: \"{$this->siteLang}\",
402403
googleElement: \"{$this->element}\",
403-
OPTION_ACTIVE: false,
404404
Languages: " . json_encode($this->getLanguages()) . ",
405405
406406
forceLanguage: function(key){
@@ -504,12 +504,12 @@ public function addScript(){
504504
var from = langs[0];
505505
var lang = langs[1];
506506
GTranslator.runTranslate(from, lang);
507-
var canRun = ". ( $this->jsTrigger ? "1" : "(GTranslator.GButton() != null ? 1 : 0)") . ";
507+
var canRun = ". ( $this->imageTrigger ? "1" : "(GTranslator.GButton() != null ? 1 : 0)") . ";
508508
if(canRun){
509509
var langImage = '<img alt=\"' + lang + '\" src=\"{$this->iconPath}' + lang + '{$this->iconType}\">';
510510
";
511511
if($this->provider == self::DEFAULT){
512-
if($this->jsTrigger){
512+
if($this->imageTrigger){
513513
$JSScript .= "document.getElementsByClassName('open-language-selector')[0].innerHTML = langImage;";
514514
}else{
515515
$JSScript .= "GTranslator.GButton().innerHTML = langImage + ' ' + GTranslator.Languages[lang] + '<span class=\"toggle-cert\"></span>';";
@@ -522,16 +522,17 @@ public function addScript(){
522522

523523
if($this->provider == self::DEFAULT){
524524
$JSScript .= "
525+
isOptionActive: false,
525526
toggle: function() {
526527
var x = document.getElementById('php-gt-languages');
527528
if (x.style.display === 'none') {
528529
x.style.display = 'block';
529530
setTimeout(function(){
530-
GTranslator.OPTION_ACTIVE = true;
531+
GTranslator.isOptionActive = true;
531532
}, 500);
532533
} else {
533534
x.style.display = 'none';
534-
GTranslator.OPTION_ACTIVE = false;
535+
GTranslator.isOptionActive = false;
535536
}
536537
},
537538
@@ -570,21 +571,21 @@ public function addScript(){
570571
571572
document.querySelectorAll('body').forEach(function(ele, i){
572573
ele.addEventListener('click', function(event){
573-
if(window.getComputedStyle(wheel).display === 'block' && GTranslator.OPTION_ACTIVE){
574+
if(window.getComputedStyle(wheel).display === 'block' && GTranslator.isOptionActive){
574575
console.log('Is Open');
575576
GTranslator.toggle();
576577
GTranslator.toggleClass();
577578
}
578579
});
579580
});
580581
}
581-
var canRun = ". ( $this->jsTrigger ? "1" : "(GTranslator.GButton() != null ? 1 : 0)") . ";
582+
var canRun = ". ( $this->imageTrigger ? "1" : "(GTranslator.GButton() != null ? 1 : 0)") . ";
582583
if(canRun && GTranslator.Current() != null){
583584
document.querySelectorAll('.drop-li').forEach(function(ele, i){
584585
if(GTranslator.Current() == ele.firstChild.getAttribute('lang')){
585586
var langImage = '<img alt=\"' + GTranslator.Current() + '\" src=\"{$this->iconPath}' + GTranslator.Current() + '{$this->iconType}\">';
586587
";
587-
if($this->jsTrigger){
588+
if($this->imageTrigger){
588589
$JSScript .= "document.getElementsByClassName('open-language-selector')[0].innerHTML = langImage;";
589590
}else{
590591
$JSScript .= "GTranslator.GButton().innerHTML = langImage + ' ' + ele.firstChild.textContent + '<span class=\"toggle-cert\"></span>';";
@@ -609,7 +610,7 @@ public function addScript(){
609610
}
610611
};";
611612
}else if($this->provider == self::SELECT){
612-
$JSScript .= "}},
613+
$JSScript .= "
613614
trigger: function(self){
614615
GTranslator.Translate(null, '{$this->siteLang}|' + self.value);
615616
localStorage.setItem('siteLang', self.value);

0 commit comments

Comments
 (0)