Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: model lsq translations don't fallback to fr (resolves #2014, #2026, #2028, #2029) #2027

Merged
merged 17 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/Http/Controllers/EngagementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,10 @@ public function updateCriteria(UpdateEngagementSelectionCriteriaRequest $request

public function show(Engagement $engagement)
{
$language = request()->query('language');

return view('engagements.show', [
'language' => $language ?? locale(),
'project' => $engagement->project,
'engagement' => $engagement,
]);
Expand Down
4 changes: 0 additions & 4 deletions app/Http/Controllers/IndividualController.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ 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(),
]));
Expand Down
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
4 changes: 0 additions & 4 deletions app/Http/Controllers/RegulatedOrganizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,6 @@ public function show(RegulatedOrganization $regulatedOrganization): View

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/StoreEngagementLanguagesRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function rules(): array
return [
'languages' => 'required|array|min:1',
'languages.*' => [
Rule::in(array_keys(get_available_languages())),
Rule::in(array_keys(get_available_languages(true))),
],
];
}
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: 2 additions & 0 deletions app/Http/Requests/UpdateProjectTeamRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function rules(): array
'contact_person_response_time' => 'required|array',
'contact_person_response_time.en' => 'required_without:contact_person_response_time.fr|nullable|string',
'contact_person_response_time.fr' => 'required_without:contact_person_response_time.en|nullable|string',
'contact_person_response_time.*' => 'nullable|string',
];
}

Expand All @@ -52,6 +53,7 @@ public function attributes(): array
'contact_person_response_time' => __('Approximate response time'),
'contact_person_response_time.en' => __('Approximate response time (English)'),
'contact_person_response_time.fr' => __('Approximate response time (French)'),
'contact_person_response_time.*' => __('Approximate response time'),
];
}

Expand Down
2 changes: 1 addition & 1 deletion app/Models/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function getSlugOptions(): SlugOptions

public function preferredLocale(): string
{
return get_written_language_for_signed_language(
return to_written_language(
User::whereBlind('email', 'email_index', $this->contact_person_email)->first()->locale ?? locale()
);
}
Expand Down
2 changes: 1 addition & 1 deletion app/Models/RegulatedOrganization.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function getSlugOptions(): SlugOptions

public function preferredLocale(): string
{
return get_written_language_for_signed_language(
return to_written_language(
User::whereBlind('email', 'email_index', $this->contact_person_email)->first()->locale ?? locale()
);
}
Expand Down
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public static function configureCipherSweet(EncryptedRow $encryptedRow): void

public function preferredLocale()
{
return get_written_language_for_signed_language($this->locale);
return to_written_language($this->locale);
}

public function canAccessFilament(Panel $panel): bool
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
16 changes: 0 additions & 16 deletions app/Traits/HasMultimodalTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,6 @@

trait HasMultimodalTranslations
{
public function getWrittenTranslation(string $attribute, string $code = ''): string
{
/** If no language code was passed, return the default attribute. */
if (! $code) {
return $this->$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
Expand Down
18 changes: 17 additions & 1 deletion app/View/Components/LanguageChanger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,36 @@
namespace App\View\Components;

use Illuminate\Contracts\View\View;
use Illuminate\Support\Str;
use Illuminate\View\Component;

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();
}

public function getLanguageLink(string $locale, bool $asQuery = false): string
{
$route = request()->route();
$params = array_merge(request()->except('language'), [Str::camel(class_basename($this->model)) => $this->model]);

if ($asQuery) {
return route($route->getName(), array_merge($params, ['language' => $locale]));
}

return localized_route_for_locale(route_name($route), $params, $locale);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/View/Components/TranslatableInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function __construct($name, $label, $hint = null, $model = null, $shortLa
{
$languages = $model->languages ?? config('locales.supported');

if (($key = array_search(get_written_language_for_signed_language(locale()), $languages)) !== false) {
if (($key = array_search(to_written_language(locale()), $languages)) !== false) {
unset($languages[$key]);
array_unshift($languages, locale());
}
Expand Down
2 changes: 1 addition & 1 deletion app/View/Components/TranslatableTextarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function __construct($name, $label, $hint = null, $model = null, $shortLa
{
$languages = $model->languages ?? config('locales.supported');

if (($key = array_search(get_written_language_for_signed_language(locale()), $languages)) !== false) {
if (($key = array_search(to_written_language(locale()), $languages)) !== false) {
unset($languages[$key]);
array_unshift($languages, locale());
}
Expand Down
67 changes: 63 additions & 4 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

use App\Settings;
use App\Settings\GeneralSettings;
use Illuminate\Routing\Route;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Route as RouteFacade;
use Illuminate\Support\HtmlString;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -141,7 +143,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 +153,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 +193,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 @@ -232,6 +234,63 @@ function get_language_exonym(string $code, string $locale = null, bool $capitali
}
}

if (! function_exists('localized_route_for_locale')) {
/**
* Gets the localized URL for the named route in the requested locale. It takes the same arguments as the
* `localized_route` method from laravel-multilingual-routes.
*
* This is to address an issue with laravel-sluggable that only returns the localized slug in the applications
* locale.
*
* See: https://github.com/spatie/laravel-sluggable/discussions/228
*/
function localized_route_for_locale(string $name, mixed $parameters, string $locale = null, bool $absolute = true): string
{
// dd(is_null($locale), $locale === $locale);
if (is_null($locale) || $locale === locale()) {
return localized_route($name, $parameters, $locale, $absolute);
}

$originalLocale = locale();

// Change to requested locale to work around https://github.com/spatie/laravel-sluggable/discussions/228
locale($locale);

$localizedURL = localized_route($name, $parameters, $locale, $absolute);

// Restore original locale
locale($originalLocale);

return $localizedURL;
}
}

if (! function_exists('route_name')) {
/**
* Returns the route name. By default it returns the unlocalized but can return the localized name if needed; which
* would be the same as calling the `getName` method on the route directly. If no route is passed in, it will attempt
* to use the current route.
*/
function route_name(Route $route = null, bool $localized = false): ?string
{
$route ??= RouteFacade::getCurrentRoute();
$routeName = $route->getName();

if ($localized) {
return $routeName;
}

foreach (get_supported_locales() as $locale) {
$prefix = "{$locale}.";
if (Str::startsWith($routeName, $prefix)) {
return Str::after($routeName, $prefix);
}
}

return $routeName;
}
}

if (! function_exists('normalize_url')) {
/**
* Normalize a URL by adding a scheme if one isn't already present.
Expand Down Expand Up @@ -355,7 +414,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
Loading