Skip to content

Commit

Permalink
Firing event when a translation variable has been modified
Browse files Browse the repository at this point in the history
  • Loading branch information
tntsoft committed May 10, 2021
1 parent 7dd2fa9 commit 36bbd5e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Events/UpdatedTranslations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class UpdatedTranslations
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('sync-translations');
}
}
2 changes: 2 additions & 0 deletions src/Http/Controllers/LanguageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace JoeDixon\Translation\Http\Controllers;

use App\Events\UpdatedTranslations;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use JoeDixon\Translation\Drivers\Translation;
Expand Down Expand Up @@ -30,6 +31,7 @@ public function create()

public function store(LanguageRequest $request)
{
UpdatedTranslations::dispatch();
$this->translation->addLanguage($request->locale, $request->name);

return redirect()
Expand Down
5 changes: 5 additions & 0 deletions src/Http/Controllers/LanguageTranslationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace JoeDixon\Translation\Http\Controllers;

use App\Events\UpdatedTranslations;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -56,8 +57,10 @@ public function store(TranslationRequest $request, $language)
if ($request->filled('group')) {
$namespace = $request->has('namespace') && $request->get('namespace') ? "{$request->get('namespace')}::" : '';
$this->translation->addGroupTranslation($language, "{$namespace}{$request->get('group')}", $request->get('key'), $request->get('value') ?: '');
UpdatedTranslations::dispatch();
} else {
$this->translation->addSingleTranslation($language, 'single', $request->get('key'), $request->get('value') ?: '');
UpdatedTranslations::dispatch();
}

return redirect()
Expand All @@ -69,8 +72,10 @@ public function update(Request $request, $language)
{
if (! Str::contains($request->get('group'), 'single')) {
$this->translation->addGroupTranslation($language, $request->get('group'), $request->get('key'), $request->get('value') ?: '');
UpdatedTranslations::dispatch();
} else {
$this->translation->addSingleTranslation($language, $request->get('group'), $request->get('key'), $request->get('value') ?: '');
UpdatedTranslations::dispatch();
}

return ['success' => true];
Expand Down
1 change: 1 addition & 0 deletions src/TranslationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ private function registerRoutes()
private function publishConfiguration()
{
$this->publishes([
__DIR__.'/Events/UpdatedTranslations.php' => base_path('/app/Events/UpdatedTranslations.php'),
__DIR__.'/../config/translation.php' => config_path('translation.php'),
], 'config');
}
Expand Down

0 comments on commit 36bbd5e

Please sign in to comment.