Using l::get('...') within a route

I know that you can direct people to a specific language version of a page using routes, but I’m hitting a speedbump trying to insert custom language variables.

Nothing gets written when I do something like this:

  array(
    'pattern' => 'url/path/goes/here',
    'method' => 'POST',
    'action' => function() {
      echo l::get('something');
      return true;
    }
  ),

I can set the detected language with a hack like this:

  array(
    'pattern' => 'url/path/goes/here',
    'method' => 'POST',
    'action' => function() {
      
      // Set detected language
      site()->visit('home', (string) site()->detectedLanguage());
      site()->kirby->localize();

      // Write the string
      echo l::get('something');
      return true;
    }
  ),

It appears that site()->detectedLanguage() gives a value, but site()->language() is still blank at this point…

1 Like