You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
this is arguably a redundancy, because your dictionary entries will usually coincide exactly with the field name, and an option to automate it away it could be useful.
I'm sure this is way too hacky to make it into the core - I'm new and don't really know what I'm doing! But a feature along these lines, more nicely implemented, could be a small useful improvement, and should be non-breaking.
trait assignLabelsFromTranslation
{
/**
* Automatically override all column labels with a translation.
*
* @param string $translation_prefix A prefix to use for the trans() lookup call.
*
*/
public function assignLabelsFromTranslation($translation_prefix = null)
{
$columnsArray = $this->crud->getOperationSetting('fields');
foreach ($columnsArray as $index => $column)
{
if (!$column['name'])
continue;
$translation = trans($translation_prefix.$column['name']);
$columnsArray[$index]['label'] = $translation;
}
$this->crud->setOperationSetting('fields', $columnsArray);
}
}
The text was updated successfully, but these errors were encountered:
There are a few options for this, one that easily comes to mind and would work out of the box without much effort is something like:
CRUD::setOperationSetting('translatableLabels', true) // opt-in so is non breaking
CRUD::setOperationSetting('translatableLabelsKey', 'my.custom_file.fields_') // uses the default crud lang file if nothing specified
Going further and making this "future proof" we could think about the concept of FieldLabeler and ColumnLabeler and use only one setting CRUD::setOperationSetting('labeler', 'App\Labelers\MyCustomLabeler::class).
Better DX, but this could easily go wrong if people start wanting Entitier, Attributabler etc, for other attributes that not only label.
Nevertheless I prefer the last concept, in fact, I started working on some prototype that aims to do exactly that: Laravel-Backpack/CRUD#4854
It's called Smart Components but it also has the Smart Attributes attached.
To sum up, we would be splitting all the field attributes into classes with their own defaults(), validation() etc.
In your case you would just need to create (if we didn't already), a TranslatableLabelAttribute extends LabelAttribute with the custom implementation.
I can't focus on working on this right now, but I like the idea of not having to type multiple times the trans('my.custom_text.fieldName') 💡
I am moving this to the community forum and leave it to have a look after v6.
Cheers
pxpm
transferred this issue from Laravel-Backpack/CRUD
Feb 10, 2023
When defining fields/columns in a
CrudController
, the usual way to give the field a translated label is:this is arguably a redundancy, because your dictionary entries will usually coincide exactly with the field name, and an option to automate it away it could be useful.
So for example
would automatically be assigned
(As an option, of course, not by default, to make it non-breaking and of course not everyone needs this!)
As a proof of concept, I created a Trait that does this, below 👇
You would call its
assignLabelsFromTranslation()
method eg insetupCreateOperation()
after defining the fields:it automatically walks through each field and assigns the translated values to each
label
:I'm sure this is way too hacky to make it into the core - I'm new and don't really know what I'm doing! But a feature along these lines, more nicely implemented, could be a small useful improvement, and should be non-breaking.
The text was updated successfully, but these errors were encountered: