Uniform in multilingual setup within ajax support

I have multiple uniform forms sended throu ajax.
Everything works as expected.

Now I need translate emails and subjects in multilingual setup.
I discussed this scenario with author of uniform: https://github.com/mzur/kirby-uniform/issues/188

But when I’m setting language to route I’m still getting only default language.

I don’t know how to solve it.

Thanks

Hi, did you use $site->visit() in your route as shown in the docs: https://getkirby.com/docs/guide/routing#multi-language-setup ?

This should effectively switch to the targeted language.

How can I use it within Uniform?
I don’t need visit some page, I need some logic behind uniform forms.

Thanks!

Could you post the complete code?

// config.php

<?php

$formCareer = function () {
    $form = new \Uniform\Form([
        'page' => [
            'rules' => ['required'],
            'message' => t('forms.errors.page'),
        ],
        'email' => [
            'rules' => ['required', 'email'],
            'message' => t('forms.errors.email'),
        ],
        'gdpr' => [
            'rules' => ['required'],
            'message' => t('forms.errors.gdpr'),
        ],
    ]);

    // Perform validation and execute guards.
    $form->withoutFlashing()->withoutRedirect()->guard();

    if (!$form->success()) {
        // Return validation errors.
        return Response::json($form->errors(), 400);
    }

    // If validation and guards passed, execute the action.
    $form->emailAction([
        'to' => 'patrik.illy@gmail.com',
        'from' => 'noreply@soma-eng.com',
        'subject' => t('emails.career.subject'),
        'template' => 'career-admin',
        'replyTo' => $form->data('email'),
    ])
    ->emailAction([
        'to' => $form->data('email'),
        'from' => 'noreply@soma-eng.com',
        'subject' => t('emails.career.subject'),
        'template' => 'career-user',
        'replyTo' => 'patrik.illy@gmail.com',
    ]);

    if (!$form->success()) {
        // This should not happen and is our fault.
        return Response::json($form->errors(), 500);
    }

    // Return code 200 on success.
    return Response::json([], 200);
};

return [
    'routes' => [
        [
            'pattern' => '/career',
            'language' => 'en',
            'method' => 'POST',
            'action' => $formCareer
        ],
        [
            'pattern' => '/karriere',
            'language' => 'de',
            'method' => 'POST',
            'action' => $formCareer
        ],
    ]
];


// career-user.html.php

<?= page('career')->email()->kirbytext() ?>

Is it enough?

@bvdputte is right, you should set the page to the correct language.

Routes:

  'routes' => [
        [
            'pattern' => 'career',
            'language' => 'de',
            'method' => 'POST',
            'action' => $formCareer
        ],
        [
            'pattern' => 'karriere',
            'language' => 'de',
            'method' => 'POST',
            'action' => $formCareer
        ],
      
    ]

Callback:

$formCareer = function ($language) {
    $form = new \Uniform\Form([
   'page' => [
            'rules' => ['required'],
            'message' => t('forms.errors.page'),
        ],
        'email' => [
            'rules' => ['required', 'email'],
            'message' => t('forms.errors.email'),
        ],
        'gdpr' => [
            'rules' => ['required'],
            'message' => t('forms.errors.gdpr'),
        ],
   
    ]);

    // Perform validation and execute guards.
    $form->withoutFlashing()->withoutRedirect()->guard();

    if (!$form->success()) {
        // Return validation errors.
        return Response::json($form->errors(), 400);
    }
    site()->visit('career', $language);
    // If validation and guards passed, execute the action.
    $form->emailAction([
        'to' => 'patrik.illy@gmail.com',
        'from' => 'noreply@soma-eng.com',
        'subject' => t('emails.career.subject'),
        'template' => 'career-admin',
        'replyTo' => $form->data('email'),
    ])
    ->emailAction([
        'to' => $form->data('email'),
        'from' => 'noreply@soma-eng.com',
        'subject' => t('emails.career.subject'),
        'template' => 'career-user',
        'replyTo' => 'patrik.illy@gmail.com',
    ]);

    if (!$form->success()) {
        // This should not happen and is our fault.
        return Response::json($form->errors(), 500);
    }

    // Return code 200 on success.
    return Response::json([], 200);
};

Thanks! This working.