Language routes for custom filtering

I have built a simple website which lists all pages inside a “main” page on the landing page. Using custom routes, I am able to filter these posts by a given category in the url slug:

// config.php
'routes' => function($site) {
        return [
            [
                'pattern' => '(:any)',
                'action' => function ($slug) {
                    if (site()->find($slug)) {
                        // render page normally if it exists,
                        // i.e "mysite.test/info"
                        return page($slug)->render();
                    } elseif (page('main/' . $slug)) {
                        // go to page if it is a page inside the "main" folder,
                        // i.e "mysite.test/main/page" (becomes "mysite.test/page")
                        $page = page('main/' . $slug);
                        return site()->visit($page);
                    } else {
                        // use slug as a filter,
                        // i.e "mysite.test/My+Filter"
                        return page('home')->render([
                            'tag' => $slug
                        ]);
                    }
                }
            ],
            [
                'pattern' => '/main/(:any)',
                'action' => function($uid) {
                    go($uid);
                }
            ]
        ];
    },

unfortunately, I found out today that the site needs to use multiple languages and a frontend language switch.

I’ve added the necessary lines in the config file and updated the panel and content accordingly (english is the main language and german should be using a slug), but the routing is tough and I could use some guidance to rewrite my code properly.

I’ve tried adding the 'en' scope to the existing code and adding another with a 'de' scope, using visit() too but it’s just breaking in all kinds of places… :confounded:is there maybe a way I can rewrite my original routes so it would be easier to add language support?

Slowly getting there:

return [
            [
                'pattern' => '(:any)',
                'language' => '*',
                'action' => function ($language, $tag) {
                    if (site()->find($tag)) {
                        // open page normally if it exists
                        return site()->visit($tag, $language);
                    } elseif (page('main/' . $tag)) {
                        // go to page if it is a main page
                        $page = page('main/' . $tag);
                        return site()->visit($page, $language);
                    } else {
                        // filter main page
                        return page('home')->render([
                            'tag' => $tag
                        ]);
                    }
                }
            ],
            [
                'pattern' => '/de/main/(:any)',
                'action' => function($uid) {
                    go('/de/' . $uid);
                }
            ],
            [
                'pattern' => '/main/(:any)',
                'action' => function($uid) {
                    go($uid);
                }
            ],
        ];

Only the filtering bit isn’t working correctly, it just renders the home page in the default language.

} else {
  // filter main page
  return page('home')->render([
    'tag' => $tag
  ]);
}

Is there a way to use render() in a language specific way, like site()->visit()?

& another question:
Can I make <?= $site->url(); ?> output the url including the language bit? (“mysite.com/test” if english; “mysite.com/de/test” if other language)

No, but if you use site()->visit() before you return render(), it should work.

1 Like

yes, perfect!

^ didn’t realize that is the case by default. my hacky routing just broke it sometimes.

thanks for your help!