How to manually set the language for the l() helper in the routes

I want to do an ajax request over the custom routes. Until now my route looks like the following:

c::set('routes', array(
    array(
        'pattern' => 'ajax-projects(:all)',
        'action'  => function($filter) {

          header('X-Robots-Tag: noindex, follow');
          snippet('project.list', array('pages' => site()->pages()));

        }
    )
));

My problem is, that the site is multilingual. So i used the l() helper in my snippet. But when the request goes through ajax, the l::get() helper doesnΒ΄t know which language should be shown. The following Notice appears:

Notice: Trying to get property of non-object in /var/www/…/kirby/branches/multilang/page.php on line 49
…

How can i set the language for the helper manually in my routes?

I found a solution:

c::set('routes', array(
    array(
        'pattern' => '((:any)/)?ajax-projects(:all)',
        'action'  => function($urllang ,$urllangcode, $querystring) {

          $lang = ($urllangcode == '') ? (string) site()->detectedLanguage() : $urllangcode;

          site()->visit('ajax-projects', $lang);

          site()->kirby->localize();

          header('X-Robots-Tag: noindex, follow');
          snippet('project.list', array('pages' => site()->pages()));

        }
    )
));
3 Likes