outl1ne / nova-settings

A Laravel Nova tool for editing custom settings using native Nova fields.
MIT License
271 stars 98 forks source link

Translatable fields don't work in view #89

Closed nbartokos closed 2 years ago

nbartokos commented 2 years ago

Hey,

I noticed that my translated fields are shown correct on the nova-settings page, but the fields on my homepage are shown as json string.

On my homepage I use: {!! nova_get_setting('contact_email') !!}

Output: {"en":"email@gmail.com","de":"email@gmail.at"}

On nova-settings: image

I used the "HasTranslations" class in my own Settings-Model: image

I changed the _nova_getsetting function in the helpers.php File as following: image

After this "small" change, everything works perfect now. So did I do something wrong or is this a bug? I works for me now, but I want to know, If I messed something up or found a bug.

Tarpsvo commented 2 years ago

Hmm, you should cast the contact_email to array using the cast functionality in this package. Then when accessing the value, you should access the corresponding locale key.

So basically, you would have:

NovaSettings::addSettingsFields([
  Text::make('Contact email', 'contact_email'),
], ['contact_email' => 'array'], 'Common');

// ...

$email = nova_get_setting('contact_email')[$locale];

That's the easiest way to handle it right now.