How to translate options in select field?

Hi,
First of all: thanks for Grav, it’s awesome, I really enjoy learning & using it.

I’ve created custom page form with field containing:
data-options@: ‘\Grav\Theme\hagne::kittenStatus’
and my function is:
public static function kittenStatus()
{
return array (
‘s’ => ‘Sold’,
‘r’ => ‘Reserved’,
‘a’ => ‘Available’
);
}

How to translate these options in page creation form?

@mnemotechnikon Yes, Grav makes building websites fun again…

Add the following to /user/themes/hagne/languages.yaml

en:
  STATUS:
    SOLD: Sold
    RESERVED: Reserved
    AVAILABLE: Available
pl:
  STATUS:
    SOLD: Sprzedany
    RESERVED: Zarezerwowany
    AVAILABLE: Dostępny

Add the following to /user/themes/hagne/hagne.php:

use Grav\Common\Grav;
...
public static function kittenStatus()
{
    $language = Grav::instance()['language'];

    return array (
        's' => $language->translate('STATUS.SOLD'),
        'r' => $language->translate('STATUS.RESERVED'),
        'a' => $language->translate('STATUS.AVAILABLE'),
    );
}

If you now switch between languages, you should see the right translation.

Update
See update in post #5

That simple… Thank you. The translation on my select field works fine.
Now after saving my content in frontmatter I see letters instead of translations. I would like to see translations on my page instead of letters. How can I change that?

imie: GIOVANNI
kolor: ‘n 22’
stan: s

imie: GIULIANO
kolor: ‘n 22’
stan: s

@mnemotechnikon Not sure if you would really want translated strings in your *.md files.

  • What if you made a typo in one of your translation? You would than have to update the page(s) too.
  • As developer you would not know what the translated values in the frontmatter mean. What if you want to set the translated value manually using a text editor?

Here are possible solutions for both options.

1. Using constants in frontmatter
Change /user/themes/hagne/hagne.php as follows:

use Grav\Common\Grav;
...
public static function kittenStatus()
{
    $language = Grav::instance()['language'];

    return array (
        'STATUS.SOLD' => $language->translate('STATUS.SOLD'),
        'STATUS.RESERVED' => $language->translate('STATUS.RESERVED'),
        'STATUS.AVAILABLE' => $language->translate('STATUS.AVAILABLE'),
    );
}

In your template ‘default.html.twig’ (or any other) use:

<p>Status: {{page.header.status|t}}</p>

So, you only store a constant in the frontmatter of a page and translate in Admin and in Twig.

2. Using translations in frontmatter

use Grav\Common\Grav;
...
public static function kittenStatus()
{
    $language = Grav::instance()['language'];

    $sold = $language->translate('STATUS.SOLD');
    $reserved = $language->translate('STATUS.RESERVED');
    $available = $language->translate('STATUS.AVAILABLE');

    return array (
        $sold => $sold,
        $reserved => $reserved,
        $available => $available,
    );

In your template ‘default.html.twig’ (or any other) use:

<p>Status: {{page.header.status}}</p>

Update
See update in post #5

@mnemotechnikon I think I should update my answers given…

Continuing on the initial post using the method 'hagne::kittenStatus()' I proposed a solution for translations in PHP in posts #2 and #4.

However, using these translations in PHP, the select field displays the translation belonging to the language being selected for the current page being edited in Admin panel.

On second thought, I think it is more logical to have the select contain the translations for the language of the user (Administrator/Editor) of the Admin panel.
In other words, if the language of the user of the Admin panel is set to Polish, the select should always display the Polish translation independent of the language being selected for the current page.

This can easily be achieved by using the following field definition in '/blueprints/default.yaml':

header.status:
    type: select
    label: KITTENSTATUS
    options:
      'STATUS.SOLD': 'STATUS.SOLD'
      'STATUS.RESERVED': 'STATUS.RESERVED'
      'STATUS.AVAILABLE': 'STATUS.AVAILABLE'

To show the translation in the front-end, use the following in template ‘default.html.twig’ (or any other):

<p>Status: {{page.header.status|t}}</p>

The method 'hagne::kittenStatus()' has therefor become obsolete.

1 Like

Thank you for your help. I got rid of ‘hagne::kittenStatus()’ function and used your suggestions from post #5 - it works fine.