From c9209cb2a9f14f6d2700c010113200a6ab8ecfb6 Mon Sep 17 00:00:00 2001 From: Paulo Castellano Date: Fri, 3 Jul 2026 18:57:09 -0300 Subject: [PATCH] Replace the boolean isRtl() with a direction() accessor on ContentLanguage isRtl() existed only to be mapped to an 'rtl'/'ltr' string in SetLocale, so the boolean was the redundant concept. direction() returns the string the one caller needs, which drops the null-safe-plus-nested-ternary from the middleware and lets it resolve the language once with an explicit DEFAULT fallback. --- app/Enums/Workspace/ContentLanguage.php | 10 +++++----- app/Http/Middleware/App/SetLocale.php | 3 ++- tests/Unit/Enums/Workspace/ContentLanguageTest.php | 6 +++--- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/app/Enums/Workspace/ContentLanguage.php b/app/Enums/Workspace/ContentLanguage.php index 9448dc34..ecb7f3e2 100644 --- a/app/Enums/Workspace/ContentLanguage.php +++ b/app/Enums/Workspace/ContentLanguage.php @@ -12,7 +12,7 @@ * * The string value is the language code stored on the workspace and passed * straight to the content prompts (`content_language`); the same codes also back - * the application's UI locales, so `isRtl()` drives the document `dir` attribute. + * the application's UI locales, so `direction()` drives the document `dir` attribute. */ enum ContentLanguage: string { @@ -84,12 +84,12 @@ public function englishName(): string } /** - * Whether the language is written right-to-left, which drives the `dir` - * attribute on the document root so the whole UI mirrors for the locale. + * The text direction (`ltr` or `rtl`) for the document root when this is the + * active UI locale — only Arabic is written right to left. */ - public function isRtl(): bool + public function direction(): string { - return $this === self::Arabic; + return $this === self::Arabic ? 'rtl' : 'ltr'; } /** diff --git a/app/Http/Middleware/App/SetLocale.php b/app/Http/Middleware/App/SetLocale.php index 49a30cde..a28cad1e 100644 --- a/app/Http/Middleware/App/SetLocale.php +++ b/app/Http/Middleware/App/SetLocale.php @@ -22,9 +22,10 @@ public function handle(Request $request, Closure $next): Response $locale = $request->cookie('locale'); $isValid = $locale && array_key_exists($locale, $available); $activeLocale = $isValid ? $locale : config('languages.default'); + $language = ContentLanguage::tryFrom($activeLocale) ?? ContentLanguage::DEFAULT; App::setLocale($activeLocale); - View::share('htmlDir', ContentLanguage::tryFrom($activeLocale)?->isRtl() ? 'rtl' : 'ltr'); + View::share('htmlDir', $language->direction()); $response = $next($request); diff --git a/tests/Unit/Enums/Workspace/ContentLanguageTest.php b/tests/Unit/Enums/Workspace/ContentLanguageTest.php index 88f8aa2e..b816c52e 100644 --- a/tests/Unit/Enums/Workspace/ContentLanguageTest.php +++ b/tests/Unit/Enums/Workspace/ContentLanguageTest.php @@ -71,12 +71,12 @@ [ContentLanguage::Arabic, 'العربية'], ]); -test('isRtl is true only for Arabic', function () { - expect(ContentLanguage::Arabic->isRtl())->toBeTrue(); +test('direction is rtl only for Arabic', function () { + expect(ContentLanguage::Arabic->direction())->toBe('rtl'); foreach (ContentLanguage::cases() as $language) { if ($language !== ContentLanguage::Arabic) { - expect($language->isRtl())->toBeFalse(); + expect($language->direction())->toBe('ltr'); } } });