Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://laravel.cm.test
APP_LOCALE=fr
FILAMENT_PATH=cp

LOG_CHANNEL=stack
Expand Down
48 changes: 48 additions & 0 deletions app/Http/Middleware/LocaleMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\HttpFoundation\Response;

final class LocaleMiddleware
{
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(Request $request, Closure $next): Response
{
$user = Auth::user();
$lang = config('lcm.app_locale');
$supportLang = config('lcm.supported_locales');

if (! Auth::check()) {

if (! is_null($request->server('HTTP_ACCEPT_LANGUAGE'))) {
$navigatorLang = substr($request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2);

Check failure on line 29 in app/Http/Middleware/LocaleMiddleware.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $string of function substr expects string, array|string given.

if (in_array($navigatorLang, $supportLang)) {
$lang = $navigatorLang;
}
}
}

if (! is_null($user)) {

if (isset($user->settings['locale']) && $user->settings['locale'] != $lang) {
$lang = $user->settings['locale'];
}
}

app()->setLocale($lang);

return $next($request);
}
}
39 changes: 39 additions & 0 deletions app/Livewire/Components/ChangeLocale.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace App\Livewire\Components;

use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;

final class ChangeLocale extends Component
{
public string $selectedLang;

public function mount(): void
{
$this->selectedLang = app()->getLocale();
}

public function changeLang(string $lang): void
{
$user = Auth::user();

if ($user) {
$settings = $user->settings;
$settings['locale'] = $lang;
$user->settings = $settings;
$user->save();
}

app()->setLocale($lang);
$this->dispatch('localeChanged');
}

public function render(): View
{
return view('livewire.components.change-locale');
}
}
4 changes: 4 additions & 0 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use App\Http\Middleware\LocaleMiddleware;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
Expand All @@ -17,6 +18,9 @@
'role' => \Spatie\Permission\Middleware\RoleMiddleware::class,
'checkIfBanned' => \App\Http\Middleware\CheckIfBanned::class,
]);
$middleware->web(append: [
LocaleMiddleware::class,
]);
})
->withExceptions(function (Exceptions $exceptions): void {
//
Expand Down
4 changes: 4 additions & 0 deletions config/lcm.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
'web_hook' => env('SLACK_WEBHOOK_URL', ''),
],

'app_locale' => env('APP_LOCALE', 'fr'),

'supported_locales' => ['fr', 'en'],

'spa_url' => env('FRONTEND_APP_URL', 'http://localhost:4200'),

'notch-pay-public-token' => env('NOTCHPAY_PUBLIC_KEY', null),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
foreach (['articles', 'discussions', 'threads'] as $key => $value) {
$afterColumn = ($key == 0) ? 'slug' : 'body';
Schema::table($value, function (Blueprint $table) use ($afterColumn): void {
$table->string('locale')->default('fr')->after($afterColumn);
});
}
}

public function down(): void
{
foreach (['articles', 'discussions', 'threads'] as $tab) {
Schema::table($tab, function (Blueprint $table): void {
$table->dropColumn('locale');
});
}
}
};
1 change: 1 addition & 0 deletions resources/views/components/layouts/footer.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class="ml-2 size-6 rounded-full"
<x-icon.youtube class="size-6" aria-hidden="true" />
</x-link>
</div>
<livewire:components.change-locale></livewire:components.change-locale>
</div>
</x-container>
</div>
Expand Down
26 changes: 26 additions & 0 deletions resources/views/livewire/components/change-locale.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<div class="flex space-x-2 font-heading cursor-pointer font-bold text-primary-700" x-data="{ init() {
window.addEventListener('localeChanged', () => {
window.location.reload();
});
} }" x-init="init()" >
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="flag-icons-cm" viewBox="0 0 640 480" class="w-6 h-6">
<path fill="#007a5e" d="M0 0h213.3v480H0z"/>
<path fill="#ce1126" d="M213.3 0h213.4v480H213.3z"/>
<path fill="#fcd116" d="M426.7 0H640v480H426.7z"/>
<g fill="#fcd116" transform="translate(320 240)scale(7.1111)">
<g id="cm-b">
<path id="cm-a" d="M0-8-2.5-.4 1.3.9z"/>
<use xlink:href="#cm-a" width="100%" height="100%" transform="scale(-1 1)"/>
</g>
<use xlink:href="#cm-b" width="100%" height="100%" transform="rotate(72)"/>
<use xlink:href="#cm-b" width="100%" height="100%" transform="rotate(144)"/>
<use xlink:href="#cm-b" width="100%" height="100%" transform="rotate(-144)"/>
<use xlink:href="#cm-b" width="100%" height="100%" transform="rotate(-72)"/>
</g>
</svg>
@if($selectedLang == 'fr')
<button wire:click="changeLang('en')">EN</button>
@else
<button wire:click="changeLang('fr')">FR</button>
@endif
</div>
Loading