Plugin with translatable (not translated) property

i am trying to make make a custom property translatable using the blueprint. so i am not looking for a solution to static translate a property.

current

janitor:
  type: janitor
  label: Clean Cache
  progress: Cleaning Cache...
  job: clean

expected

janitor:
  type: janitor
  label: 
    en: Clean Cache
    de: Cache leeren
  progress: Cleaning Cache...
  job: clean

like in blueprints:Translating blueprints | Kirby CMS

thought i would find something in the docs how to do that for plugins but could not. how to achieve that?

It is right there: https://getkirby.com/docs/reference/extensions/fields#php-definition
Subheadline “Translated values”. You would need to process it like this in your field props PHP definition.

'label' => function (string $label = null) {
   return \Kirby\Toolkit\I18n::translate($label);
},

yield error

Invalid value for “label”

not other debug data in browser console available.
i am using devkit 3.0.0 single-language setup. only user account switched from en to de language in its index.php.

did a print_r/is_null of return value using kirbylog plugin and its null.

sorry to pull you into this thread but maybe apart from @distantnative panel plugin master @sylvainjule knows whats wrong. is my plugin broken or does it simply not work as intended.

1 Like

You should get it working with:

'label' => function ($label = null) {
   return \Kirby\Toolkit\I18n::translate($label, $label);
},

You shouldn’t expect $label to be a string if it’s a translatable object.

Note the second $label arg in the translate function, used as a fallback when the field isn’t translatable (ie. is a simple string) :slight_smile:

2 Likes

@sylvainjule thank you. i should have checked the code for the translate function. :thinking: