/**
  * Display a listing of the resource.
  *
  * @param string $language
  * @param string $group
  * @return \Illuminate\Http\Response
  */
 public function index($language = '1', $group = '1')
 {
     $translations = $this->repository->fetch($language, $group)->load('groups', 'language');
     $groups = $this->repository->groups();
     $languages = $this->repository->languages();
     return view('translation.index', compact('translations', 'groups', 'languages'));
 }
 /**
  * Execute the command.
  *
  * @param Filesystem $fileSystem
  * @param Application $app
  * @param TranslationRepositoryInterface $repository
  * @param Dispatcher $event
  * @return Collection of Group
  */
 public function handle(Filesystem $fileSystem, Application $app, TranslationRepositoryInterface $repository, Dispatcher $event)
 {
     $files = $fileSystem->allFiles($app->langPath());
     /**
      * Retrieves all local groups
      */
     $groups = collect($files)->transform(function ($file) {
         return pathinfo($file)['filename'];
     })->unique();
     /**
      * Save Database instance with all groups
      */
     $database = $repository->groups();
     /**
      * List Only names
      */
     $names = $database->pluck('name');
     /**
      * Create New Group for those which has been set locally
      * but was not present yet on the database
      */
     $newGroups = $groups->merge($names)->diff($names)->map(function ($name) {
         return $this->dispatch(new CreateGroupCommand($name));
     });
     /**
      * Announce GroupWasCreated
      */
     if (!$newGroups->isEmpty()) {
         $event->fire(new GroupsWasCreated($newGroups));
     }
     /**
      * Returns All groups
      */
     return $database->merge($newGroups);
 }
 /**
  * Execute the command.
  *
  * @param TranslationRepositoryInterface $repository
  */
 public function handle(TranslationRepositoryInterface $repository)
 {
     /**
      * Retrieve all languages for further usage
      */
     $allLanguages = $repository->languages();
     /**
      * Get All Groups
      */
     $groups = $repository->groups()->load('translations', 'translations.language');
     /**
      * For Each Group Execute Operation
      */
     $groups->map(function ($group) use($allLanguages) {
         /**
          * Get All Languages Contained within this Group
          */
         $languages = $group->translations->groupBy('language.id');
         /**
          * Check if all languages are contained within $languages, otherwise append it
          * with an empty collection so it will generate all keys, values for this language
          * as it means the language does not have any translation at the moment
          */
         $missingLanguages = collect($allLanguages->keyBy('id')->keys())->diff($languages->keys());
         $missingLanguages->each(function ($language) use($languages) {
             $languages->put($language, collect());
         });
         /**
          * Merge All Values in order to get uniques
          */
         $merged = $languages->map(function ($translations) {
             return $translations->lists('key');
         })->collapse();
         /**
          * Now for each language extract the merged groups in order to get new keys
          */
         $languages->each(function ($translations, $language) use($merged, $group) {
             $newValues = collect($merged)->diff($translations->lists('key'));
             /**
              * For each new Value, Create a new Translation
              */
             $newValues->each(function ($key) use($language, $group) {
                 $this->dispatch(new CreateTranslationCommand($language, $group->id, compact('key')));
             });
         });
     });
 }