Skip to content

Commit

Permalink
fix: model lsq translations don't fallback to fr
Browse files Browse the repository at this point in the history
  • Loading branch information
jobara committed Nov 29, 2023
1 parent e4ffdc3 commit d974e1c
Show file tree
Hide file tree
Showing 30 changed files with 159 additions and 114 deletions.
10 changes: 1 addition & 9 deletions app/Http/Controllers/IndividualController.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,7 @@ public function saveRoles(SaveIndividualRolesRequest $request): RedirectResponse

public function show(Individual $individual): View
{
$language = request()->query('language');

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

return view('individuals.show', array_merge(compact('individual'), [
'language' => $language ?? locale(),
]));
return view('individuals.show', compact('individual'));
}

public function edit(Individual $individual): View
Expand Down
10 changes: 1 addition & 9 deletions app/Http/Controllers/OrganizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,7 @@ public function storeLanguages(StoreOrganizationLanguagesRequest $request, Organ

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(),
]));
return view('organizations.show', compact('organization'));
}

public function edit(Organization $organization): View
Expand Down
7 changes: 0 additions & 7 deletions app/Http/Controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,6 @@ public function show(Project $project): View
{
$user = Auth::user();

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

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

if ($user->can('manage', $project)) {
$engagements = $project->allEngagements;
} elseif ($user->isAdministrator()) {
Expand All @@ -113,7 +107,6 @@ public function show(Project $project): View
}

return view('projects.show', [
'language' => $language ?? locale(),
'project' => $project,
'engagements' => $engagements,
]);
Expand Down
8 changes: 1 addition & 7 deletions app/Http/Controllers/RegulatedOrganizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,7 @@ public function show(RegulatedOrganization $regulatedOrganization): View
$regulatedOrganization->load('completedProjects', 'inProgressProjects', 'upcomingProjects');
}

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

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

