Multi-Lingual - Redirect to always have code in url?

I came up with this, which seems to work, though it looks a bit hacky:

c::set('routes', array(
  array(
    'pattern' => '(:any)',
    'action' => function($uri) {
      if ($uri == 'de' or $uri == 'en' or substr( $uri, 0, 2 ) === "de" || substr($uri, 0, 2) == 'en') {
        $site = kirby()->site();
        $page = page('home');
        return $site->visit($page);
      } else {
          $site = kirby()->site();
          $page = page($uri);
          go('en/' . $page);
      }
   }
  )
));
1 Like