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 all 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
37 changes: 37 additions & 0 deletions app/Http/Middleware/LocaleMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

final class LocaleMiddleware
{
public function handle(Request $request, Closure $next): Response
{
$browserLocale = explode('_', (string) $request->getPreferredLanguage())[0];
$currentLocale = app()->getLocale();
$activeLocale = session()->get('locale');
$supportedLocales = config('lcm.supported_locales');

if (Auth::check()) {
$userLocale = Auth::user()?->setting('locale', $currentLocale);

if ($userLocale && $userLocale !== $currentLocale) {
app()->setLocale($userLocale);
}
} else {
if (! $activeLocale && in_array($browserLocale, $supportedLocales)) {
app()->setLocale($browserLocale);
} else {
app()->setLocale($activeLocale);
}
}

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

declare(strict_types=1);

namespace App\Livewire\Components;

use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Pluralizer;
use Livewire\Attributes\Computed;
use Livewire\Component;

final class ChangeLocale extends Component
{
public string $currentLocale;

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

public function changeLocale(): void
{
$locale = $this->currentLocale === 'fr' ? 'en' : 'fr';

if (Auth::check()) {
Auth::user()?->settings(['locale' => $locale]);
}

$this->currentLocale = $locale;
app()->setLocale($locale);
session()->put('locale', $locale);

Pluralizer::useLanguage($this->currentLocale === 'fr' ? 'french' : 'english');

$this->redirectRoute('home', navigate: true);
}

#[Computed]
public function locale(): string
{
return $this->currentLocale === 'fr' ? 'English' : 'Français';
}

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

use App\Contracts\ReactableInterface;
use App\Models\Builders\ArticleQueryBuilder;
use App\Models\Scopes\LocaleScope;
use App\Traits\HasAuthor;
use App\Traits\HasSlug;
use App\Traits\HasTags;
use App\Traits\Reactable;
use App\Traits\RecordsActivity;
use CyrildeWit\EloquentViewable\Contracts\Viewable;
use CyrildeWit\EloquentViewable\InteractsWithViews;
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -40,6 +42,7 @@
* @property \Illuminate\Support\Carbon $created_at
* @property \Illuminate\Support\Carbon $updated_at
*/
#[ScopedBy([LocaleScope::class])]
final class Article extends Model implements HasMedia, ReactableInterface, Viewable
{
use HasAuthor;
Expand Down
3 changes: 3 additions & 0 deletions app/Models/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Contracts\SpamReportableContract;
use App\Contracts\SubscribeInterface;
use App\Models\Builders\DiscussionQueryBuilder;
use App\Models\Scopes\LocaleScope;
use App\Traits\HasAuthor;
use App\Traits\HasReplies;
use App\Traits\HasSlug;
Expand All @@ -20,6 +21,7 @@
use Carbon\Carbon;
use CyrildeWit\EloquentViewable\Contracts\Viewable;
use CyrildeWit\EloquentViewable\InteractsWithViews;
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -40,6 +42,7 @@
* @property User $user
* @property Collection | SpamReport[] $spamReports
*/
#[ScopedBy([LocaleScope::class])]
final class Discussion extends Model implements ReactableInterface, ReplyInterface, SpamReportableContract, SubscribeInterface, Viewable
{
use HasAuthor;
Expand Down Expand Up @@ -68,7 +71,7 @@
];

protected $appends = [
'count_all_replies_with_child',

Check failure on line 74 in app/Models/Discussion.php

View workflow job for this annotation

GitHub Actions / phpstan

Property 'count_all_replies_with_child' does not exist in model.
];

protected bool $removeViewsOnDelete = true;
Expand Down
17 changes: 17 additions & 0 deletions app/Models/Scopes/LocaleScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace App\Models\Scopes;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

final class LocaleScope implements Scope
{
public function apply(Builder $builder, Model $model): void
{
$builder->where('locale', app()->getLocale());
}
}
3 changes: 3 additions & 0 deletions app/Models/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Contracts\SubscribeInterface;
use App\Exceptions\CouldNotMarkReplyAsSolution;
use App\Filters\Thread\ThreadFilters;
use App\Models\Scopes\LocaleScope;
use App\Traits\HasAuthor;
use App\Traits\HasReplies;
use App\Traits\HasSlug;
Expand All @@ -21,6 +22,7 @@
use CyrildeWit\EloquentViewable\Contracts\Viewable;
use CyrildeWit\EloquentViewable\InteractsWithViews;
use Exception;
use Illuminate\Database\Eloquent\Attributes\ScopedBy;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -51,6 +53,7 @@
* @property Reply | null $solutionReply
* @property \Illuminate\Database\Eloquent\Collection | Channel[] $channels
*/
#[ScopedBy([LocaleScope::class])]
final class Thread extends Model implements Feedable, ReactableInterface, ReplyInterface, SpamReportableContract, SubscribeInterface, Viewable
{
use HasAuthor;
Expand Down
2 changes: 2 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@
* @property string | null $bio
* @property string | null $website
* @property string | null $banned_reason
* @property array $settings
* @property Carbon | null $email_verified_at
* @property Carbon | null $last_login_at
* @property Carbon | null $banned_at
* @property Collection | Activity[] $activities
* @property-read Collection | SocialAccount[] $providers
*/
final class User extends Authenticatable implements FilamentUser, HasAvatar, HasMedia, HasName, MustVerifyEmail
{
Expand Down
2 changes: 1 addition & 1 deletion app/Traits/HasSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

trait HasSettings
{
public function setting(string $name, string $default): string
public function setting(string $name, string $default): mixed
{
if ($this->settings && array_key_exists($name, $this->settings)) {
return $this->settings[$name];
Expand Down
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
Loading
Loading