example for hebrew and english
artisan
php artisan lang:publish
php artisan make:controller LocaleController --invokable
php artisan make:middleware LocaleMiddleware
echo '{
"welcome": "Welcome",
"ok": "Ok"
}' > lang/en.json
echo '{
"welcome": "ברוכים הבאים",
"ok": "בסדר"
}' > lang/he.json
past it on app/Http/Controllers/LocaleController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Session;
class LocaleController extends Controller
{
public function __invoke(Request $request)
{
$locale = $request->input('locale');
if (in_array($locale, ['en', 'he'])) {
Session::put('locale', $locale);
App::setLocale($locale);
}
return redirect()->back();
}
}
past it on routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
use App\Http\Controllers\LocaleController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::post('/locale', LocaleController::class)->name('locale.change');
Route::get('/example', function () {
return view('example');
});
create view example.blade.php and past it
<form action="{{ route('locale.change') }}" method="POST">
@csrf
<select name="locale" onchange="this.form.submit()">
<option value="en"{{ app()->getLocale() == 'en' ? ' selected' : '' }}>English</option>
<option value="he"{{ app()->getLocale() == 'he' ? ' selected' : '' }}>עברית</option>
<!-- Additional language options -->
</select>
</form>
<div>
<h1>
{{ __('welcome') }}
</h1>
</div>
past it on app/Http/Middleware/LocaleMiddleware.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Session;
use Symfony\Component\HttpFoundation\Response;
class LocaleMiddleware
{
public function handle($request, Closure $next)
{
$locale = Session::get('locale') ?? 'en';
Session::put('locale', $locale);
App::setLocale($locale);
return $next($request);
}
}
past it on bootstrap/app.php
<?php
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
return Application::configure(basePath: dirname(__DIR__))
->withRouting(
web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->web(append:[
\App\Http\Middleware\LocaleMiddleware::class,
]);
$middleware->alias([
'check.author' => \App\Http\Middleware\CheckAuthorStatus::class,
]);
})
->withEvents(discover: [
__DIR__.'/../app/Domain/Listeners',
])
->withExceptions(function (Exceptions $exceptions) {
//
})->create();