Skip to content

Commit

Permalink
fix get all translations && add whereTranslatable
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAhmedGad committed Jul 11, 2019
1 parent 9fd96a9 commit 827e996
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/Http/Controllers/TranslationController.php
Expand Up @@ -50,7 +50,7 @@ public function store(Request $request)
$files = new LangFilesService();
$files->createNewLangFilesFromDefault($locale->iso);

return redirect(action('Motwreen\Translation\Http\Controllers\TranslationController@index'))->with(['success'=>'Locale Created Successfully']);
return redirect(action('\Motwreen\Translation\Http\Controllers\TranslationController@index'))->with(['success'=>'Locale Created Successfully']);
}

public function show($locale)
Expand All @@ -75,7 +75,7 @@ public function destroy($locale)
$locale->delete();
$files = new LangFilesService();
$files->deleteDirectory($locale->iso);
return redirect(action('Motwreen\Translation\Http\Controllers\TranslationController@index'))->with(['success'=>'Locale deleted Successfully']);
return redirect(action('\Motwreen\Translation\Http\Controllers\TranslationController@index'))->with(['success'=>'Locale deleted Successfully']);
}

/**
Expand Down Expand Up @@ -121,7 +121,7 @@ public function saveTranslations(Locale $locale, Request $request)
$filesService->updateLangFile($locale->iso,$request->get('file'),$newKeysToAddInBoathFiles['other']);
}

return redirect(action('Motwreen\Translation\Http\Controllers\TranslationController@show',[$locale]))->with(['success'=>'Your Translations saved successfully']);
return redirect(action('\Motwreen\Translation\Http\Controllers\TranslationController@show',[$locale]))->with(['success'=>'Your Translations saved successfully']);
}

public function readLangFileAjax(Request $request)
Expand Down
39 changes: 33 additions & 6 deletions src/Traits/TranslatableTrait.php
Expand Up @@ -46,12 +46,25 @@ public function translations(){
return $this->hasMany( Translation::class,'model_id')->where('model',get_class($this));
}

// public function getAllTranslations()
// {
// $locales = $this->locales();
// foreach ($locales as $locale)
// $trans[$locale->iso]=$this->getTranslatedAttributes($locale->id);
// return $trans;
// }

public function getAllTranslations()
{
$trans = [];
$locales = $this->locales();
foreach ($locales as $locale)
$trans[$locale->iso]=$this->getTranslatedAttributes($locale->id);
return $trans;
foreach ($this->translatable as $attribute){
foreach ($locales as $locale){
$trans[$attribute][$locale->iso] = $this->getTranslation($attribute,$locale->id);
}
}
$this->attributes = array_merge($this->attributes,$trans);
return $this;
}

public function getTranslatedAttributes($locale=null)
Expand Down Expand Up @@ -114,11 +127,10 @@ protected function setTranslation($key, $value, $locale = NULL,$model_id)
$translation->model_id = $model_id;
$translation->attribute = $key;
$translation->locale_id = $locale;
$translation->value = $value;
$translation->value = $value??"";
} else {
$translation->value = $value;
$translation->value = $value??"";
}

return $translation->save();
}

Expand Down Expand Up @@ -158,6 +170,21 @@ public function deleteTranslations($locale = null)
$translations->delete();
}

public function scopeWhereTranslatable($query,$attribute,$operator="=",$value=null)
{
if(!in_array($attribute,$this->translatable))
return $query;

if(func_num_args() < 4){
$value = $operator;
$operator = "=";
}
return $query->whereHas('translations',function ($translations)use($attribute,$operator,$value){
$translations->where('attribute',$attribute)->where('value',$operator,$value);
});
}


public static function makeSlug($string, $locale = null)
{
if (!$locale) {
Expand Down

0 comments on commit 827e996

Please sign in to comment.