How to use t ('varialbe') in email templates?

Hi all!

How to use t (‘varialbe’) in email templates?

I tried like this, but it doesn’t work:

    $kirby->setCurrentLanguage('en');
    
    $kirby->email([
        'template' => $template,
        'to' => $to,
        'from' => $site->email_from()->value(),
        'replyTo' => $site->email_from()->value(),
        'subject' => $subject,
        'data' => [
            'data' => $data
        ],
    ]);

and in email template

<h2><?= t('accept_text') ?></h2>

I think your email template doesn’t know anything about the current language, it would probably make more sense to pass the variable to the template via the data array.

Maybe it can be done with routing? But v3.3.5 needs to be used.

Thanks texnixe!

I tried this option now:

$kirby->setCurrentLanguage('en');
$kirby->email([
            'template' => "accept",
            'to' => $data->email()->value(),
            'from' => $site->email_from()->value(),
            'replyTo' => $site->email_from()->value(),
            'subject' => $subject,
            'data' => [
                'data' => $data,
                'lang' => $lang,
                'text' => t('accept_text'),
                'worktitle' => t('worktitle'),
                'email' => t('email'),
                'phone' => t('phone'),
                'entity' => t('entity')
            ],
        ]);

and email template:

<?= $text ?>

But does not work (
Language does not switch.

tried to make a test controller:

<?php
return function ($kirby, $page) {
    $kirby->setCurrentLanguage('en');       
    $kirby->email([
        'template' => 'accept',
        'to' => 'test@me.com',
        'from' => 'noreply@site.com',
        'replyTo' => 'noreply@site.com',
        'subject' => 'test',
        'data' => [
                'data' => array(),
                'lang' => 'en',
                'text' => t('accept_text'),
                'worktitle' => t('worktitle'),
                'email' => t('email'),
                'phone' => t('phone'),
                'entity' => t('entity')
            ],
    ]);
        
   return compact('page');
};

Language did not switch :thinking:

Could you try following?

$kirby->setCurrentTranslation('en');
$kirby->setCurrentLanguage('en');

Thanks!

Now let’s try!

Thanks byybora!

$kirby->setCurrentTranslation('en');
work fine!