Juergen Posted August 22, 2014 Share Posted August 22, 2014 (edited) I have setted up a multilingual site with 2 languages (German and English). Goal:If a user logs in in frontend the language would automatically change to his prefered language (entered in his user profile). F.e.: John Smith has English as his prefered language (stored in the language field). The default language of the site is German. So if he logs in, the site language should be changing to English until he logs out. I use the following language switcher in the frontend. <form id="language-switcher" name="language-switcher" class="navbar-form navbar-right" role="form"> <select class='form-control' 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> </form> This language switcher code is from another entry and works quite well. The problem is that $user->language->id is the current state in the language switcher and not the stored value in John Smiths profile, so it is dynamic and changes every time I activate the language switcher. I can get the stored value with a static call of the db. $loginuser = $user->id; $sql = "SELECT data FROM field_language WHERE pages_id=$loginuser"; $result = wire('db')->query($sql); list($data) = $result->fetch_row(); $userlanguageindb = $data; So the variable $userlanguageindb is the real stored data. So far so good. But how can I influence the site language after the login?? This is my main problem. I would be glad if someone can point me into the right direction. Best regards Edited August 22, 2014 by kongondo Code formatting and title Link to comment Share on other sites More sharing options...
Juergen Posted August 22, 2014 Author Share Posted August 22, 2014 Has anyone an idea??? Link to comment Share on other sites More sharing options...
Sergio Posted August 22, 2014 Share Posted August 22, 2014 Hi Juergen. I'm a newbie, but I think you can get the profile language using the user ID, like this: $userID = 41; $profile = $pages->get($userID); if ($user->language != $profile->language) { //if the user session language is not the same of his/her profile. echo "User language is different from session"; } else { echo $user->language->title; echo '<br /> '.$user->name; } Link to comment Share on other sites More sharing options...
Juergen Posted August 22, 2014 Author Share Posted August 22, 2014 Hello Sergio, thank you for your answere! But: I can get the user language with the direct call of the db. My problem is to trigger the site language in that way. I have created a session and compared it in an if statement like you do, but thats the wrong way. It doesnt work. My code: if ($user->isLoggedin()){ if ($_SESSION['count'] == 1){ $user->language->id = $userlanguageindb; }} $user->language->id doesnt influence the site language - thats the main problem. Best regards Jürgen Link to comment Share on other sites More sharing options...
Soma Posted August 23, 2014 Share Posted August 23, 2014 The language is set by what url you view a page. 1 Link to comment Share on other sites More sharing options...
Juergen Posted August 23, 2014 Author Share Posted August 23, 2014 So I have to redirect after the login to the profile page in the desired language. Step 1: Call the login page Step 2 Enter your userdata (username and password) Step 3 redirect to the profile page in the users prefered language This is excactly where I am struggling. my redirect code is: if($user->isLoggedin()) $session->redirect($profileurl); I get the $profileurl in this way: $profile = $pages->get(1084);$profileurl = $profile->url; But how can I get the url in the desired language ? Best regards Link to comment Share on other sites More sharing options...
Juergen Posted August 23, 2014 Author Share Posted August 23, 2014 I found the solution (thanx to Soma to put me into the right direction) The essential part is to redirect the user in his language to the next page (in this case a profile page where he can update his user information). The main problem was to get the redirection url in the users prefered language. Here is the code I use (this piece of code is in the login page): if($user->isLoggedin()) {$loginuser = $user->id;$sql = "SELECT data FROM field_language WHERE pages_id=$loginuser";$result = wire('db')->query($sql);list($data) = $result->fetch_row();foreach($languages as $language) { $profileurl = $pages->get(1084)->localUrl($data); }$session->redirect($profileurl);} 1084 is the id of the profile page where I want to redirect. The first step is to make a static call of the db to get the stored user language (not the current of the page!!!!). This is the reason why i dont use the PW api. $data is the variable for the language stored in the users profile (in this case 1011 as the id for the English language) The last step is to create the url for the English version ($profileurl) - it leads to /en/my-profile in my case - this is the path to English profile page. To redirect to this page I use this url in the session. Ready to go. I dont know if this is the perfect solution but it works. Link to comment Share on other sites More sharing options...
Soma Posted August 23, 2014 Share Posted August 23, 2014 Ok, no need to use SQL here. Also not sure why you're foreach($languages as $language)? You can uncache the user and load it again via API, so it will load it fresh with language stored on user page. try{ $u = $session->login($username, $pass); if($u && $u->id){ $pages->uncache($user); // make sure it doesn't get user from cache $langID = $users->get($u->id)->language->id; // now load the user page $profileUrl = $pages->get(1003)->localUrl($langID); $session->redirect($profileUrl); } else { $loginError = "Login failed"; } } catch(Exception $e){ $loginError = $e->getMessage(); } I posted it in various login threads already, because all example login code posted in the forums has some flaws in that it doesn't catch errors when login failed multiple times (login throttle). To catch that you need to use a try catch method like in my example. 2 Link to comment Share on other sites More sharing options...
Juergen Posted August 23, 2014 Author Share Posted August 23, 2014 Thank you Soma, your solution works perfect. SQL-query: to get stored language and not the current language - this was the idea, but unchaching the user is much better foreach: to grab all links to the profile page in each language and pick out the one which fetches the user language - this was the idea behind it. Best regards Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now