return view('regulated-organizations.show', array_merge(compact('regulatedOrganization'), ['language' => $language ?? locale()]));
return view('regulated-organizations.show', compact('regulatedOrganization'));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/UpdateIndividualRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function rules(): array
new Enum(ProvinceOrTerritory::class),
],
'pronouns' => 'nullable|array:'.implode(',', to_written_languages($this->individual->languages)),
'bio' => 'required|array:'.implode(',', to_written_languages($this->individual->languages)).'|required_array_keys:'.get_written_language_for_signed_language($this->individual->user->locale),
'bio' => 'required|array:'.implode(',', to_written_languages($this->individual->languages)).'|required_array_keys:'.to_written_language($this->individual->user->locale),
'bio.en' => 'required_without:bio.fr',
'bio.fr' => 'required_without:bio.en',
'bio.*' => 'nullable|string',
Expand Down
2 changes: 0 additions & 2 deletions app/Models/Individual.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use App\Models\Scopes\IndividualUserNotSuspendedScope;
use App\Models\Scopes\ReachableIdentityScope;
use App\Traits\HasDisplayRegion;
use App\Traits\HasMultimodalTranslations;
use App\Traits\HasMultipageEditingAndPublishing;
use App\Traits\HasSchemalessAttributes;
use Carbon\Carbon;
Expand Down Expand Up @@ -50,7 +49,6 @@ class Individual extends Model implements CipherSweetEncrypted
use HasDisplayRegion;
use HasFactory;
use HasMergedRelationships;
use HasMultimodalTranslations;
use HasMultipageEditingAndPublishing;
use HasRelationships;
use HasSchemalessAttributes;
Expand Down
2 changes: 0 additions & 2 deletions app/Models/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use App\Models\Scopes\ReachableIdentityScope;
use App\Traits\GeneratesMultilingualSlugs;
use App\Traits\HasDisplayRegion;
use App\Traits\HasMultimodalTranslations;
use App\Traits\HasMultipageEditingAndPublishing;
use App\Traits\HasSchemalessAttributes;
use Carbon\Carbon;
Expand Down Expand Up @@ -53,7 +52,6 @@ class Organization extends Model
use HasFactory;
use HasMembers;
use HasMergedRelationships;
use HasMultimodalTranslations;
use HasMultipageEditingAndPublishing;
use HasRelationships;
use HasSchemalessAttributes;
Expand Down
2 changes: 0 additions & 2 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Models\Scopes\ProjectableNotSuspendedScope;
use App\Statuses\EngagementStatus;
use App\Traits\HasMultimodalTranslations;
use App\Traits\HasMultipageEditingAndPublishing;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
Expand All @@ -29,7 +28,6 @@
class Project extends Model
{
use HasFactory;
use HasMultimodalTranslations;
use HasMultipageEditingAndPublishing;
use HasRelationships;
use HasStatus;
Expand Down
2 changes: 0 additions & 2 deletions app/Models/RegulatedOrganization.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use App\Models\Scopes\OrganizationNotSuspendedScope;
use App\Traits\GeneratesMultilingualSlugs;
use App\Traits\HasDisplayRegion;
use App\Traits\HasMultimodalTranslations;
use App\Traits\HasMultipageEditingAndPublishing;
use Carbon\Carbon;
use Hearth\Traits\HasInvitations;
Expand Down Expand Up @@ -43,7 +42,6 @@ class RegulatedOrganization extends Model
use HasFactory;
use HasInvitations;
use HasMembers;
use HasMultimodalTranslations;
use HasMultipageEditingAndPublishing;
use HasStatus;
use HasTranslatableSlug;
Expand Down
18 changes: 17 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Composer\InstalledVersions;
use Filament\Facades\Filament;
use Filament\Navigation\NavigationItem;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\ServiceProvider;
use Makeable\EloquentStatus\StatusManager;
Expand Down Expand Up @@ -74,7 +75,22 @@ public function boot(UrlGenerator $url)
StatusManager::bind(RegulatedOrganization::class, RegulatedOrganizationStatus::class);
StatusManager::bind(Project::class, ProjectStatus::class);
StatusManager::bind(User::class, UserStatus::class);
Translatable::fallback(fallbackLocale: 'en', fallbackAny: true);
Translatable::fallback(fallbackLocale: 'en', fallbackAny: true, missingKeyCallback: function (
Model $model,
string $translationKey,
string $locale,
string $fallbackTranslation,
string $fallbackLocale
) {
// Handles fallback of sign language to equivalent written language
$writtenLocale = to_written_language($locale);
// Ignoring the next line for static analysis because it doesn't know that $model will only be types
// that have the HasTranslatable trait.
// @phpstan-ignore-next-line
$writtenTranslation = $model->getTranslationWithoutFallback($translationKey, $writtenLocale);

return ! empty($writtenTranslation) ? $writtenTranslation : $fallbackTranslation;
});
Engagement::observe(EngagementObserver::class);
User::observe(UserObserver::class);
}
Expand Down
39 changes: 0 additions & 39 deletions app/Traits/HasMultimodalTranslations.php

This file was deleted.

8 changes: 4 additions & 4 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function get_supported_locales(bool $signed = true): array
};
}

