Jump to content

Language selector: URL segments are lost when switching language


da²
 Share

Recommended Posts

Hello,

I've recently updated a multi-language site to use the page names module.

Reading the documentation there's nothing to do to make url segments to work with localized urls:

Quote

Page numbers (pagination) and URL segments are fully supported by multi-language page names. You don't have to do anything other than you would without multi-language support. Meaning, you can simply enable these features from your template settings, and you are good to go.


Also documentation provides an example of a language selector:

<select onchange='window.location=$(this).val();'>
<?php
foreach($languages as $language) {
  $selected = '';

  // if this page isn't viewable (active) for the language, skip it
  if(!$page->viewable($language)) continue;

  // if language is current user's language, make it selected
  if($user->language->id == $language->id) $selected = " selected=selected";

  // determine the "local" URL for this language
  $url = $page->localUrl($language);

  // output the option tag
  echo "<option$selected value='$url'>$language->title</option>";
}
?>
</select>

 

Problem: When I'm on a page mysite.com/fr/page-name/17175/, after switching language I go to mysite.com/fr/page-name/, the last url segment is lost and the page displays a 404 because it requires this segment.

Is it the expected behavior?

Is there something in the API I should use to make PW takes care of segments?

Or do I have to manage the segments myself, using $input? 🤔

Edited by da²
Link to comment
Share on other sites

If the developer have to manage segments, here is an update of the documentation example:

<select onchange='window.location=$(this).val();'>
<?php

$urlSegments = $input->urlSegmentStr();

foreach($languages as $language) {
  $selected = '';

  // if this page isn't viewable (active) for the language, skip it
  if(!$page->viewable($language)) continue;

  // if language is current user's language, make it selected
  if($user->language->id == $language->id) $selected = " selected=selected";

  // determine the "local" URL for this language
  $url = $page->localUrl($language);

  // output the option tag
  echo "<option$selected value='$url$urlSegments'>$language->title</option>";
}
?>
</select>

Maybe this could be included in documentation, since it seems to me this should be the default behavior.

Edited by da²
  • 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...