/** * */ public function import(Request $request) { Excel::filter('chunk')->load($request->only('file')['file'], 'UTF-8')->chunk(100, function ($language) { // Loop through all rows $language->each(function ($row) { $default = Locale::find(1); $locales = Locale::lists('id', 'name'); $langs = []; foreach ($row as $lang => $translation) { if (array_key_exists(ucfirst($lang), $locales->toArray())) { // if imported lang exist $ilang_id = $locales[ucfirst($lang)]; // if ($ilang_id == $default->id) { $langs[ucfirst($lang)]['translation'] = $translation; $langs[ucfirst($lang)]['id'] = $ilang_id; } else { $langs['child'][ucfirst($lang)]['translation'] = $translation; $langs['child'][ucfirst($lang)]['id'] = $ilang_id; } } } $new_trans = LocaleTranslation::firstOrNew(['locale_id' => $langs[$default->name]['id'], 'translation' => $langs[$default->name]['translation']]); $new_trans->save(); foreach ($langs['child'] as $cland => $ctrans) { $new_ctrans = LocaleTranslation::firstOrNew(['locale_id' => $ctrans['id'], 'translation_id' => $new_trans->id]); $new_ctrans->translation = $ctrans['translation']; $new_ctrans->save(); } }); }); return redirect()->route('admin.language.index')->withFlashSuccess('Imported'); }
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())); } }
/** * 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]); } }
public function dashboard() { // $this->middleware('auth'); $researchs = Research::count(); $funds = Fund::count(); $translations = Translation::count(); $users = User::count(); return view('dashboard', ['researchs' => $researchs, 'funds' => $funds, 'translations' => $translations, 'users' => $users]); }
/** * Run the database seeds. * * @return void */ public function run() { Model::unguard(); DB::table('translation')->delete(); $items = array(['english' => 'This', 'armenian' => 'այս', 'persian' => 'اس'], ['english' => 'That', 'armenian' => 'որ', 'persian' => 'کہ']); foreach ($items as $item) { Translation::create($item); } Model::reguard(); }
/** * 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|unique:translations,body']); if ($validator->passes()) { $translation = Translation::create(['body' => $request->input('body')]); $response = response()->json(['id' => $translation->getId()], 201); } else { $response = response()->json(['errors' => $validator->messages()->all()], 400); } return $response; }
public function save(array $options = []) { // before save code parent::save($options); // after save code $this->Translations()->delete(); foreach ($this->translatedAttributes as $field) { if (is_array($this->{$field})) { foreach ($this->{$field} as $k => $v) { \App\Translation::create(['table' => 'groupes', 'foreign_id' => $this->id, 'lang' => $k, 'field' => $field, 'value' => $v]); } } } }
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]]; }
public function createCollocations() { $translations = Translation::all(); if (!count($translations)) { foreach (range(1, 10) as $i) { $word = new Word(); $word->body = $this->faker->word; $word->ts = $this->faker->word; $word->save(); echo 'Word id: ' . $word->id . '<br>'; foreach (range(1, 3) as $j) { $translation = new Translation(); $translation->body = $this->faker->word; $word->translations()->save($translation); echo '->Translation id: ' . $translation->id . '<br>'; } } } else { echo 'Collocations already exist.<br>'; } }
public function updateTranslation(Request $request) { $lang_id = $request->get('lang_id'); //return $lang_id; die(); foreach ($lang_id as $id => $translation) { //return $id; die(); $translate = Translation::find($id); //return $translate;die(); if (!is_null($translate)) { //this is original or translated if (!is_null($translate->translation_id)) { //this is translated $original = $translate->original; } else { //this is original $original = $translate; } //return $original; die(); //$original->translated()->delete(); foreach ($translation as $lang => $string) { $locale = Locale::where('code', $lang)->first(); $child = Translation::firstOrNew(['locale_id' => $locale->id, 'translation_id' => $original->id]); $child->translation = $string; $child->original()->dissociate(); $child->locale()->dissociate(); $child->original()->associate($original); $child->locale()->associate($locale); $child->save(); } } else { $json['message'] = 'Something wrong!'; } } $json['status'] = true; $json['message'] = 'Translation updated!'; return json_encode($json); }
/** * 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 dashboard() { $researchs = Research::count(); $funds = Fund::count(); $translations = Translation::count(); $users = User::count(); return view('researchcenter.dashboard', ['researchs' => $researchs, 'funds' => $funds, 'translations' => $translations, 'users' => $users]); }
public function saveStatus(Request $request, $translate_id) { $translation = Translation::find($translate_id); $translation->status = $request->input('status'); $translation->save(); return redirect()->route('translate_list'); }