Skip to content

Commit

Permalink
fix: lang switcher missing site language links
Browse files Browse the repository at this point in the history
  • Loading branch information
jobara committed Dec 7, 2023
1 parent 986c8a0 commit b39ac76
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 15 deletions.
4 changes: 0 additions & 4 deletions app/Http/Controllers/OrganizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,6 @@ public function show(Organization $organization): View
{
$language = request()->query('language');

if (! in_array($language, $organization->languages)) {
$language = false;
}

return view('organizations.show', array_merge(compact('organization'), [
'language' => $language ?? locale(),
]));
Expand Down
4 changes: 0 additions & 4 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ public function show(Project $project): View

$language = request()->query('language');

if (! in_array($language, $project->languages)) {
$language = false;
}

if ($user->can('manage', $project)) {
$engagements = $project->allEngagements;
} elseif ($user->isAdministrator()) {
Expand Down
5 changes: 4 additions & 1 deletion app/View/Components/LanguageChanger.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ class LanguageChanger extends Component
{
public mixed $model;

public string $currentLanguage;

/**
* Create a new component instance.
*
* @return void
*/
public function __construct(mixed $model)
public function __construct(mixed $model, ?string $currentLanguage)
{
$this->model = $model;
$this->currentLanguage = $currentLanguage ?? locale();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion resources/views/components/language-changer.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<label class="whitespace-nowrap" for='available_languages'>{{ __('Page also available in:') }}</label>
<ul class="flex flex-wrap gap-3" id='available_languages' role="list">
@foreach ($model->languages as $code)
@if ($code !== locale())
@if ($code !== $currentLanguage)
@if (in_array($code, config('locales.supported')))
{{-- Make sure at least the model name is translated to avoid 404 errors. --}}
@if ($model->isTranslatableAttribute($testValue) || !empty($model->getTranslation($testValue, $code, false)))
Expand Down
2 changes: 1 addition & 1 deletion resources/views/engagements/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
@endcan
</x-slot>

<x-language-changer :model="$project" />
<x-language-changer :model="$project" :currentLanguage="$language" />

<div class="stack mb-12 w-full md:w-2/3">
<h2>{{ __('Description') }}</h2>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/individuals/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
</div>
</x-slot>

<x-language-changer :model="$individual" />
<x-language-changer :model="$individual" :currentLanguage="$language" />
<div class="with-sidebar">
<nav class="secondary" aria-labelledby="individual">
<ul role="list">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/organizations/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
</div>
</x-slot>

<x-language-changer :model="$organization" />
<x-language-changer :model="$organization" :currentLanguage="$language" />

<div class="with-sidebar">
<nav class="secondary" aria-labelledby="organization">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/projects/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
@can('update', $project)
<x-translation-manager :model="$project" />
@else
<x-language-changer :model="$project" />
<x-language-changer :model="$project" :currentLanguage="$language" />
@endcan

<div class="with-sidebar">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/regulated-organizations/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
</div>
</x-slot>

<x-language-changer :model="$regulatedOrganization" />
<x-language-changer :model="$regulatedOrganization" :currentLanguage="$language" />

<div class="with-sidebar">
<nav class="secondary" aria-labelledby="regulated-organization">
Expand Down
41 changes: 41 additions & 0 deletions tests/Feature/LanguageChangerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use App\Models\Individual;
use App\View\Components\LanguageChanger;

beforeEach(function () {
$this->model = Individual::factory()->create([
'bio' => [
'en' => 'bio (en)',
'fr' => 'bio (fr)',
'es' => 'bio (es)',
],
'languages' => ['en', 'fr', 'es'],
]);
});

test('example', function (string $appLocale, ?string $pageLocale) {
App::setLocale($appLocale);

$contentLocale = $pageLocale ?? $appLocale;
$otherLocales = array_filter($this->model->languages, fn ($code) => $code !== $contentLocale);

$view = $this->component(LanguageChanger::class, [
'model' => $this->model,
'currentLanguage' => $pageLocale,
]);

$view->assertDontSee(get_language_exonym($contentLocale));

foreach ($otherLocales as $locale) {
$view->assertSee(get_language_exonym($locale));
}
})->with([
'English app locale' => 'en',
'French app locale' => 'fr',
])->with([
'English page locale' => 'en',
'French page locale' => 'fr',
'Spanish page locale' => 'es',
'No page locale' => null,
]);

0 comments on commit b39ac76

Please sign in to comment.