diff --git a/app/Http/Controllers/TranslationController.php b/app/Http/Controllers/TranslationController.php new file mode 100644 index 0000000..bd0878a --- /dev/null +++ b/app/Http/Controllers/TranslationController.php @@ -0,0 +1,105 @@ +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 + */ + public function get(string $locale = 'id') + { + app()->setLocale($locale); + + return $this->all() ?: '{}'; + } + + /** + * @return array|null + */ + private function all() + { + return File::exists($this->path()) ? json_decode(File::get($this->path()), true) : []; + } + + /** + * @param \Illuminate\Http\Request $request + * @param string $locale + * @return \Illuminate\Http\Response + */ + public function register(Request $request, string $locale = 'id') + { + $request->validate([ + 'text' => 'required|string', + ]); + + app()->setLocale($locale); + + $all = $this->all(); + + if (!array_key_exists($request->text, $all)) { + $all[$request->text] = $request->text; + } + + return File::put($this->path(), json_encode( + $all, JSON_PRETTY_PRINT + )); + } +} diff --git a/app/functions.php b/app/functions.php new file mode 100644 index 0000000..dad2f67 --- /dev/null +++ b/app/functions.php @@ -0,0 +1,40 @@ +getLocale() . '.json'); + + return File::exists($path) ? json_decode(File::get($path), true) : []; + }; + + if (is_null($key)) { + return $key; + } + + if (is_string($key)) { + $all = $allDefinedTranslation(); + + if (!array_key_exists($key, $all)) { + $all[$key] = $key; + + $path = lang_path(app()->getLocale() . '.json'); + + File::put($path, json_encode($all, JSON_PRETTY_PRINT)); + } + } + + return trans($key, $replace, $locale); + } +} \ No newline at end of file diff --git a/config/app.php b/config/app.php index 6484af0..2f595be 100644 --- a/config/app.php +++ b/config/app.php @@ -69,7 +69,7 @@ return [ | */ - 'timezone' => 'UTC', + 'timezone' => 'Asia/Jakarta', /* |-------------------------------------------------------------------------- @@ -82,7 +82,7 @@ return [ | */ - 'locale' => 'en', + 'locale' => 'id', /* |-------------------------------------------------------------------------- @@ -108,7 +108,7 @@ return [ | */ - 'faker_locale' => 'en_US', + 'faker_locale' => 'id_ID', /* |-------------------------------------------------------------------------- diff --git a/database/seeders/MenuSeeder.php b/database/seeders/MenuSeeder.php index c05a4d2..57970a3 100644 --- a/database/seeders/MenuSeeder.php +++ b/database/seeders/MenuSeeder.php @@ -108,6 +108,17 @@ class MenuSeeder extends Seeder ])->get(['id']) ); + $translation = $builtin->childs()->create([ + 'name' => 'translation', + 'route_or_url' => 'superuser.translation.index', + 'icon' => 'language', + 'position' => 5, + 'deleteable' => false, + 'actives' => [ + 'superuser.translation.*', + ], + ]); + $activities = Menu::create([ 'name' => 'activities', 'icon' => 'address-card', diff --git a/public/index.php b/public/index.php index 1d69f3a..4dd1860 100644 --- a/public/index.php +++ b/public/index.php @@ -20,6 +20,8 @@ if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) require $maintenance; } +require __DIR__ . '/../app/functions.php'; + /* |-------------------------------------------------------------------------- | Register The Auto Loader diff --git a/resources/js/Components/DashboardLayout/Sidebar/Link.vue b/resources/js/Components/DashboardLayout/Sidebar/Link.vue index 34c1aa0..f2e2af0 100644 --- a/resources/js/Components/DashboardLayout/Sidebar/Link.vue +++ b/resources/js/Components/DashboardLayout/Sidebar/Link.vue @@ -13,10 +13,14 @@ const link = route().has(menu.route_or_url) ? route(menu.route_or_url) : menu.ro \ No newline at end of file diff --git a/resources/js/Components/DashboardLayout/Sidebar/Links.vue b/resources/js/Components/DashboardLayout/Sidebar/Links.vue index c570422..553d8cf 100644 --- a/resources/js/Components/DashboardLayout/Sidebar/Links.vue +++ b/resources/js/Components/DashboardLayout/Sidebar/Links.vue @@ -43,10 +43,14 @@ const open = ref(active ? true : false)