| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Aitor24\Laralang; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Aitor24\Laralang\Models\DB_Translation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Illuminate\Support\Facades\DB; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use App\Http\Controllers\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class Translation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * Setup public vars. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public $translator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     public $translation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public $debug; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public $from; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public $to; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * Setup default values. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param string $string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function __construct($string) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->translator = config('laralang.default.translator'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->debug = config('laralang.default.debug'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->from = config('laralang.default.from_lang'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->to = config('laralang.default.to_lang'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->string = $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->translation = $string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         // Checking whether from_lang or to_lang are set as app_locale. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         if ($this->from == 'app_locale') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             $this->from = App::getLocale(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if ($this->to == 'app_locale') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $this->to = App::getLocale(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * Setup debug value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param bool $debug | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     public function setDebug($debug) | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $this->debug = $debug; | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         return $this; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * Setup fromLang value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param string $lang | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function setFromLang($lang) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $this->from = $lang; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * Setup tolang value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param string $lang | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     public function setToLang($lang) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $this->to = $lang; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * Setup translator. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param string $translator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     public function setTranslator($translator) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->translator = $translator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     private function exists() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $existing = DB_Translation::where('string', '=', $this->string) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         ->where('from_lang', '=', $this->from) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         ->where('to_lang', '=', $this->to) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         ->where('translator', '=', $this->translator)->get(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         if (count($existing) == 0){ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         if ($this->debug === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $this->translation = "<font style='color:#00CC00;'>Loaded correclty from you DB</font>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $this->translation = $existing[0]->translation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * Function to save translations to DB | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     private function save() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $trans = new DB_Translation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $trans->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         $trans->alias = $trans->getId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $trans->string = $this->string; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $trans->from_lang = $this->from; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         $trans->to_lang = $this->to; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $trans->translator = $this->translator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         $trans->translation = $this->translation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         $trans->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * Main function of the class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * Check what translator must select | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     private function run() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         // Return the value if the language is the same. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         if ($this->from == $this->to) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             if ($this->debug === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                 $this->translation = "<font style='color:orange;'>Same in <> out languge</font>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         if ($this->exists() === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $available_transoltors = ['apertium', 'mymemory']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         // Checks available translators. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         if (in_array($this->translator, $available_transoltors) == false) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |             if ($this->debug === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |                 $this->translation = "<font style='color:red;'>Not suported translator: ".$this->translator.'</font>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |         if ($this->translator == 'apertium') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             return $this->apertiumTrans(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |         } elseif ($this->translator == 'mymemory') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |             return $this->mymemoryTrans(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |      /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |       * Get translation from mymemory API. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |       */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |      private function mymemoryTrans() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |      { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |          // Check if it can be translated from online sources. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |          $host = 'api.mymemory.translated.net'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |          if ($socket = @fsockopen($host, 80, $errno, $errstr, 30)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |              // Host online | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |              $urlString = urlencode($this->string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |              $url = "http://$host/get?q=$urlString&langpair=$this->from%7C$this->to"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |              $json = file_get_contents($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |              $data = json_decode($json); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |              // Checking response status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |              if ($data->responseStatus != 200) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |                  if ($this->debug == true) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |                      $details = $data->responseDetails; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |                      if ($data->responseStatus == 403) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |                          $details = ($data->responseDetails); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |                      } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |                      $this->translation = "<font style='color:red;'>Error ".$data->responseStatus.': '.$details.'</font>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |                  } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |                  return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |              } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |              $transObtained = $data->responseData->translatedText; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |              $this->translation = utf8_decode(ucfirst(strtolower(trim($transObtained)))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |              $this->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |              // Checking debug setting to determinate how to output translation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |              if ($this->debug === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |                  $this->translation = "<font style='color:#00CC00;'>".$this->translation.'</font>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |              } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |              return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |              fclose($socket); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |          } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |              //host offline | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |              $this->hostDown(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |          } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |      } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |      * Get translation from apertium API. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |     private function apertiumTrans() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |         // Check if it can be translated from online sources. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |         $host = 'api.apertium.org'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |         if ($socket = @fsockopen($host, 80, $errno, $errstr, 30)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |             // Host online | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |             $urlString = urlencode($this->string); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |             $url = "http://$host/json/translate?q=$urlString&langpair=$this->from%7C$this->to"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |             $json = file_get_contents($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |             $data = json_decode($json); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |             // Checking response status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |             if ($data->responseStatus != 200) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |                 if ($this->debug === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |                     $this->translation = "<font style='color:red;'>Error ".$data->responseStatus.': '.$data->responseDetails.'</font>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |                 return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 |  |  |             $transObtained = $data->responseData->translatedText; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |             $this->translation = ucfirst(strtolower(trim(str_replace('*', ' ', $transObtained)))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 |  |  |             // Checking debug setting to determinate how to output translation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |             if ($this->debug === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |                 $errors = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |                 $words = explode(' ', $transObtained); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |                 foreach ($words as $word) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |                     if ($word != '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |                         if ($word[0] == '*') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |                             $errors = $errors.substr($word, 1).', '; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |                         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |                 if ($errors == '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |                     $this->translation = "<font style='color:#00CC00;'>".$this->translation.'</font>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |                     $this->translation = "<font style='color:orange;'>Unknoun words: ".substr($errors, 0, -2).'</font>'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 |  |  |             fclose($socket); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |             //host offline | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |             $this->hostDown(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |      * This fuction is called when host is down, and it would set translation if debug is true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 |  |  |     private function hostDown() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 |  |  |         if ($this->debug === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 |  |  |             $this->translation = "<font style='color:red;'>$this->translator host is down</font>"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 |  |  |     /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |      * This fuction is called by trans() function of Fadade Laralang | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 |  |  |      * It would call run() function of this class and returns the translation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |     public function __toString() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |         $this->run(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |         return $this->translation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 316 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 317 |  |  |  | 
            
                        
When comparing two booleans, it is generally considered safer to use the strict comparison operator.