Files
timebank-cc-public/app/Polls/Localized.php
T

36 lines
1018 B
PHP

<?php
namespace App\Polls;
/**
* Picks a label from a per-locale array: {"en": "...", "nl": "..."}.
*/
class Localized
{
/**
* The label in the given (or current) locale, falling back to the app fallback
* locale and then to the first label that is filled in.
*/
public static function pick(?array $labels, ?string $locale = null): string
{
$labels = array_filter($labels ?? [], fn ($label) => is_string($label) && trim($label) !== '');
$locale = $locale ?? app()->getLocale();
return $labels[$locale]
?? $labels[config('app.fallback_locale')]
?? (string) reset($labels);
}
/**
* Validation rule: at least one locale has a label.
*/
public static function filledRule(): \Closure
{
return function (string $attribute, $value, \Closure $fail) {
if (!is_array($value) || self::pick($value) === '') {
$fail(__('Fill in at least one language.'));
}
};
}
}