if (! function_exists('get_written_language_for_signed_language')) {
if (! function_exists('to_written_language')) {
/**
* Get the written language which most closely corresponds to a signed language.
* If a code other than ASL or LSQ is passed, it will be returned without modification.
Expand All @@ -151,7 +151,7 @@ function get_supported_locales(bool $signed = true): array
* @param string $code Either 'asl' or 'lsq'
* @return string An ISO 639 code
*/
function get_written_language_for_signed_language(string $code): string
function to_written_language(string $code): string
{
return match ($code) {
'asl' => 'en',
Expand Down Expand Up @@ -191,7 +191,7 @@ function get_signed_language_for_written_language(string $code): string
function to_written_languages(array $codes): array
{
foreach ($codes as $key => $code) {
$codes[$key] = get_written_language_for_signed_language($code);
$codes[$key] = to_written_language($code);
}

return array_unique($codes);
Expand Down Expand Up @@ -355,7 +355,7 @@ function orientation_link(string $userType): string
*/
function settings_localized(string $key = null, string $locale = null, mixed $default = null): mixed
{
$locale = get_written_language_for_signed_language($locale ?? config('app.locale'));
$locale = to_written_language($locale ?? config('app.locale'));
$settings = settings($key, []);

return $settings[$locale] ?? $settings[config('app.fallback_locale')];
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"amirami/localizator": "^0.12.1-alpha@alpha",
"barryvdh/laravel-debugbar": "^3.6",
"barryvdh/laravel-ide-helper": "^2.12",
"calebporzio/sushi": "^2.4",
"fakerphp/faker": "^1.19",
"laravel/dusk": "^7.0",
"laravel/pint": "^1.13",
Expand Down
54 changes: 53 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 @@ -19,7 +19,7 @@
@endif
@else
<li><a
href="{{ localized_route($model->getRoutePrefix() . '.show', [Str::camel(class_basename($model)) => $model, 'language' => $code], get_written_language_for_signed_language($code)) }}">{{ get_language_exonym($code) }}</a>
href="{{ localized_route($model->getRoutePrefix() . '.show', [Str::camel(class_basename($model)) => $model, 'language' => $code], to_written_language($code)) }}">{{ get_language_exonym($code) }}</a>
</li>
@endif
@endif
Expand Down
2 changes: 1 addition & 1 deletion resources/views/components/language-modal.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<li class="flex flex-col">
<a class="cta flex flex-col"
href="{{ trans_current_route($locale, route($locale . '.welcome')) }}"
hreflang="{{ get_written_language_for_signed_language($locale) }}"
hreflang="{{ to_written_language($locale) }}"
aria-current="{{ request()->routeIs($locale . '.*') ? 'true' : 'false' }}"
rel="alternate">
@if (is_signed_language($locale))
Expand Down
6 changes: 3 additions & 3 deletions resources/views/components/language-switcher.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
<x-slot name="content">
@foreach ($locales as $key => $locale)
<li>
<x-nav-link hreflang="{{ get_written_language_for_signed_language($key) }}" rel="alternate"
:href="trans_current_route($key, route($key . '.welcome'))" :active="request()->routeIs($key . '.*')">
<x-nav-link hreflang="{{ to_written_language($key) }}" rel="alternate" :href="trans_current_route($key, route($key . '.welcome'))"
:active="request()->routeIs($key . '.*')">
@if (is_signed_language($key))
{{ trans(':signLanguage (with :locale)', ['signLanguage' => get_language_exonym($key, $key), 'locale' => get_language_exonym(get_written_language_for_signed_language($key), $key)], $key) }}
{{ trans(':signLanguage (with :locale)', ['signLanguage' => get_language_exonym($key, $key), 'locale' => get_language_exonym(to_written_language($key), $key)], $key) }}
@else
{{ $locale }}
@endif
Expand Down
2 changes: 1 addition & 1 deletion resources/views/components/translation-picker.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class="flex h-14 w-full items-center justify-between border border-x-0 border-b-0 border-solid first-of-type:border-t-0">
<p class="repel w-full"><span x-text="language.exonym"></span><button class="secondary" type="button"
x-bind:data-index="index" @click="removeLanguage($event)"
x-show="languages.length > 1 && language.code !== '{{ get_written_language_for_signed_language(locale()) }}' && canRemove(language.code)">{{ __('Remove') }}<span
x-show="languages.length > 1 && language.code !== '{{ to_written_language(locale()) }}' && canRemove(language.code)">{{ __('Remove') }}<span
class="visually-hidden" x-text="language.exonym"></span></button></p>

<input name="languages[]" type="hidden" x-bind:value="language.code" />
Expand Down
2 changes: 1 addition & 1 deletion resources/views/individuals/partials/about.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{ safe_nl2br($individual->getWrittenTranslation('bio', $language)) }}
{{ safe_nl2br($individual->bio) }}

<h3>
{{ __('Languages :name uses', ['name' => $individual->first_name]) }}</h3>
Expand Down
4 changes: 2 additions & 2 deletions resources/views/individuals/partials/experiences.blade.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
@if ($individual->lived_experience)
<h3>{{ __('Lived experience') }}</h3>
{{ safe_nl2br($individual->getWrittenTranslation('lived_experience', $language)) }}
{{ safe_nl2br($individual->lived_experience) }}
@endif

@if ($individual->skills_and_strengths)
<h3>{{ __('Skills and strengths') }}</h3>
{{ safe_nl2br($individual->getWrittenTranslation('skills_and_strengths', $language)) }}
{{ safe_nl2br($individual->skills_and_strengths) }}
@endif

@if ($individual->relevant_experiences && count($individual->relevant_experiences) > 0)
Expand Down
Loading

0 comments on commit d974e1c

Please sign in to comment.