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.
This commit is contained in:
parent
c7be1af124
commit
c9209cb2a9
3 changed files with 10 additions and 9 deletions
|
|
@ -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';
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue