Skip to content

Commit

Permalink
Creating a loading backend translation hook
Browse files Browse the repository at this point in the history
  • Loading branch information
nWidart committed Oct 16, 2017
1 parent 633085d commit 2951b67
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
26 changes: 6 additions & 20 deletions Modules/Core/Composers/TranslationsViewComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,17 @@
namespace Modules\Core\Composers;

use Illuminate\Contracts\View\View;
use Modules\Core\Events\LoadingBackendTranslations;

class TranslationsViewComposer
{
public function compose(View $view)
{
$staticTranslations = json_encode([
'page' => array_dot(trans('page::pages')),
'pages' => array_dot(trans('page::pages')),
'core' => array_dot(trans('core::core')),
'media' => array_dot(trans('media::media')),
'folders' => array_dot(trans('media::folders')),
'roles' => array_dot(trans('user::roles')),
'users' => array_dot(trans('user::users')),
'sidebar' => array_dot(trans('core::sidebar')),
'dashboard' => array_dot(trans('dashboard::dashboard')),
'menu' => array_dot(trans('menu::menu')),
'menu-items' => array_dot(trans('menu::menu-items')),
'settings' => array_dot(trans('setting::settings')),
'tags' => array_dot(trans('tag::tags')),
'translations' => array_dot(trans('translation::translations')),
'workshop' => array_dot(trans('workshop::workshop')),
'modules' => array_dot(trans('workshop::modules')),
'themes' => array_dot(trans('workshop::themes')),
]);
if (app('asgard.onBackend') === false) {
return;
}
event($staticTranslations = new LoadingBackendTranslations());

$view->with(compact('staticTranslations'));
$view->with('staticTranslations', json_encode($staticTranslations->getTranslations()));
}
}
34 changes: 34 additions & 0 deletions Modules/Core/Events/LoadingBackendTranslations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Modules\Core\Events;

/**
* Hook LoadingBackendTranslations
* Triggered when loading the backend
* Used to send laravel translations to the frontend
* Example for VueJS
* @package Modules\Core\Events
*/
class LoadingBackendTranslations
{
private $translations = [];

public function getTranslations() : array
{
return $this->translations;
}

public function load($key, array $translations)
{
$this->translations = array_merge($this->translations, [$key => $translations]);

return $this;
}

public function loadMultiple(array $translations)
{
$this->translations = array_merge($this->translations, $translations);

return $this;
}
}

0 comments on commit 2951b67

Please sign in to comment.