Multilang route ArgumentCountError on default translation of site



I read this post which lead to this issue. However, I’m getting a different issue.

c::set('routes', array(
        'pattern' => '(?:(en)/)?message',
        'method' => 'POST',
        'action' => function ($lang) {
            // stuff

Basically, I want to capture the message route on all languages. It works on the English version (en/message), but doesn’t on the Bulgarian version (which is the main one). I get this error:

ArgumentCountError thrown with message “Too few arguments to function Kirby::{closure}(), 0 passed in C:\xampp\htdocs\mebeliroko\kirby\vendor\getkirby\toolkit\helpers.php on line 282 and exactly 1 expected”

#3 ArgumentCountError in C:\xampp\htdocs\mebeliroko\site\config\config.php:28
#2 Kirby:{closure} in C:\xampp\htdocs\mebeliroko\kirby\vendor\getkirby\toolkit\helpers.php:282
#1 call in C:\xampp\htdocs\mebeliroko\kirby\kirby.php:743
#0 Kirby:launch in C:\xampp\htdocs\mebeliroko\index.php:16

My guess is since in the main version, en/ is not present, there are zero capture groups and therefore nothing passed to my handler function. Is that supposed to be this way? I fixed it by changing the pattern to include an empty capture group in case en/ is not present:

'pattern' => '(?:(en)\/|())message'

It works this way because something is always captured and always passed to the function. However, is there a better way? Is there something in Kirby that could handle this situation? I think there should be.


Check this one out: Routes pattern for multilanguage site