knuckleswtf / scribe

Generate API documentation for humans from your Laravel codebase.✍
https://scribe.knuckles.wtf/laravel/
MIT License
1.7k stars 303 forks source link

Nested translations for validation rules rely on non-universal translation feature #824

Closed bjhijmans closed 6 months ago

bjhijmans commented 6 months ago

Scribe version

4.33.0

PHP version

8.1

Framework

Laravel

Framework version

10.*

Scribe config

not relevant

What happened?

This code is in ParsesValidationRules:

$description = trans("validation.{$rule}");
        // For rules that can apply to multiple types (eg 'max' rule), Laravel returns an array of possible messages
        // 'numeric' => 'The :attribute must not be greater than :max'
        // 'file' => 'The :attribute must have a size less than :max kilobytes'
        if (is_array($description)) {
            $description = $description[$baseType];
        }

It expects trans to return an array for the max rule. However, our application uses a different translation engine (specifically joedixon/laravel-translation with translations in the database) that does not support returning arrays.

In any case, I have fixed the issue by changing it to:

$translationString = "validation.{$rule}";
$description = trans($translationString);

if ($translationString === $description) {
    $translationString = "validation.{$rule}.{$baseType}";
    $translated = trans($translationString);
    if ($translated !== $translationString) {
        $description = $translated;
    }
}

I have no idea how widespread this issue is, and I haven't found any official docs on creating translation engines. Maybe it's the engine's fault. Unfortunately, it was a bit of a pain to fix, because I had to extend 5 strategies that use the ParsesValidationRules trait and add a test that checks the original getDescription for code changes. Would you consider adding this change?

Docs

shalvah commented 6 months ago

Yeah, sure, send in a fix.