create translation crud
This commit is contained in:
@@ -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
|
||||
));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user