Пример #1
0
 public static function generateTranslation($phrase, $thisLang)
 {
     if ($phrase && $thisLang) {
         $checkPresentPhrase = self::where("phrase", "like", $phrase)->first();
         if (!isset($checkPresentPhrase->id)) {
             $newPhrase = self::create(["phrase" => $phrase]);
             try {
                 $langsDef = Config::get('translations::config.def_locale');
                 $langsAll = Config::get('translations::config.alt_langs');
                 foreach ($langsAll as $lang) {
                     $lang = str_replace("ua", "uk", $lang);
                     $langsDef = str_replace("ua", "uk", $langsDef);
                     $translator = new Translator(Config::get('builder::translate_cms.api_yandex_key'));
                     $translation = $translator->translate($phrase, $langsDef . '-' . $lang);
                     $lang = str_replace("uk", "ua", $lang);
                     if (isset($translation->getResult()[0])) {
                         Translate::create(["id_translations_phrase" => $newPhrase->id, "lang" => $lang, "translate" => $translation->getResult()[0]]);
                     } else {
                         return "error.No get results";
                     }
                 }
             } catch (Yandex\Translate\Exception $e) {
                 return $e->getMessage();
                 // handle exception
             }
             self::reCacheTrans();
             $arrayTranslate = Trans::fillCacheTrans();
             if (isset($arrayTranslate[$phrase][$thisLang])) {
                 $phraseReturn = $arrayTranslate[$phrase][$thisLang];
             } else {
                 $phraseReturn = "Ошибка перевода";
             }
             return $phraseReturn;
         } else {
             $translatePhrase = Translate::where("id_translations_phrase", $checkPresentPhrase->id)->where("lang", "like", $thisLang)->first();
             if (isset($translatePhrase->translate)) {
                 return $translatePhrase->translate;
             }
         }
     }
 }
 public function doSavePhrase()
 {
     $lang = Input::get("name");
     $phrase = Input::get("value");
     $id = Input::get("pk");
     if ($id && $phrase && $lang) {
         $phrase_change = Translate::where("id_translations_phrase", $id)->where("lang", $lang)->first();
         if (isset($phrase_change->id)) {
             $phrase_change->translate = $phrase;
             $phrase_change->save();
         } else {
             Translate::create(["id_translations_phrase" => $id, "lang" => $lang, "translate" => $phrase]);
         }
     }
     Trans::reCacheTrans();
 }
 private function check_db()
 {
     $langs = Config::get('translations::config.alt_langs');
     foreach ($this->arr_fraze as $el) {
         $el_slashes = addslashes($el);
         $check_phrase = DB::table("translations_phrases")->whereRaw(" phrase like BINARY '{$el_slashes}' ")->count();
         if ($check_phrase == 0) {
             $model = new Trans();
             $model->phrase = trim($el_slashes);
             $model->save();
             $id_last = $model->id;
             foreach ($langs as $k => $ellg) {
                 $lg = $ellg;
                 if ($ellg == "ua") {
                     $ellg = "uk";
                 }
                 $def_lang = Config::get('translations::config.def_locale');
                 if ($def_lang == "ua") {
                     $def_lang = "uk";
                 }
                 $translator = new Translator(Config::get('builder::translate_cms.api_yandex_key'));
                 $translation = $translator->translate($el, $def_lang . '-' . $ellg);
                 if (isset($translation->getResult()[0])) {
                     $text = $translation->getResult()[0];
                 }
                 if ($ellg == "uk") {
                     $ellg = "ua";
                 }
                 if ($text) {
                     $translate_rec = new Translate();
                     $translate_rec->lang = $ellg;
                     $translate_rec->translate = $text;
                     $translate_rec->id_translations_phrase = $id_last;
                     $translate_rec->save();
                 }
             }
         }
     }
 }