From bd8e2cb6f3e9ffc914ec90901849ac9f6cce9785 Mon Sep 17 00:00:00 2001 From: dkc Date: Sat, 8 Jul 2023 07:51:03 +0900 Subject: [PATCH 01/11] Add Language Switcher Resource Files --- stubs/app/Http/Kernel.stub | 1 + stubs/app/Http/Middleware/Localization.php | 26 ++++++++++++++++++++++ stubs/default/routes/web.stub | 8 +++++++ 3 files changed, 35 insertions(+) create mode 100644 stubs/app/Http/Kernel.stub create mode 100644 stubs/app/Http/Middleware/Localization.php create mode 100644 stubs/default/routes/web.stub diff --git a/stubs/app/Http/Kernel.stub b/stubs/app/Http/Kernel.stub new file mode 100644 index 0000000..2ffae4b --- /dev/null +++ b/stubs/app/Http/Kernel.stub @@ -0,0 +1 @@ +\App\Http\Middleware\Localization::class, \ No newline at end of file diff --git a/stubs/app/Http/Middleware/Localization.php b/stubs/app/Http/Middleware/Localization.php new file mode 100644 index 0000000..5c92717 --- /dev/null +++ b/stubs/app/Http/Middleware/Localization.php @@ -0,0 +1,26 @@ +setLocale($locale); + session()->put('locale', $locale); + + return redirect()->back(); +}); From 05e0d9b596de61c6ac3b439dd97213b98129036c Mon Sep 17 00:00:00 2001 From: dkc Date: Sat, 8 Jul 2023 07:51:19 +0900 Subject: [PATCH 02/11] Add Language Switcher Install Command --- src/Commands/InstallLanguageSwitcher.php | 66 ++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/Commands/InstallLanguageSwitcher.php diff --git a/src/Commands/InstallLanguageSwitcher.php b/src/Commands/InstallLanguageSwitcher.php new file mode 100644 index 0000000..63b937a --- /dev/null +++ b/src/Commands/InstallLanguageSwitcher.php @@ -0,0 +1,66 @@ +info('言語切替用のRoute language/{locale} を準備します'); + + // 実行済みなら実行しない + $routesFile = file_get_contents(base_path('routes/web.php')); + if (strpos($routesFile, '// Language Switcher Route 言語切替用ルートだよ') !== false) { + $this->info('言語切替用の Route は既に登録済みです'); + } else { + file_put_contents( + base_path('routes/web.php'), + file_get_contents(__DIR__.'/../../stubs/default/routes/web.stub'), + FILE_APPEND + ); + } + + // If Middleware Localization is already installed, skip + if (file_exists(base_path('app/Http/Middleware/Localization.php'))) { + $this->info('言語切替用の Middleware は既に登録済みです'); + } else { + $this->info('言語切替用の Middleware を準備します'); + copy(__DIR__.'/../../stubs/app/Http/Middleware/Localization.php', base_path('app/Http/Middleware/Localization.php')); + (new Filesystem)->copyDirectory(__DIR__.'/../../stubs/lang/', lang_path()); + } + + $this->info('Kernel に Middleware を登録します'); + // Read the contents of the file into a string + $file = base_path('app/Http/Kernel.php'); + $contents = file_get_contents($file); + + // 実行済みなら実行しない + if (strpos($contents, '\App\Http\Middleware\Localization::class,') !== false) { + $this->info('言語切替用の Middleware は既に登録済みです'); + return self::SUCCESS; + } + // Kernel内の既存の \App\Http\Middleware\VerifyCsrfToken::class の位置を取得 + $position = strpos($contents, '\App\Http\Middleware\VerifyCsrfToken::class,'); + if ($position !== false) { + $appendText = file_get_contents(__DIR__.'/../../stubs/app/Http/Kernel.stub'); + + $contents = substr_replace($contents, $appendText, $position, 0); + file_put_contents($file, $contents); + + $this->info('Language Switherのインストールが完了しました!'); + return self::SUCCESS; + } + + $this->error('Language Switherのインストールが失敗しました!'); + + return self::FAILURE; + } +} From 40d074b048a28057f905ecf1886cb92ecd995f3f Mon Sep 17 00:00:00 2001 From: dkc Date: Sat, 8 Jul 2023 07:51:35 +0900 Subject: [PATCH 03/11] Support Language Switcher option --- src/Commands/BreezejpCommand.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Commands/BreezejpCommand.php b/src/Commands/BreezejpCommand.php index 3f64a4f..94d4be7 100644 --- a/src/Commands/BreezejpCommand.php +++ b/src/Commands/BreezejpCommand.php @@ -7,12 +7,19 @@ class BreezejpCommand extends Command { - public $signature = 'breezejp'; + use InstallLanguageSwitcher; + + public $signature = 'breezejp {--langswitch : Install Language Switcher 言語切替機能のインストール}'; public $description = 'Add Japanese Translation files for Laravel Breeze'; public function handle(): int { + // Install Language Switcher 言語切替機能をインストール + if ($this->option('langswitch')) { + return $this->installLanguageSwitcher(); + } + $this->info('Laravel Breeze用に日本語翻訳ファイルを準備します'); (new Filesystem)->ensureDirectoryExists(lang_path()); From 3a7d23de016442f28693006135ef11e771ba994b Mon Sep 17 00:00:00 2001 From: dkc Date: Sat, 8 Jul 2023 07:51:52 +0900 Subject: [PATCH 04/11] =?UTF-8?q?=E8=A8=80=E8=AA=9E=E5=88=87=E6=9B=BF?= =?UTF-8?q?=E6=A9=9F=E8=83=BD=E3=82=A4=E3=83=B3=E3=82=B9=E3=83=88=E3=83=BC?= =?UTF-8?q?=E3=83=AB=E7=AE=87=E6=89=80=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index d5dcd5c..45da09c 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,7 @@ php artisan breezejp - [Laravel Langと何が違うの?](#laravel-langと何が違うの) - [おまけ](#おまけ) - [言語切り替えサンプルアプリ](#言語の切り替えサンプルアプリ) +- [言語切替機能のインストール](#言語の切り替え機能のインストール) - [変更履歴](#変更履歴) - [貢献について](#貢献について) - [セキュリティや脆弱性について](#セキュリティや脆弱性について) @@ -354,6 +355,37 @@ MAIL_FROM_NAME="${APP_NAME}" ![251501263-d807d110-971e-44c0-a284-9e1b57c73894](https://github.com/askdkc/breezejp/assets/7894265/d52738c5-c6ae-4f92-87ef-0046f8cff4f7) +## 言語の切り替え機能のインストール +サンプルアプリなんて面倒だよ、さっさと言語切り替え使いたいよ!という人のために、言語切り替え機能をインストールする方法をご紹介します🤗 + +```bash +php artisan breezejp --langswitch +``` + +後は `/language/{locale}` にアクセスするだけで言語が切り替わるので、そこを叩くリクエストを送ってご利用ください + +#### Laravelアプリの起動 + +```bash +php artisan serve +``` + +- 日本語に切り替える例 + +``` +http://127.0.0.1:8000/language/ja +``` + +- 英語に切り替える例 + +``` +http://127.0.0.1:8000/language/ja +``` + +これでLaravel Breezeの各種メニューの言語が切り替わるのが確認できると思います🤯 + + +簡単でしょ?😁 ## 変更履歴 From 8ef470bdf81b24b4ffaf62e5b90290abfe896091 Mon Sep 17 00:00:00 2001 From: askdkc Date: Fri, 7 Jul 2023 22:52:21 +0000 Subject: [PATCH 05/11] Fix styling --- src/Commands/InstallLanguageSwitcher.php | 3 ++- stubs/app/Http/Middleware/Localization.php | 13 ++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Commands/InstallLanguageSwitcher.php b/src/Commands/InstallLanguageSwitcher.php index 63b937a..f3cb7b3 100644 --- a/src/Commands/InstallLanguageSwitcher.php +++ b/src/Commands/InstallLanguageSwitcher.php @@ -2,7 +2,6 @@ namespace Askdkc\Breezejp\Commands; -use Illuminate\Console\Command; use Illuminate\Filesystem\Filesystem; trait InstallLanguageSwitcher @@ -45,6 +44,7 @@ public function installLanguageSwitcher(): int // 実行済みなら実行しない if (strpos($contents, '\App\Http\Middleware\Localization::class,') !== false) { $this->info('言語切替用の Middleware は既に登録済みです'); + return self::SUCCESS; } // Kernel内の既存の \App\Http\Middleware\VerifyCsrfToken::class の位置を取得 @@ -56,6 +56,7 @@ public function installLanguageSwitcher(): int file_put_contents($file, $contents); $this->info('Language Switherのインストールが完了しました!'); + return self::SUCCESS; } diff --git a/stubs/app/Http/Middleware/Localization.php b/stubs/app/Http/Middleware/Localization.php index 5c92717..64e14b6 100644 --- a/stubs/app/Http/Middleware/Localization.php +++ b/stubs/app/Http/Middleware/Localization.php @@ -10,17 +10,16 @@ class Localization { /** - * Handle an incoming request. - * - * @param \Illuminate\Http\Request $request - * @param \Closure $next - * @return mixed - */ + * Handle an incoming request. + * + * @return mixed + */ public function handle(Request $request, Closure $next) { if (Session::has('locale')) { App::setLocale(Session::get('locale')); } + return $next($request); } -} \ No newline at end of file +} From 43a875bf934d14a740f1b7415ec9b8f6695e8369 Mon Sep 17 00:00:00 2001 From: dkc Date: Sat, 8 Jul 2023 08:14:26 +0900 Subject: [PATCH 06/11] Remove wrong php doc type --- src/Commands/InstallLanguageSwitcher.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Commands/InstallLanguageSwitcher.php b/src/Commands/InstallLanguageSwitcher.php index 63b937a..73604d2 100644 --- a/src/Commands/InstallLanguageSwitcher.php +++ b/src/Commands/InstallLanguageSwitcher.php @@ -9,8 +9,6 @@ trait InstallLanguageSwitcher { /** * Install the Language Switcher stack. - * - * @return int|null */ public function installLanguageSwitcher(): int { From d7f0c69d311e0bb2182314d9509c0a8aba51ea0c Mon Sep 17 00:00:00 2001 From: dkc Date: Sat, 8 Jul 2023 08:42:25 +0900 Subject: [PATCH 07/11] Update phpunit.xml.dist --- phpunit.xml.dist | 55 ++++++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 34 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a6b7299..ad596c9 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,36 +1,23 @@ - - - - tests - - - - - ./src - - - - - - - - - - + + + + tests + + + + + + + + + + + + + + + ./src + + From 7b6fcb2dd0fcfec410d4c813aae09c3a3e9cef6a Mon Sep 17 00:00:00 2001 From: dkc Date: Sat, 8 Jul 2023 08:44:54 +0900 Subject: [PATCH 08/11] Fix Output of Language Switcher --- src/Commands/InstallLanguageSwitcher.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/InstallLanguageSwitcher.php b/src/Commands/InstallLanguageSwitcher.php index a6379ec..96f67a0 100644 --- a/src/Commands/InstallLanguageSwitcher.php +++ b/src/Commands/InstallLanguageSwitcher.php @@ -41,7 +41,7 @@ public function installLanguageSwitcher(): int // 実行済みなら実行しない if (strpos($contents, '\App\Http\Middleware\Localization::class,') !== false) { - $this->info('言語切替用の Middleware は既に登録済みです'); + $this->info('言語切替用の Middleware は Kernel に既に登録済みです'); return self::SUCCESS; } From 179af986a65e8822d23c34f5559e8c5037ae8ffd Mon Sep 17 00:00:00 2001 From: dkc Date: Sat, 8 Jul 2023 08:45:15 +0900 Subject: [PATCH 09/11] Add Language Switcher Install Command Test --- tests/Kernel.php.stub | 68 +++++++++++++++++++++++++++ tests/LanguageSwitcherInstallTest.php | 14 ++++++ tests/NoDebugCommandLeftTest.php | 5 ++ tests/TestCase.php | 24 ++++++++++ tests/web.php.stub | 31 ++++++++++++ 5 files changed, 142 insertions(+) create mode 100644 tests/Kernel.php.stub create mode 100644 tests/LanguageSwitcherInstallTest.php create mode 100644 tests/NoDebugCommandLeftTest.php create mode 100644 tests/web.php.stub diff --git a/tests/Kernel.php.stub b/tests/Kernel.php.stub new file mode 100644 index 0000000..494c050 --- /dev/null +++ b/tests/Kernel.php.stub @@ -0,0 +1,68 @@ + + */ + protected $middleware = [ + // \App\Http\Middleware\TrustHosts::class, + \App\Http\Middleware\TrustProxies::class, + \Illuminate\Http\Middleware\HandleCors::class, + \App\Http\Middleware\PreventRequestsDuringMaintenance::class, + \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, + \App\Http\Middleware\TrimStrings::class, + \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, + ]; + + /** + * The application's route middleware groups. + * + * @var array> + */ + protected $middlewareGroups = [ + 'web' => [ + \App\Http\Middleware\EncryptCookies::class, + \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, + \Illuminate\Session\Middleware\StartSession::class, + \Illuminate\View\Middleware\ShareErrorsFromSession::class, + \App\Http\Middleware\VerifyCsrfToken::class, + \Illuminate\Routing\Middleware\SubstituteBindings::class, + ], + + 'api' => [ + // \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class, + \Illuminate\Routing\Middleware\ThrottleRequests::class.':api', + \Illuminate\Routing\Middleware\SubstituteBindings::class, + ], + ]; + + /** + * The application's middleware aliases. + * + * Aliases may be used instead of class names to conveniently assign middleware to routes and groups. + * + * @var array + */ + protected $middlewareAliases = [ + 'auth' => \App\Http\Middleware\Authenticate::class, + 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, + 'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class, + 'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class, + 'can' => \Illuminate\Auth\Middleware\Authorize::class, + 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, + 'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class, + 'precognitive' => \Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests::class, + 'signed' => \App\Http\Middleware\ValidateSignature::class, + 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, + 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, + ]; +} diff --git a/tests/LanguageSwitcherInstallTest.php b/tests/LanguageSwitcherInstallTest.php new file mode 100644 index 0000000..28c3726 --- /dev/null +++ b/tests/LanguageSwitcherInstallTest.php @@ -0,0 +1,14 @@ +artisan('breezejp --langswitch') + ->expectsOutput('言語切替用のRoute language/{locale} を準備します') + ->expectsOutput('言語切替用の Middleware を準備します') + ->expectsOutput('Kernel に Middleware を登録します') + ->expectsOutput('Language Switherのインストールが完了しました!') + ->assertExitCode(0); + + $this->assertFileExists(base_path('app/Http/Middleware/Localization.php')); + $webfile = file_get_contents(base_path('routes/web.php')); + $this->assertStringContainsString("// Language Switcher Route 言語切替用ルートだよ", $webfile); +}); diff --git a/tests/NoDebugCommandLeftTest.php b/tests/NoDebugCommandLeftTest.php new file mode 100644 index 0000000..d57f591 --- /dev/null +++ b/tests/NoDebugCommandLeftTest.php @@ -0,0 +1,5 @@ +expect(['dd', 'dump','ray']) + ->not->toBeUsed(); diff --git a/tests/TestCase.php b/tests/TestCase.php index d051dbd..e7e4b26 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -12,6 +12,26 @@ protected function setUp(): void { parent::setUp(); + // テスト用のファイルが残ってたら消す(web.php) + if (is_file(__DIR__.'/../vendor/orchestra/testbench-core/laravel/routes/web.php')) { + unlink(__DIR__.'/../vendor/orchestra/testbench-core/laravel/routes/web.php'); + } + + // テスト用のファイル作成(web.php) + if (!is_file(__DIR__.'/../vendor/orchestra/testbench-core/laravel/routes/web.php')) { + copy(__DIR__.'/web.php.stub', __DIR__.'/../vendor/orchestra/testbench-core/laravel/routes/web.php'); + } + + // テスト用のファイルが残ってたら消す(Kernel.php) + if (is_file(__DIR__.'/../vendor/orchestra/testbench-core/laravel/app/Http/Kernel.php')) { + unlink(__DIR__.'/../vendor/orchestra/testbench-core/laravel/app/Http/Kernel.php'); + } + + // テスト用のファイル作成(Kernel.php) + if (!is_file(__DIR__.'/../vendor/orchestra/testbench-core/laravel/app/Http/Kernel.php')) { + copy(__DIR__.'/Kernel.php.stub', __DIR__.'/../vendor/orchestra/testbench-core/laravel/app/Http/Kernel.php'); + } + Factory::guessFactoryNamesUsing( fn (string $modelName) => 'Askdkc\\Breezejp\\Database\\Factories\\'.class_basename($modelName).'Factory' ); @@ -26,6 +46,10 @@ protected function setUp(): void rmdir(__DIR__.'/../vendor/orchestra/testbench-core/laravel/lang/ja'); } + if (is_file(__DIR__.'/../vendor/orchestra/testbench-core/laravel/app/Http/Middleware/Localization.php')) { + unlink(__DIR__.'/../vendor/orchestra/testbench-core/laravel/app/Http/Middleware/Localization.php'); + } + // config/app.phpのlocaleをenに戻す $configfile = file_get_contents(__DIR__.'/../vendor/orchestra/testbench-core/laravel/config/app.php'); $configfile = str_replace("'locale' => 'ja'", "'locale' => 'en'", $configfile); diff --git a/tests/web.php.stub b/tests/web.php.stub new file mode 100644 index 0000000..67fd3d6 --- /dev/null +++ b/tests/web.php.stub @@ -0,0 +1,31 @@ +middleware(['auth', 'verified'])->name('dashboard'); + +Route::middleware('auth')->group(function () { + Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit'); + Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update'); + Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy'); +}); + +require __DIR__.'/auth.php'; From d9a719698efeb9c2be64912ee299c59b5602bd80 Mon Sep 17 00:00:00 2001 From: askdkc Date: Fri, 7 Jul 2023 23:45:37 +0000 Subject: [PATCH 10/11] Fix styling --- tests/LanguageSwitcherInstallTest.php | 2 +- tests/NoDebugCommandLeftTest.php | 2 +- tests/TestCase.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/LanguageSwitcherInstallTest.php b/tests/LanguageSwitcherInstallTest.php index 28c3726..fa4b3fa 100644 --- a/tests/LanguageSwitcherInstallTest.php +++ b/tests/LanguageSwitcherInstallTest.php @@ -10,5 +10,5 @@ $this->assertFileExists(base_path('app/Http/Middleware/Localization.php')); $webfile = file_get_contents(base_path('routes/web.php')); - $this->assertStringContainsString("// Language Switcher Route 言語切替用ルートだよ", $webfile); + $this->assertStringContainsString('// Language Switcher Route 言語切替用ルートだよ', $webfile); }); diff --git a/tests/NoDebugCommandLeftTest.php b/tests/NoDebugCommandLeftTest.php index d57f591..02e14ba 100644 --- a/tests/NoDebugCommandLeftTest.php +++ b/tests/NoDebugCommandLeftTest.php @@ -1,5 +1,5 @@ expect(['dd', 'dump','ray']) + ->expect(['dd', 'dump', 'ray']) ->not->toBeUsed(); diff --git a/tests/TestCase.php b/tests/TestCase.php index e7e4b26..0ff1962 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -18,7 +18,7 @@ protected function setUp(): void } // テスト用のファイル作成(web.php) - if (!is_file(__DIR__.'/../vendor/orchestra/testbench-core/laravel/routes/web.php')) { + if (! is_file(__DIR__.'/../vendor/orchestra/testbench-core/laravel/routes/web.php')) { copy(__DIR__.'/web.php.stub', __DIR__.'/../vendor/orchestra/testbench-core/laravel/routes/web.php'); } @@ -28,7 +28,7 @@ protected function setUp(): void } // テスト用のファイル作成(Kernel.php) - if (!is_file(__DIR__.'/../vendor/orchestra/testbench-core/laravel/app/Http/Kernel.php')) { + if (! is_file(__DIR__.'/../vendor/orchestra/testbench-core/laravel/app/Http/Kernel.php')) { copy(__DIR__.'/Kernel.php.stub', __DIR__.'/../vendor/orchestra/testbench-core/laravel/app/Http/Kernel.php'); } From 568d70a647fde25f583a37e2c109d4bdc70f84f3 Mon Sep 17 00:00:00 2001 From: askdkc Date: Sat, 8 Jul 2023 08:56:03 +0900 Subject: [PATCH 11/11] Delete NoDebugCommandLeftTest.php MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ローカルだと問題ないけど、CI上だと怒られるので削除 ```bash Call to a member function toBeUsed() on array at tests/NoDebugCommandLeftTest.php:5 1▕ expect(['dd', 'dump','ray']) ➜ 5▕ ->not->toBeUsed(); ``` --- tests/NoDebugCommandLeftTest.php | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 tests/NoDebugCommandLeftTest.php diff --git a/tests/NoDebugCommandLeftTest.php b/tests/NoDebugCommandLeftTest.php deleted file mode 100644 index 02e14ba..0000000 --- a/tests/NoDebugCommandLeftTest.php +++ /dev/null @@ -1,5 +0,0 @@ -expect(['dd', 'dump', 'ray']) - ->not->toBeUsed();