Translate panel select box options in front end

(I couldn’t find anything on this topic in the forum and unfortunately this link is dead: Translate checkboxes, select, … for end-users)

I don’t quite get what the best practise is, when translating select box options in the panel via the language files. Or do I use the new query language?

Best case would be that if the panel-user changes the language, also the select box options change accordingly. (I don’t mean changing the user’s general panel language but with the button next to the settings button.)

I have a setup where the panel-user can select a product category via a select box. In the frontend the selected option is shown as a string in a simple <div>.

It would be cool if you can you point me in the right direction!

Thanks!

You can set up the values you want to translate in your language files, then use the t() helper to fetch those values:

https://getkirby.com/docs/guide/languages/custom-language-variables

While you can set you option display to different languages, this only takes effect when the user chooses a different user language, not when changing the content language.

So for example, if you have categories like this:

category:
  label: Category
  type: select
  options:
    option1: Design
    option2: Architecture
    option3: Travel

The in your language file:

<?php

return [
  'code' => 'de',
  'default' => false,
  'direction' => 'ltr',
  'locale' => 'de_DE',
  'name' => 'Deutsch',
  'translations' => [
    'option1' => 'Design',
    'option2' => 'Architektur',
    'option3' => 'Reisen'
  ]
];

And in your template:

<?= t($page->category()->value()) ?>

Ah, cool, I didn’t know that you could layer the t() and fetch $page items. Thanks!

In the panel though, is it possible to have the selected option value displayed as info text in the page list inside pages section?

I tried info: "{{ page.productcategory }}"
and info: "{{ page.productcategory.value }}"
but both display the option not the value.

Thanks for your insight!

The value is what is saved in your content file, you probably mean you want to show what the user sees in the select field? Or the translated string?

Ah, yeah! Doesn’t have to be the translated string.
Like info: "{{ page.category }}" would show “Architecture” or “Reisen” next to the page title.

In a template, you could get the displayed value like this:

$field = $page->blueprint()->field('category')['options'][$page->category()->value()];

So you could create a custom field method and use that to display the desired value.

Thanks!
Do you think – with the queries-language in the panel – it’s possible to achieve the following? To display the selected option.

41

Yes, that’s what I think and why I suggested it.

Ah, sorry, I misunderstood.

But if want to display the part of the option that is show in the select (not the part that is saved to file), then you need a custom method, because you can’t use array indices in the query language.

1 Like