laravel / nova-issues

554 stars 34 forks source link

Model name is being parsed for translation but returns content of translation file as array instead of translation #2774

Closed k2idev closed 4 years ago

k2idev commented 4 years ago

Description:

I upgraded from Nova 3.7.1 to 3.8.2 and now Nova is unable to show the dashboard

because "htmlspecialchars() expects parameter 1 to be string, array given"

in vendor/laravel/nova/resources/views/resources/navigation.blade.php

My understanding of the issue

I have a Model name 'Module' It is registered in Nova as public static $model = 'App\Module';

When I open the dashboard in Nova an error occurs.

The error occurs in this line in navigation.blade.php (line 24) {{ $resource::label() }}

What seems to be happening is that label() tries to translate the name of the model with __('Modules') The helper function __() does this: return app('translator')->get($key, $replace, $locale); and tries to get the translation.

In ressources/lang/en I have a file called modules.php

<?php

return [
    /* English */

    'overview' => 'Overview of Learning Units',
    'content' => 'Content',
    'selfAssessment' => 'Self Assessment',
];

Now the problem is that app('translator')->get($key, $replace, $locale) returns the content of this file and not the translation.

This breaks navigation.blade.php with the "expected string but got array" because app('translator')->get(..) returns

  Array
    (
       [overview] => Overview of Learning Units
       [content] => Content
       [selfAssessment] => Self Assessment
    )

instead of 'Modules'

But it seems that this is bug(?) and should not happen - and it works fine in v 3.7.1

PS: Sorry if there are any formal mistakes in reporting this issue (it's the first time I am doing this :))

davidhemphill commented 4 years ago

Nova’s translation system is designed to be used with Laravel’s JSON translations, not the array based ones.

k2idev commented 4 years ago

Ok - still seems strange that it works in 3.7.1 and stops working in 3.8.2 ?

Suggestion

Maybe add a hint in the documentation that when there is no translation for a model name in the JSON file (resources/lang/vendor/nova), Nova goes looking for a php based file with the model name in the translation directory (resources/lang/en) and returns the array from that file which breaks translation and navigation.

https://nova.laravel.com/docs/3.0/customization/localization.html#overview