Multilanguage Routing

Hi Kirby Lovers

This question is here; Question concerning the Blog in Kirby 3.5.1

I was wondering if I could use the features of the blog (e.g. tags, categories, summaries, rendering all blog entries together on a page) and yet create a different url?

More explicitely:

Instead of my-site.com/blog/entry
Create my-site.com/entry

There are 3 language DE,EN,TR and DE is main language. I removed “filme, Musik and texte” from url for all languages but when you click language menu then “Home” page doesn’t load. You can check here; https://nedimhazar.de/demo/

Could you please help me how can do correctly this routing?

    'routes' => [
      [
        'pattern' => '(:any)',
        'action'  => function($uid) {
            $page = page($uid);
            if(!$page) $page = page('filme/' . $uid);
            if(!$page) $page = page('musik/' . $uid);
            if(!$page) $page = page('texte/' . $uid);
            if(!$page) $page = site()->errorPage();
            return site()->visit($page);
        }
      ],
      [
        'pattern' => ['filme/(:any)', 'musik(:any)', 'texte(:any)'],
        'action'  => function($uid) {
            go($uid);
        }
      ],
      [
          'pattern' => '(:any)',
          'language' => 'en',
          'action'  => function($language, $uid) {

              $page = page($uid);

              if(!$page) $page = page('films/' . $uid);
              if(!$page) $page = page('music/' . $uid);
              if(!$page) $page = page('articles/' . $uid);
              if(!$page) $page = site()->errorPage();
              return site()->visit($page, $language);
          }
          
      ],
      [
          'pattern' => ['films/(:any)', 'music/(:any)', 'articles/(:any)'],
          'language' => 'en',
          'action'  => function($language, $uid) {
            go($language. '/' . $uid);
          }
        ],
      [
          'pattern' => '(:any)',
          'language' => 'tr',
          'action'  => function($language, $uid) {

              $page = page($uid);

              if(!$page) $page = page('filmler/' . $uid);
              if(!$page) $page = page('muzik/' . $uid);
              if(!$page) $page = page('makaleler/' . $uid);
              if(!$page) $page = site()->errorPage();
              return site()->visit($page, $language);
          }
      ],
      [
          'pattern' => ['filmler/(:any)', 'muzik/(:any)', 'makaleler/(:any)'],
          'language' => 'tr',
          'action'  => function($language, $uid) {
            go($language. '/' . $uid);
          }
      ]
  ],

Regards

I’m not sure but I guess language pattern seems missing.
Did you try with 'language' => '*' like below?

'routes' => [
  [
    'pattern' => '(:any)',
    'language' => '*',
    'action'  => function($uid) {
      ...
    }
  ]
];

Hi Bora yepp I have tried that 'language' => '*'

What happens when you put this on top? :thinking:

[
  'pattern'  => '/',
  'language' => '*',
  'action'   => function($language) {
      return site()->visit('home', $language);
  }
],

Hi Bora!

I tried to put it on top but nothing change :worried:

Hi Bora! I did like that but now main language doesn’t work: https://nedimhazar.de/demo/de/tigris-rebellen

How can I remove that “de” on url

    'routes' => [

      [
          'pattern' => '(:any)',
          'language' => '*',
          'action'  => function($language, $uid) {

              $page = page($uid);

              if(!$page) $page = page('filme/' . $uid);
              if(!$page) $page = page('musik/' . $uid);
              if(!$page) $page = page('texte/' . $uid);
              if(!$page) $page = page('films/' . $uid);
              if(!$page) $page = page('music/' . $uid);
              if(!$page) $page = page('articles/' . $uid);
              if(!$page) $page = page('filmler/' . $uid);
              if(!$page) $page = page('muzik/' . $uid);
              if(!$page) $page = page('makaleler/' . $uid);
             
              return site()->visit($page, $language);
       
          }
          
      ],
      [
          'pattern' => ['filme/(:any)', 'musik/(:any)', 'texte/(:any)', 'films/(:any)', 'music/(:any)', 'articles/(:any)', 'filmler/(:any)', 'muzik/(:any)', 'makaleler/(:any)'],
          'language' => '*',
          'action'  => function($language, $uid) {
            go($language. '/' . $uid);
          }
        ]
        
  ],

You can use models to remove parent page slug like following:

/site/models/subpage.php

<?php

class SubpagePage extends Page
{
    public function urlForLanguage($language = null, array $options = null): string
    {
        if ($language === null || $this->kirby()->defaultLanguage()->code() === $language) {
            return $this->url = $this->site()->urlForLanguage($language) . '/' . $this->slug($language);
        }
        
        return parent::urlForLanguage($language, $options);
    }
}