diff --git a/app/Http/Controllers/TranslationController.php b/app/Http/Controllers/TranslationController.php index 5191bba..bd0878a 100644 --- a/app/Http/Controllers/TranslationController.php +++ b/app/Http/Controllers/TranslationController.php @@ -5,9 +5,61 @@ namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\File; +use Inertia\Inertia; +use Throwable; class TranslationController extends Controller { + /** + * @return string + */ + private function path() + { + return lang_path(app()->getLocale() . '.json'); + } + + /** + * @return \Illuminate\Http\Response + */ + public function index() + { + return Inertia::render('Translation/Index')->with([ + 'translations' => $this->all(), + ]); + } + + /** + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\Response + */ + public function update(Request $request) + { + $request->validate([ + 'key' => 'required|string', + 'value' => 'required|string', + ]); + + $all = $this->all(); + $all[$request->key] = $request->value; + + try { + File::put($this->path(), json_encode( + $all, JSON_PRETTY_PRINT + )); + + return redirect()->back()->with('success', __( + '`:key` has been translated to `:value`', [ + 'key' => $request->key, + 'value' => $request->value, + ], + )); + } catch (Throwable $e) { + return redirect()->back()->with('error', __( + $e->getMessage() + )); + } + } + /** * @param string $locale * @return \Illuminate\Http\Response @@ -16,19 +68,15 @@ class TranslationController extends Controller { app()->setLocale($locale); - return $this->all() ?? '{}'; + return $this->all() ?: '{}'; } /** * @return array|null */ - public function all() + private function all() { - $path = lang_path(app()->getLocale() . '.json'); - - if (File::exists($path)) { - return json_decode(File::get($path), true); - } + return File::exists($this->path()) ? json_decode(File::get($this->path()), true) : []; } /** @@ -44,15 +92,13 @@ class TranslationController extends Controller app()->setLocale($locale); - $all = $this->all() ?? []; + $all = $this->all(); if (!array_key_exists($request->text, $all)) { $all[$request->text] = $request->text; } - $path = lang_path($locale . '.json'); - - return File::put($path, json_encode( + return File::put($this->path(), json_encode( $all, JSON_PRETTY_PRINT )); } diff --git a/resources/js/Pages/Translation/Index.vue b/resources/js/Pages/Translation/Index.vue new file mode 100644 index 0000000..0317c99 --- /dev/null +++ b/resources/js/Pages/Translation/Index.vue @@ -0,0 +1,80 @@ + + + \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index 5ee3ff0..f3ee23e 100644 --- a/routes/web.php +++ b/routes/web.php @@ -43,6 +43,11 @@ Route::middleware(['auth:sanctum', config('jetstream.auth_session'), 'verified'] Route::resource('menu', App\Http\Controllers\Superuser\MenuController::class)->only([ 'index', 'store', 'update', 'destroy', ])->middleware(['permission:read menu']); + + Route::prefix('/translation')->name('translation.')->controller(App\Http\Controllers\TranslationController::class)->group(function () { + Route::get('/', 'index')->name('index'); + Route::patch('/', 'update')->name('update'); + }); Route::get('/activity/login', [App\Http\Controllers\ActivityController::class, 'login'])->name('activity.login'); });