Translation of URL redirect via page model

Hey all,

Adding on to Methods for URL Cloaking/Redirecting? - #3 by kuwts as the translation does not seem to be working for this solution. Two solutions that I tried are setting the translation in the page model with the lang code:

    $language = $kirby->language();
	$lang_code = $language->code();

public function cloakedUrl() {
          return url($lang_code . 'redirect/' . $this->id());
      }

as well as in the routes in config.php:

'routes' => [
        [
          'pattern' => 'redirect/(:all)',
          'action'  => function ($uri) {
            if ($page = page($uri) ) {
              go($page->translation()->ref_url());
            }

          }
        ]
      ]

Not too sure if it’s simply a syntax issue and this would be set another way, or if it is something else. Any insight is much appreciated, thanks :slight_smile:

Try

 [
          'pattern' => 'redirect/(:all)',
          'language' => '*',
          'action'  => function ($language, $uri) {
            if ($page = page($uri) ) {
              go($page->content($language->code())->ref_url());
            }

          }
        ]

I’m not 100% sure, if $language is a language object (in which calling code() would make sense, or if it returns the language code, so you have to test this.

The cloakedUrl() method would not change, I think.

But in any case, if that was necessary, you would have to get the language code inside your method, not outside.

According to this article: Routing | Kirby the $language object is valid. However, it is not yet translating the field :confused: . Is there a way to dump the ‘routes’ in config.php?