Jump to content

Use multi-language without URL changes


a-ok
 Share

Recommended Posts

An odd question but I'm curious.

Is it possible to use multi-language without URL changes? So it'll only show a different page title and/or content based on what the $user-language is set to?

In a nutshell I want to use the multi-language functionality to display different prices/currencies (executed via a function) and it requires from server side conditioning that multi-language is perfect for.

I've got it currently set up as follows:

1135187293_ScreenShot2018-11-20at17_52_15.thumb.png.a89f31f38160f8fb256a35520b2f58ec.png

<select onchange="window.location=$(this).val();">
	<?php foreach ($languages as $language) : ?>
		<?php $url = $page->localUrl($language); ?>
		<option <?php if ($user->language->id == $language->id) : ?>selected<?php endif; ?> value="<?php echo $url; ?>"><?php echo $language->title; ?></option>
	<?php endforeach; ?>
</select>

However when I check what the $user->language is upon different selections it's always the same (default).

Edited by a-ok
Link to comment
Share on other sites

If you don't have different names, then localUrls with be identical. Your select will just reload the page. What you need to do is pass some kind of indicator (e.g. a GET parameter with the language id) to the server and set the user's language before the page is rendered.

So you need to output something like this in your page for the language selector:

<select onchange="window.location=$(this).val();">
	<?php foreach ($languages as $language) : ?>
		<?php $url = $page->localUrl($language) . "?lang=" . $language->id; ?>
		<option <?php if ($user->language->id == $language->id) : ?>selected<?php endif; ?> value="<?php echo $url; ?>"><?php echo $language->title; ?></option>
	<?php endforeach; ?>
</select>

Then, in the backend (best site/ready.php), you need to adapt the current language according to the parameter and store the selection in the session to make PW remember it (untested):

<?php

if($input->get->lang) {
  $newLang = (int)$input->get->lang;
  if(! $languages->get($newLang) instanceof NullPage) {
    $session->set("currlang", $newLang);
  }
}

$newLang = $session->get("currlang");
if($newLang) {
  $user->language = $languages->get($newLang);
}

You should think hard whether you want to do that though. Google & Co. will put a massive penalty on your site if you show differing content on identical URLs. Languages are different beasts from locations. I have, e.g., some colleagues from foreign countries who are a lot more comfortable doing their shopping in English rather than in German, but they still need to see the usual Euro prices and German VAT.

So you might just use a different thing than the language, e.g. a GET variable named "country" that you store in the session just like the code above does and pass that to your conversion function.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...