Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Detrol's avatar

Alternative Locale/Languages for Laravel

So I have a portfolio which is a one-page site, where I found it very tedious to use the regular locale function for translations, because I won't be needing to update the content very often, and there is a lot of text.

Therefore I wanted to do it in a more old-fashioned way, which I wanted to share in case someone else prefers it.

First off, this will make it necessary to create language folders inside the views. In this example, I will use EN(US) and SV(SE).

Here is the folder structure:

Views -> layouts -> app.blade.php

Views -> layouts -> en -> footer.blade.php
Views -> layouts -> en -> navigation.blade.php
Views -> layouts -> sv -> footer.blade.php
Views -> layouts -> sv -> navigation.blade.php

Views -> pages -> en -> home.blade.php
Views -> pages -> sv -> home.blade.php

Here is my layout file:

Views -> layouts -> app.blade.php

<body class="font-sans antialiased" style="font-display: swap;">
    <div>
        <header id="top" class="w-full flex flex-col fixed sm:relative shadow-lg" style="z-index: 1000">
            @include('layouts.'. session('locale') .'.navigation')
        </header>

        <main class="content">
            @yield(session('locale') . '.content')
        </main>
    </div>
</body>

<footer class="text-gray-600 body-font bg-gray-100">
    @include('layouts.'. session('locale') .'.footer')
</footer>

Language switch in navigation files

<div class="flex">
	<div class="w-1/2 mr-3">
 		<a href="/lang/en">
			<img src="{{ asset('img/flags/24x24/US.png') }}" alt="US" />
		</a>
	</div>

	<div class="w-1/2">
		<a href="/lang/sv">
			<img src="{{ asset('img/flags/24x24/SE.png') }}" alt="SV" />
		</a>
	</div>
</div>

For the pages folder - home.blade.php

Here I change how we load the section content

@section(session('locale') . '.content')
your content here
@endsection

Then I created a Middleware called Locale.php which loads the default locale if no session has been set.

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class Locale
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if (empty(session('locale'))) {
            session()->put('locale', config('app.locale'));
        }

        return $next($request);
    }
}

Which I load through web.php, and don't forget to add it to the kernel.

Route::get('/', function () {
    return view('pages.'. session('locale') .'.home');
})->middleware('locale');

After that, I created a controller called LocalizationController.php for the switch.

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;

class LocalizationController extends Controller
{
    public function lang($locale)
    {
        session()->put('locale', $locale);
        return redirect()->back();
    }
}

And that's it! I hope someone else finds this useful.

0 likes
0 replies

Please or to participate in this conversation.