Translations in select field options via options

I’m trying to get translatable options in a select field and I’m trying to do it without building a new custom field plugin :sweat_smile: I’m stuck atm, maybe somebody else has any creative ideas :grimacing:

My blueprint is a basic selectbox and I want it to have configurable options. (and translated)

First attempt: Just putting the translations in the option field and hoping that the array gets automagically converted to the right blueprint syntax. (failed, I guess this is not the feature I am looking for :smiley:)

buttonType:
    width: 1/2
    label:
      en: Button type
      nl: Knop type
    type: select
    options: query
    query: kirby.options('buttons')
<?php
return [
    'buttons' => [
        'default' => [
            'nl' => 'Standaard',
            'en' => 'Default',
        ]
    ]
];

Second attempt: Trying to use an api to use the active user language and fetch the right options. Failed because I can’t access the language on runtime. Third attempt putting the routes in the ready one but also failed because (obviously) routes need to be loaded on runtime.

buttonType:
    width: 1/2
    label:
      en: Button type
      nl: Knop type
    type: select
    options: api
    api: "{{ site.url }}/components/common/links/button.json"
<?php
return [
    'buttons' => [
        'nl' => [
            'default' => 'Standaard',
        ],
        'en' => [
            'default' => 'Default',
        ],
    ],
    'ready' => function($kirby) {
        return [
            'routes' => [
                [
                    'pattern' => 'components/common/links/button.json',
                    'action'  => function () {
                        return option('buttons.'. kirby()->user()->language());
                    }
                ]
            ],
        ];
    }
];

So I’m out of good ideas. Any other good or creative ideas out there to make this happen? :sweat_smile:

I just had a last creative idea and managed to solve it. Here’s the solution (passing language parameter to the json)

buttonType:
    width: 1/2
    label:
      en: Button type
      nl: Knop type
    type: select
    options: api
    api: "{{ site.url }}/components/common/links/button.json?l={{kirby.user.language}}"
'routes' => [
        [
            'pattern' => 'components/common/links/button.json',
            'action'  => function () {
                return option('genx.core.components.common.links.buttons.'. get('l', 'nl'));
            }
        ]
    ],

:partying_face:

For whoever would also like to to this, small update to catch unconfigured languages:

'routes' => [
        [
            'pattern' => 'components/common/links/button.json',
            'action'  => function () {
                if(!array_key_exists(get('l', 'en'), option('genx.core.components.common.links.buttons')))
                {
                    $l = 'en';
                }
                else
                {
                    $l = get('l', 'en');
                }

                return option('genx.core.components.common.links.buttons.'. $l);
            }
        ]
    ],