Skip to content

Add local columns in tables and add middleware to check and set app l… #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
42 changes: 42 additions & 0 deletions app/Http/Middleware/LocaleMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?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.lang.app_local');
$supportLang = config('lcm.lang.support_local');
if (! Auth::check()) {
if (! is_null($request->server('HTTP_ACCEPT_LANGUAGE'))) {
$navigatorLang = substr($request->server('HTTP_ACCEPT_LANGUAGE'), 0, 2);

Check failure on line 27 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['default_lang']) && $user->settings['default_lang'] != $lang) {
$lang = $user->settings['default_lang'];
}
}
app()->setLocale($lang);

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

declare(strict_types=1);

namespace App\Livewire\Components;

use Illuminate\Contracts\View\View;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Symfony\Component\HttpFoundation\RedirectResponse;

final class Locale extends Component
{
public string $selectedLang;

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

public function changeLang(string $lang): RedirectResponse|Redirector

Check failure on line 22 in app/Livewire/Components/Locale.php

View workflow job for this annotation

GitHub Actions / phpstan

Method App\Livewire\Components\Locale::changeLang() never returns Illuminate\Routing\Redirector so it can be removed from the return type.
{
$user = Auth::user();
if ($user) {
$settings = $user->settings;
$settings['default_lang'] = $lang;
$user->settings = $settings;
$user->save();
}
app()->setLocale($lang);

return redirect()->to(url()->current());
}

public function render(): View
{
return view('livewire.components.locale');
}
}
5 changes: 5 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,10 @@
'role' => \Spatie\Permission\Middleware\RoleMiddleware::class,
'checkIfBanned' => \App\Http\Middleware\CheckIfBanned::class,
]);
$middleware->appendToGroup('web', [
LocaleMiddleware::class,
\Illuminate\Session\Middleware\StartSession::class,
]);
})
->withExceptions(function (Exceptions $exceptions): void {
//
Expand Down
5 changes: 5 additions & 0 deletions config/lcm.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
'web_hook' => env('SLACK_WEBHOOK_URL', ''),
],

'lang' => [
'app_local' => env('APP_LOCALE', 'fr'),
'support_local' => ['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,36 @@
<?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
{
private array $tables = ['articles', 'discussions', 'threads'];

public function up(): void
{
Schema::table('articles', function (Blueprint $table): void {
$table->string('locale')->default('fr')->after('slug');
});

Schema::table('discussions', function (Blueprint $table): void {
$table->string('locale')->default('fr')->after('body');
});

Schema::table('threads', function (Blueprint $table): void {
$table->string('locale')->default('fr')->after('body');
});
}

public function down(): void
{
foreach ($this->tables 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.locale></livewire:components.locale>
</div>
</x-container>
</div>
Expand Down
22 changes: 22 additions & 0 deletions resources/views/livewire/components/locale.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div class="flex space-x-2 font-heading cursor-pointer font-bold text-primary-700" >
<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