Go() Helper inside Routes - URLs with language code

Hi,
I’m working on a simple Online-Shop with the MERX-Plugin, and run into a problem with the go()-Helper inside routes in combination with a multi language setup. (standard language: de, secondary language: en)

[
	'pattern' => 'empty-cart',
	'method' => 'post',
	'action'  => function () {
		try {
			$cart = cart();
			$cart->delete();
			go('/cart');
		} catch (Exception $ex) {
			return $ex->getMessage();
		}
	},
],

this example-route uses the go()-Helper to go to the cart-Page after removing a product from the cart.
When a have english as current language a then use the empty-cart-route, it does what it should (removes all items and links me to the cart) except the language switches to the standard language. I want to see the cart in the current language.
Any suggestions? Am I use the go() helper wrong?

Maybe. Untested, cause im on my Phone. :grin:
I would try to get first the current language.
Then the page url and after that i would trigger the go.

// Current language
$language = $kirby->language();
$page = $page('cart');
$page->url($language->code());
return $page->go();

Maybe just an Idea :slightly_smiling_face: im back in office on monday and can test it.

Hey, thanks for your idea. Unfortunately it doesn’t work.
After some further testing I can say: It looks like the $language = $kirby->language(); gives not the current language correctly. Is it because I call the object/method inside a route?
I tried this

'pattern' => 'empty-cart',
				'method' => 'post',
				'action'  => function () {
					$kirby = kirby();
					$language = $kirby->language();
					try {
						$cart = cart();
						$cart->delete();
						echo($language);
					} catch (Exception $ex) {
						return $ex->getMessage();
					}
				},

…and I can see that it returns “DE” although the current language is “EN”…
:frowning:

In a multi-language environment, you can set the language scope:

[
    'pattern' => 'whatever',
    'language' => '*',
    'action' => function ($language) {
	
		dump($language);
	
    }
],

$language in this context gives you a language object.

Hi and thanks,
i am ashamed of myself, perhaps I’m to stupid, but I don’t understand it. :frowning:
@texnixe when you write “$language in this context gives you a language object” - does it gives me the language object of the current language? - this is, what I would need, but when I try your code I always see ‘de’ even when my current language is ‘en’…

Yes.

With the route above, when you call, for example,

https://example.com/en/whatever

You should get the English language object

When you call

https://example.com/de/whatever

You should get the German language object

However, if you call the route without the language code in the URL ( https://example.com/whatever), you will always only get the default language.

Okay, thanks.
I think I described my problem misleading.
my case is different, I want to achieve the following:

When I am on a page, for example (en is not the default language)

https://example.com/en/cart

and then press a button which call a route like “do some action and then go to a page”

→ my goal is, that it goes to the page and uses the same language version, like the page from where I come from…

do you understand?

Well, of course I understand. But as I outlined above, you need to pass the language on to the route in one way or the other, that means.

  1. your button calls the route with the language code in the url
  2. You pass a query string with the language and fetch that in your route: Routes | Kirby CMS

Okay, fine, I get it!

Thanks a lot.