public function translate(Request $request)
 {
     $data = $request->only('word', 'src_language', 'trans_language');
     $validator = Validator::make($data, array('word' => 'required', 'src_language' => 'required', 'trans_language' => 'required'));
     if ($validator->passes()) {
         $columns = Schema::getColumnListing('translation');
         // users table
         if (!in_array($data['src_language'], $columns)) {
             return response()->json(array('error' => 1, 'labels' => 'Source language is not available'));
         } else {
             if (!in_array($data['trans_language'], $columns)) {
                 return response()->json(array('error' => 1, 'labels' => 'Translation language is available'));
             } else {
                 $tra = Translation::where($data['src_language'], $data['word'])->first();
                 if (!$tra) {
                     return response()->json(array('error' => 1, 'labels' => 'This word is not available in our source language'));
                 } else {
                     $data = array('word' => $data['word'], 'source' => $data['src_language'], 'target' => $data['trans_language'], 'result' => $tra->{$data}['trans_language']);
                     return response()->json(array('error' => 0, 'data' => $data, 'errors' => array()));
                 }
             }
         }
     } else {
         return response()->json(array('error' => 1, 'labels' => $validator->messages()));
     }
 }
 /**
  * Display a listing of the resource.
  *
  * @return \Illuminate\Http\Response
  */
 public function index()
 {
     if ($body = \Request::input('body')) {
         $translation = Translation::where('body', $body)->first();
         if ($translation) {
             $response = response()->json([['id' => $translation->getId(), 'body' => $translation->body]]);
         } else {
             $response = response()->json(['errors' => ['The translation hasn\'t found.']], 404);
         }
     } else {
         if ($autocomplete = \Request::input('autocomplete')) {
             $translations = Translation::select('body')->where('body', 'LIKE', "{$autocomplete}%")->take(\Request::header('Limit') ?: 5)->get();
             if (count($translations) > 0) {
                 $response = response()->json($translations);
             } else {
                 $response = response()->json(['errors' => ['The matched translations haven\'t found.']], 404);
             }
         } else {
             $result = Translation::paginate(\Request::header('Limit') ?: 10);
             $headers['Current-Page'] = $result->currentPage();
             $headers['Last-Page'] = $result->lastPage();
             $translations = [];
             foreach ($result as $key => $item) {
                 $translations[$key]['id'] = $item->getId();
                 $translations[$key]['body'] = $item->body;
             }
             if (count($translations) > 0) {
                 $response = response()->json($translations, 200, $headers);
             } else {
                 $response = response()->json(['errors' => ['there aren\'t any translations.']], 404);
             }
         }
     }
     return $response;
 }
 /**
  * Update a single locale or a group of them for a group.needle.
  * For a single locale, use locale.group.needle notation as the first
  * param and a string as the second one. For a group of locales, use as the
  * first param group.needle notation, and an [locale => text] array as the
  * second one. 
  * For a secure needle or group string updating, see updateGroupNeedle method.
  * 
  * @param string $localeGroupNeedle E.g., 'fruits.apple' or 'es.fruits.apple'
  * @param string|array $texts Texts for locales. E.g., ['es' => 'manzana', 'en' => 'apple']
  * @param array $extra Extra fillable columns. E.g., ['type' => 'home']
  */
 public function update($localeGroupNeedle, $texts, array $extra = [])
 {
     $pointer = $this->parseLocaleGroupNeedle($localeGroupNeedle);
     //Updated locales list. It will be used as a parameter for event
     $locales = [];
     if (is_null($pointer->needle)) {
         return false;
     }
     //Extra paramenter can not have sensitive attributes. So, we remove them.
     $extra = $this->removeSensitiveAttributes($extra);
     //Two ways: a single locale to update or a list of locales.
     if (!is_null($pointer->locale)) {
         if (!is_array($texts)) {
             $row = $this->model->where('group', $pointer->group)->where('needle', $pointer->needle)->where('locale', $pointer->locale)->first();
             if (count($row)) {
                 $row->update(array_merge($extra, ['text' => $texts]));
                 $locales[] = $pointer->locale;
             }
         }
     } else {
         foreach ($texts as $locale => $text) {
             $row = $this->model->where('group', $pointer->group)->where('needle', $pointer->needle)->where('locale', $locale)->first();
             if (!count($row)) {
                 continue;
             }
             $row->update(array_merge($extra, ['text' => $text]));
             $locales[] = $locale;
         }
     }
     if (count($locales)) {
         $this->event->fire('translator.updated', [$locales, $pointer->group, $pointer->needle]);
     }
 }
 private function translate($word, $sourceLang, $targetLang)
 {
     $translate = Translation::where($sourceLang, strtolower($word))->first();
     if ($translate === NULL) {
         return ['data' => ['error' => 'There is no results for request']];
     }
     $result_word = $translate->{$targetLang};
     if ($result_word !== NULL) {
         if ($this->startsWithUpper($word)) {
             $result_word = mb_convert_case($result_word, MB_CASE_TITLE, "UTF-8");
         }
         if ($this->fullUpperString($word)) {
             $result_word = mb_convert_case($result_word, MB_CASE_UPPER, "UTF-8");
         }
     }
     return ['data' => ['word' => $word, 'source' => $sourceLang, 'target' => $targetLang, 'result' => $result_word]];
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  *
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     $validator = \Validator::make($request->all(), ['body' => 'required', 'translation_id' => 'sometimes|exists:translations,id', 'via_dictionary' => 'sometimes|boolean']);
     if ($validator->passes()) {
         if ($request->input('via_dictionary')) {
             $definitions = \YaDictionary::lookup($request->input('body'));
             if ($definitions) {
                 foreach ($definitions as $definition) {
                     $partOfSpeech = $definition->getPartOfSpeech();
                     if (!($position = Position::where('body', $partOfSpeech)->first())) {
                         $position = Position::create(['body' => $partOfSpeech]);
                     }
                     if (!Word::where('body', $definition->getText())->where('position_id', $position->id)->first()) {
                         $word = new Word();
                         $word->body = $definition->getText();
                         $word->ts = $definition->getTranscription();
                         $word->position()->associate($position);
                         $word->save();
                         $translationIds = [];
                         foreach ($definition->getTranslations() as $yaTranslation) {
                             if (!($translation = Translation::where('body', $yaTranslation->getText())->first())) {
                                 $translation = Translation::create(['body' => $yaTranslation->getText()]);
                             }
                             $translationIds[] = $translation->id;
                         }
                         $word->translations()->attach($translationIds);
                     }
                 }
                 $response = response('This word has created from the dictionary', 201);
             } else {
                 $response = response()->json(['errors' => ['This word has not found in the dictionary.']], 404);
             }
         } else {
             if ($translationId = $request->input('translation_id')) {
                 $word = Word::create(['body' => $request->input('body')]);
                 $word->translations()->attach($translationId);
                 $response = response()->json(['id' => $word->getId()], 201);
             } else {
                 $response = response()->json(['errors' => ['It needs a translation.']], 400);
             }
         }
     } else {
         $response = response()->json(['errors' => $validator->messages()->all()], 400);
     }
     return $response;
 }
 public function listTranslate()
 {
     if (Auth::user()->is('admin')) {
         $type = 'admin';
         $translate = Translation::paginate(20);
     } else {
         $type = 'user';
         $translate = Translation::where('owner', Auth::user()->id)->paginate(20);
     }
     return view('translate.list', ['type' => $type, 'translates' => $translate]);
 }