From d974e1c2130ea3db29f9f95568f218cd3fcfe7ad Mon Sep 17 00:00:00 2001 From: Justin Obara Date: Wed, 29 Nov 2023 14:23:10 -0500 Subject: [PATCH] fix: model lsq translations don't fallback to fr --- app/Http/Controllers/IndividualController.php | 10 +--- .../Controllers/OrganizationController.php | 10 +--- app/Http/Controllers/ProjectController.php | 7 --- .../RegulatedOrganizationController.php | 8 +-- app/Http/Requests/UpdateIndividualRequest.php | 2 +- app/Models/Individual.php | 2 - app/Models/Organization.php | 2 - app/Models/Project.php | 2 - app/Models/RegulatedOrganization.php | 2 - app/Providers/AppServiceProvider.php | 18 ++++++- app/Traits/HasMultimodalTranslations.php | 39 -------------- app/helpers.php | 8 +-- composer.json | 1 + composer.lock | 54 ++++++++++++++++++- .../components/language-changer.blade.php | 2 +- .../views/components/language-modal.blade.php | 2 +- .../components/language-switcher.blade.php | 6 +-- .../components/translation-picker.blade.php | 2 +- .../individuals/partials/about.blade.php | 2 +- .../partials/experiences.blade.php | 4 +- resources/views/individuals/show.blade.php | 6 +-- .../organizations/partials/about.blade.php | 2 +- resources/views/organizations/show.blade.php | 6 +-- .../projects/partials/overview.blade.php | 8 +-- .../views/projects/partials/team.blade.php | 2 +- .../partials/about.blade.php | 2 +- .../regulated-organizations/show.blade.php | 6 +-- .../hearth/components/locale-select.blade.php | 2 +- tests/Feature/HasTranslationsFallbackTest.php | 52 ++++++++++++++++++ tests/Unit/LanguageHelpersTest.php | 4 +- 30 files changed, 159 insertions(+), 114 deletions(-) delete mode 100644 app/Traits/HasMultimodalTranslations.php create mode 100644 tests/Feature/HasTranslationsFallbackTest.php diff --git a/app/Http/Controllers/IndividualController.php b/app/Http/Controllers/IndividualController.php index 0734680ee..24a4ab428 100644 --- a/app/Http/Controllers/IndividualController.php +++ b/app/Http/Controllers/IndividualController.php @@ -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 diff --git a/app/Http/Controllers/OrganizationController.php b/app/Http/Controllers/OrganizationController.php index 467247b5c..3b2908de1 100644 --- a/app/Http/Controllers/OrganizationController.php +++ b/app/Http/Controllers/OrganizationController.php @@ -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 diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 5b9754876..8b1bac609 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -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()) { @@ -113,7 +107,6 @@ public function show(Project $project): View } return view('projects.show', [ - 'language' => $language ?? locale(), 'project' => $project, 'engagements' => $engagements, ]); diff --git a/app/Http/Controllers/RegulatedOrganizationController.php b/app/Http/Controllers/RegulatedOrganizationController.php index 6d0110697..78fbad8ec 100644 --- a/app/Http/Controllers/RegulatedOrganizationController.php +++ b/app/Http/Controllers/RegulatedOrganizationController.php @@ -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')); } /** diff --git a/app/Http/Requests/UpdateIndividualRequest.php b/app/Http/Requests/UpdateIndividualRequest.php index 4d52db458..2fd7ff66a 100644 --- a/app/Http/Requests/UpdateIndividualRequest.php +++ b/app/Http/Requests/UpdateIndividualRequest.php @@ -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', diff --git a/app/Models/Individual.php b/app/Models/Individual.php index d8426e2eb..adb253cee 100644 --- a/app/Models/Individual.php +++ b/app/Models/Individual.php @@ -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; @@ -50,7 +49,6 @@ class Individual extends Model implements CipherSweetEncrypted use HasDisplayRegion; use HasFactory; use HasMergedRelationships; - use HasMultimodalTranslations; use HasMultipageEditingAndPublishing; use HasRelationships; use HasSchemalessAttributes; diff --git a/app/Models/Organization.php b/app/Models/Organization.php index 775b42886..4de466879 100644 --- a/app/Models/Organization.php +++ b/app/Models/Organization.php @@ -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; @@ -53,7 +52,6 @@ class Organization extends Model use HasFactory; use HasMembers; use HasMergedRelationships; - use HasMultimodalTranslations; use HasMultipageEditingAndPublishing; use HasRelationships; use HasSchemalessAttributes; diff --git a/app/Models/Project.php b/app/Models/Project.php index 1e843e9d6..791122b1b 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -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; @@ -29,7 +28,6 @@ class Project extends Model { use HasFactory; - use HasMultimodalTranslations; use HasMultipageEditingAndPublishing; use HasRelationships; use HasStatus; diff --git a/app/Models/RegulatedOrganization.php b/app/Models/RegulatedOrganization.php index d7856d4f0..5a5ab667c 100644 --- a/app/Models/RegulatedOrganization.php +++ b/app/Models/RegulatedOrganization.php @@ -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; @@ -43,7 +42,6 @@ class RegulatedOrganization extends Model use HasFactory; use HasInvitations; use HasMembers; - use HasMultimodalTranslations; use HasMultipageEditingAndPublishing; use HasStatus; use HasTranslatableSlug; diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 0ac656bbe..ccd8cb7dd 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -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; @@ -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); } diff --git a/app/Traits/HasMultimodalTranslations.php b/app/Traits/HasMultimodalTranslations.php deleted file mode 100644 index 5b9e080f5..000000000 --- a/app/Traits/HasMultimodalTranslations.php +++ /dev/null @@ -1,39 +0,0 @@ -$attribute; - } - - /** If the language code is for a signed language, get the attribute in the written language which most closely corresponds to the signed language. */ - if (is_signed_language($code)) { - return $this->getTranslation($attribute, get_written_language_for_signed_language($code)); - } - - /** Get the attribute in the language. */ - return $this->getTranslation($attribute, $code); - } - - /** - * @param string $attribute - * @param string $code - * @return void - */ - // TODO: Implement this - // public function getSignedTranslation(string $attribute, string $code = '') - // { - // /** If no language code was passed, or if the language code is not for a signed language, return null. */ - // if (! $code || ! is_signed_language($code)) { - // return null; - // } - // - // /** Get the attribute in the signed language. */ - // return $this->getTranslation($attribute, $code); - // } */ -} diff --git a/app/helpers.php b/app/helpers.php index c724b3a98..5c17e2eba 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -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. @@ -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', @@ -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); @@ -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')]; diff --git a/composer.json b/composer.json index f54f70eb7..b59c88611 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/composer.lock b/composer.lock index 708b779a7..82467c1c0 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "47e710fc0745a53469c312d170503286", + "content-hash": "8b4b28d8654cbdd2631d5bbba37997f4", "packages": [ { "name": "aws/aws-crt-php", @@ -12129,6 +12129,58 @@ ], "time": "2023-10-10T15:11:25+00:00" }, + { + "name": "calebporzio/sushi", + "version": "v2.4.5", + "source": { + "type": "git", + "url": "https://github.com/calebporzio/sushi.git", + "reference": "932d09781bff75c812541d2d269400fd7d730bab" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/calebporzio/sushi/zipball/932d09781bff75c812541d2d269400fd7d730bab", + "reference": "932d09781bff75c812541d2d269400fd7d730bab", + "shasum": "" + }, + "require": { + "illuminate/database": "^5.8 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0", + "illuminate/support": "^5.8 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0", + "php": "^7.1.3|^8.0" + }, + "require-dev": { + "doctrine/dbal": "^2.9 || ^3.1.4", + "orchestra/testbench": "3.8.* || 3.9.* || ^4.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0", + "phpunit/phpunit": "^7.5 || ^8.4 || ^9.0 || ^10.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Sushi\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Caleb Porzio", + "email": "calebporzio@gmail.com" + } + ], + "description": "Eloquent's missing \"array\" driver.", + "support": { + "source": "https://github.com/calebporzio/sushi/tree/v2.4.5" + }, + "funding": [ + { + "url": "https://github.com/calebporzio", + "type": "github" + } + ], + "time": "2023-10-17T14:42:34+00:00" + }, { "name": "composer/class-map-generator", "version": "1.1.0", diff --git a/resources/views/components/language-changer.blade.php b/resources/views/components/language-changer.blade.php index 2bda66bc4..1bcdbedf1 100644 --- a/resources/views/components/language-changer.blade.php +++ b/resources/views/components/language-changer.blade.php @@ -19,7 +19,7 @@ @endif @else
  • {{ get_language_exonym($code) }} + href="{{ localized_route($model->getRoutePrefix() . '.show', [Str::camel(class_basename($model)) => $model, 'language' => $code], to_written_language($code)) }}">{{ get_language_exonym($code) }}
  • @endif @endif diff --git a/resources/views/components/language-modal.blade.php b/resources/views/components/language-modal.blade.php index 6116b76de..f777bcf99 100644 --- a/resources/views/components/language-modal.blade.php +++ b/resources/views/components/language-modal.blade.php @@ -16,7 +16,7 @@
  • @if (is_signed_language($locale)) diff --git a/resources/views/components/language-switcher.blade.php b/resources/views/components/language-switcher.blade.php index 2feb2f364..515865d16 100644 --- a/resources/views/components/language-switcher.blade.php +++ b/resources/views/components/language-switcher.blade.php @@ -6,10 +6,10 @@ @foreach ($locales as $key => $locale)
  • - + @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 diff --git a/resources/views/components/translation-picker.blade.php b/resources/views/components/translation-picker.blade.php index 8499fc1d7..81d7fcf8f 100644 --- a/resources/views/components/translation-picker.blade.php +++ b/resources/views/components/translation-picker.blade.php @@ -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">

    diff --git a/resources/views/individuals/partials/about.blade.php b/resources/views/individuals/partials/about.blade.php index 637f3474f..134782fc3 100644 --- a/resources/views/individuals/partials/about.blade.php +++ b/resources/views/individuals/partials/about.blade.php @@ -1,4 +1,4 @@ -{{ safe_nl2br($individual->getWrittenTranslation('bio', $language)) }} +{{ safe_nl2br($individual->bio) }}

    {{ __('Languages :name uses', ['name' => $individual->first_name]) }}

    diff --git a/resources/views/individuals/partials/experiences.blade.php b/resources/views/individuals/partials/experiences.blade.php index f7b3b94c9..380626082 100644 --- a/resources/views/individuals/partials/experiences.blade.php +++ b/resources/views/individuals/partials/experiences.blade.php @@ -1,11 +1,11 @@ @if ($individual->lived_experience)

    {{ __('Lived experience') }}

    - {{ safe_nl2br($individual->getWrittenTranslation('lived_experience', $language)) }} + {{ safe_nl2br($individual->lived_experience) }} @endif @if ($individual->skills_and_strengths)

    {{ __('Skills and strengths') }}

    - {{ safe_nl2br($individual->getWrittenTranslation('skills_and_strengths', $language)) }} + {{ safe_nl2br($individual->skills_and_strengths) }} @endif @if ($individual->relevant_experiences && count($individual->relevant_experiences) > 0) diff --git a/resources/views/individuals/show.blade.php b/resources/views/individuals/show.blade.php index a16812e48..78e5cd34e 100644 --- a/resources/views/individuals/show.blade.php +++ b/resources/views/individuals/show.blade.php @@ -2,8 +2,8 @@ {{ $individual->name }} @if (auth()->hasUser() && - auth()->user()->isAdministrator() && - $individual->user->checkStatus('suspended')) + auth()->user()->isAdministrator() && + $individual->user->checkStatus('suspended')) @push('banners') {{ __('This account has been suspended.') }} @endpush @@ -73,7 +73,7 @@
  • @svg('heroicon-o-globe-alt') - {{ __('Website', [], !is_signed_language($language) ? $language : locale()) }} + {{ __('Website') }}
  • @endif diff --git a/resources/views/organizations/partials/about.blade.php b/resources/views/organizations/partials/about.blade.php index 0c77985e3..85ee5cc4b 100644 --- a/resources/views/organizations/partials/about.blade.php +++ b/resources/views/organizations/partials/about.blade.php @@ -1,7 +1,7 @@

    {{ __('About the organization') }}

    -{{ safe_nl2br($organization->getWrittenTranslation('about', $language)) }} +{{ safe_nl2br($organization->about) }}

    {{ __('Type of organization') }}

    diff --git a/resources/views/organizations/show.blade.php b/resources/views/organizations/show.blade.php index ad36e4b45..c7edcbb8a 100644 --- a/resources/views/organizations/show.blade.php +++ b/resources/views/organizations/show.blade.php @@ -1,5 +1,5 @@ - {{ $organization->getWrittenTranslation('name', $language) }} + {{ $organization->name }} @if (auth()->hasUser() && auth()->user()->isAdministrator() && @@ -16,7 +16,7 @@

    - {{ $organization->getWrittenTranslation('name', $language) }} + {{ $organization->name }}

    @if ($organization->checkStatus('draft')) {{ __('Draft mode') }} @@ -57,7 +57,7 @@
  • @svg('heroicon-o-globe-alt') - {{ __('Website', [], !is_signed_language($language) ? $language : locale()) }} + {{ __('Website') }}
  • @endif diff --git a/resources/views/projects/partials/overview.blade.php b/resources/views/projects/partials/overview.blade.php index df8ddfd22..d4a188ea4 100644 --- a/resources/views/projects/partials/overview.blade.php +++ b/resources/views/projects/partials/overview.blade.php @@ -1,7 +1,7 @@

    {{ __('Project goals') }}

    -{{ safe_nl2br($project->getWrittenTranslation('goals', $language)) }} +{{ safe_nl2br($project->goals) }}

    {{ __('Engagements') }}

    @@ -24,7 +24,7 @@ -{{ safe_nl2br($project->getWrittenTranslation('scope', $language)) }} +{{ safe_nl2br($project->scope) }}

    {{ __('Geographical areas this project will impact') }}

    @@ -50,7 +50,7 @@

    {{ __('Not in this project') }}

    - {{ safe_nl2br($project->getWrittenTranslation('out_of_scope', $language)) }} + {{ safe_nl2br($project->out_of_scope) }} @endif

    {{ __('Project timeframe') }}

    @@ -92,7 +92,7 @@

    {{ __('Tangible outcomes of this project') }}

    - {{ safe_nl2br($project->getWrittenTranslation('outcomes', $language)) }} + {{ safe_nl2br($project->outcomes) }} @endif

    {{ __('Project reports') }}

    diff --git a/resources/views/projects/partials/team.blade.php b/resources/views/projects/partials/team.blade.php index 7cf2e570a..0d7735d12 100644 --- a/resources/views/projects/partials/team.blade.php +++ b/resources/views/projects/partials/team.blade.php @@ -29,7 +29,7 @@ @endif @if ($project->consultant_responsibilities) - {{ safe_nl2br($project->getWrittenTranslation('consultant_responsibilities', $language)) }} + {{ safe_nl2br($project->consultant_responsibilities) }} @endif @endif diff --git a/resources/views/regulated-organizations/partials/about.blade.php b/resources/views/regulated-organizations/partials/about.blade.php index 8889a4a83..bba312f5c 100644 --- a/resources/views/regulated-organizations/partials/about.blade.php +++ b/resources/views/regulated-organizations/partials/about.blade.php @@ -1,6 +1,6 @@

    {{ __('About the organization') }}

    -{{ safe_nl2br($regulatedOrganization->getWrittenTranslation('about', $language)) }} +{{ safe_nl2br($regulatedOrganization->about) }}

    {{ __('Service areas') }}

    diff --git a/resources/views/regulated-organizations/show.blade.php b/resources/views/regulated-organizations/show.blade.php index 726152c72..5eb991fc1 100644 --- a/resources/views/regulated-organizations/show.blade.php +++ b/resources/views/regulated-organizations/show.blade.php @@ -1,5 +1,5 @@ - {{ $regulatedOrganization->getWrittenTranslation('name', $language) }} + {{ $regulatedOrganization->name }} @if (auth()->hasUser() && auth()->user()->isAdministrator() && @@ -16,7 +16,7 @@ @endif

    - {{ $regulatedOrganization->getWrittenTranslation('name', $language) }} + {{ $regulatedOrganization->name }} @if ($regulatedOrganization->checkStatus('draft')) {{ __('Draft mode') }} @endif @@ -61,7 +61,7 @@
  • @svg('heroicon-o-globe-alt') - {{ __('Website', [], !is_signed_language($language) ? $language : locale()) }} + {{ __('Website') }}
  • @endif diff --git a/resources/views/vendor/hearth/components/locale-select.blade.php b/resources/views/vendor/hearth/components/locale-select.blade.php index dcfe7d68d..6f3de7ea9 100644 --- a/resources/views/vendor/hearth/components/locale-select.blade.php +++ b/resources/views/vendor/hearth/components/locale-select.blade.php @@ -7,7 +7,7 @@ @if ($invalid) aria-invalid="true" @endif> @foreach ($locales as $key => $locale) @endforeach diff --git a/tests/Feature/HasTranslationsFallbackTest.php b/tests/Feature/HasTranslationsFallbackTest.php new file mode 100644 index 000000000..32e272644 --- /dev/null +++ b/tests/Feature/HasTranslationsFallbackTest.php @@ -0,0 +1,52 @@ + 'string', + ]; + + protected $fillable = [ + 'prop', + ]; + + protected $casts = [ + 'prop' => 'array', + ]; + + public array $translatable = [ + 'prop', + ]; +} + +test('Translation fallback', function (array $translations, string $requestedLocale, string $expectedTranslation) { + $model = TestModel::create([ + 'prop' => $translations, + ]); + + expect($model->getTranslation('prop', $requestedLocale))->toBe($expectedTranslation); + + App::setLocale($requestedLocale); + + expect($model->prop)->toBe($expectedTranslation); +})->with([ + 'No ASL; fallback to EN' => [['en' => 'test'], 'asl', 'test'], + 'No LSQ; fallback to FR' => [['fr' => 'teste'], 'lsq', 'teste'], + 'No FR; fallback to EN' => [['en' => 'test'], 'fr', 'test'], + 'No LSQ and FR, fallback to EN' => [['en' => 'test'], 'lsq', 'test'], + 'Has ASL; no fallback' => [['asl' => 'test', 'en' => 'don’t fallback'], 'asl', 'test'], + 'Has LSQ; no fallback' => [['lsq' => 'teste', 'fr' => 'don’t fallback'], 'lsq', 'teste'], + 'Has FR; no fallback' => [['fr' => 'teste', 'en' => 'don’t fallback'], 'fr', 'teste'], + 'Has EN; no fallback' => [['fr' => 'teste', 'en' => 'test'], 'asl', 'test'], +]); diff --git a/tests/Unit/LanguageHelpersTest.php b/tests/Unit/LanguageHelpersTest.php index 37971872d..17cc2068a 100644 --- a/tests/Unit/LanguageHelpersTest.php +++ b/tests/Unit/LanguageHelpersTest.php @@ -63,8 +63,8 @@ }); test('get written language for signed language', function () { - expect(get_written_language_for_signed_language('asl'))->toEqual('en'); - expect(get_written_language_for_signed_language('lsq'))->toEqual('fr'); + expect(to_written_language('asl'))->toEqual('en'); + expect(to_written_language('lsq'))->toEqual('fr'); }); test('get signed language for written language', function () {