Switching language without having access to the page object

I know about this:

https://getkirby.com/docs/languages/switching-languages

I don’t have access to the page() because I’m writing a plugin so page() always return the home page object.

Let’s say I have these urls:

/en/about
/de/uber-uns
om-oss

When only having access to for example uber-uns, I want to get the page object from that uri.

From om-oss it’s not a problem because it’s the real url / folder name.

Maybe something like this:

translatedUriToPageObject('uber-uns', 'de');

UPDATE

Here is the case. Build an image gallery on a multi-language setup. It works when the directory matches (default language). It does not work in other languages. It’s important to send the page object because else it will only get the home page object.

kirby()->routes(array(
	array(
		'pattern' => array('(.*?/image/.+)', '(^image/.+)'),
		'action' => function($uri) {
                        $uri = filterOnlyUri($uri);
			$page = page($uri);
			tpl::load(
				kirby()->roots()->templates() . DS . 'image.php',
				array('page' => $page),
				false
			);
			die;
		}
	)
));

It works with $_GET variables because then it’s still on the image parent page but it would be more nice to have routes.

What about this …?

<?php $page = site()->find($uri); ?>

Work for the default language but not for the other languages, so I can’t use that. :confused:

Damn, you’re right. I mixed up my settings for the default language while testing it. Sorry.

Kirby don’t seems to be built to do it the other way around so I needed to build a very dirty solution.

In short, I create and index of all uris with a hook and the do the matching in the route.

Goes into a plugin

The variable names are not optimal, I know.

function foo($page) {
	$array = array();
	$site = site();
	foreach( $site->index() as $item ) {
		foreach($site->languages() as $language) {
			$prefix = '';
			if( $language->code() == 'en' || $language->code() == 'de' ) {
				$prefix = $language->code() . '/';
			}
			$array[$item->uri('sv')][$language->code()] = $prefix . $item->uri($language->code());
		}
	}
	$json = json_encode($array);
	file_put_contents( kirby()->roots()->content() . '/index.json', $json);
}

kirby()->hook('panel.page.create', 'foo');
kirby()->hook('panel.page.update', 'foo');
kirby()->hook('panel.page.delete', 'foo');
kirby()->hook('panel.page.move', 'foo');

Goes into another plugin, or the same

<?php
if(!str::startsWith(url::path(), 'panel') && !str::startsWith(url::path(), 'patterns')) {
	kirby()->routes(array(
		array(
			'pattern' => array('(.*?/image/.+)', '(^image/.+)'),
			'action' => function($uri) {
				$content = file_get_contents( kirby()->roots()->content() . '/index.json' );
				$array = json_decode($content);
				foreach( $array as $sv_uri => $item ) {
					foreach( $item as $lang => $lang_uri ) {
						$match = match($uri, $lang_uri, $sv_uri);
						if( ! empty( $match ) ) {
							echo page($match)->title();
						}
					}
				}
				die;
			}
		)
	));
}

function match($uri, $lang_uri, $sv_uri) {
	$part = explode('/image/', $uri);
	if( $part[0] == $lang_uri ) {
		return $sv_uri;
	}
}

This code is quite specific for my site so if you want to use it you need to rewrite the code to fit your need. Instead see this as an inspiration of how it can work with a dirty workaround.