Jump to content

Language Switcher: do i need to update all URLs in the API?


lenoir
 Share

Recommended Posts

Hi,

I've just setup a website to multiple languages, and I have to say, it's very straight forward and quickly done! 

But still I'm not sure I understand the process correctly. I have the module "Languages Support - Page Names" installed. I can view all pages either in DE or EN, and I'm using this piece of code as a language switcher (from the introduction page):

// remember what language is set to
$savedLanguage = $user->language;

foreach($languages as $language) {

  // if user is already viewing the page in this language, skip it
  if($language->id == $savedLanguage->id) continue;

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

  // set the user's language, so that the $page->url and any other
  // fields we access from it will be reflective of the $language
  $user->language = $language;

  // output a link to this page in the other language
  echo "<li><a href='$page->url'>$language->title: $page->title</a></li>";
}
// restore the original language setting
$user->language = $savedLanguage;

It works for the current page, but as soon as i click on any link, the language switches back to default (in my case german). 
My question is, do I need to update all URLs in the template files? I thought that the language switcher updated the language in the session and kept it this way…

Could you give me light? 

Thanks

Link to comment
Share on other sites

9 minutes ago, Zeka said:

@lenoir

Have you updated names of your homepage? 

Yes, I did. "en" and "de".

But the names of the languages (under "languages") are "default" and "deutsch". But as far as I know, they don't have to (can't) be the same?

Link to comment
Share on other sites

3 hours ago, lenoir said:

It works for the current page, but as soon as i click on any link, the language switches back to default (in my case german). 

Could you provide an example of your template code?

I ask because of something I did wrong in the past.
Some links were static and set within the code itself and didn't come from ProcessWire.

So if you set a the href to /impressum/ it will stay the same no matter what language you wanted to use.

But if you let ProcessWire do the job like $pages->get('id=4')->url it would/should show /impressum/ or /en/imprint/.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

12 minutes ago, wbmnfktr said:

Could you provide an example of your template code?

I ask because of something I did wrong in the past.
Some links were static and set within the code itself and didn't come from ProcessWire.

So if you set a the href to /impressum/ it will stay the same no matter what language you wanted to use.

But if you let ProcessWire do the job like $pages->get('id=4')->url it would/should show /impressum/ or /en/imprint/.

You nailed it. It's an old site (one of my first ever with pw), and I wasn't rendering the URLs correctly. It works now. Thanks a lot for your help, wbnjfktr (whatever that means). ?

  • 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...