adrianmak Posted January 20, 2015 Posted January 20, 2015 I'm building a front-end login form on a multi-language pw website. I built a logoff form. I have some translated string in the logoff template file and those has been translated. When I sign in with another language, and logoff, the logoff message won't display the translated string. Here is a video I recorded my logoff template file <?php $lang = ($user->language->name == 'default') ? '' : $user->language->name . '/'; if ($user->isLoggedin()) { $session->logout(); } else { // $session->redirect($config->urls->root . $lang); } $logoutText = __("You have logged out."); //$pageRefresh = "<meta http-equiv='refresh' content='3;url={$config->urls->root}{$lang}'>"; $page->body .= $logoutText; include('./main.php'); ?> my main,php file <?php $signin_text = __("Sign in"); $logoff_text = __("Logout"); ?> <!DOCTYPE html> <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--> <!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]--> <!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]--> <!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]--> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <?php echo $pageRefresh; ?> <title><?php echo $page->title; ?></title> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Place favicon.ico and apple-touch-icon.png in the root directory --> <link rel="stylesheet" type="text/css" href="<?php echo $config->urls->templates?>assets/css/normalize.css" /> <script src="<?php echo $config->urls->templates?>assets/js/vendor/modernizr-2.6.2.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css"> <link rel="stylesheet" type="text/css" href="<?php echo $config->urls->templates?>assets/css/main.css"> </head> <body> <!--[if lt IE 7]> <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p> <![endif]--> <!-- Add your site or application content here --> <nav class="navbar navbar-default navbar-static-top"> <nav class="nav navbar-left"> <div><?php echo $page->title; ?> | Learing processwire</div> </nav> <?php $lang = ($user->language->name == 'default') ? '' : $user->language->name . '/'; if ($user->isLoggedin()) { echo <<< _OUT <nav class="nav navbar-right"> Welcome $user->name<br/> <a href="{$config->urls->root}{$lang}user/logoff">$logoff_text</a> </nav> _OUT; } else { echo <<< _OUT <div class="navbar-right"> <a href="{$config->urls->root}{$lang}user/login">$signin_text</a> </div> _OUT; } ?> </nav> <div class="header"> <ul class="nav nav-pills"> <?php $homepage = $pages->get('/'); foreach($homepage->and($homepage->children) as $item) { if($item->id == $page->rootParent->id) echo "<li class='current'>"; else echo "<li>"; echo "<a href='$item->url'>$item->title</a></li>"; } ?> </ul> <ul class="languages"> <?php $currentLanguage = $user->language; // remember language foreach($languages as $language) { if(!$page->viewable($language)) continue; // is page viewable in this language? $user->language = $language; if($language->id == $currentLanguage->id) { echo "<li class='current'>"; } else { echo "<li>"; } echo "<a href='$page->url'>$language->title</a></li>"; } $user->language = $currentLanguage; // restore language ?> </ul> </div> <div class="content"> <?php echo $page->body; ?> </div> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script> <script>window.jQuery || document.write('<script src="<?php echo $config->urls->templates?>assets/js/vendor/jquery-1.10.2.min.js"><\/script>')</script> <script src="<?php echo $config->urls->templates?>assets/js/plugins.js"></script> <script src="<?php echo $config->urls->templates?>assets/js/main.js"></script> <!-- Google Analytics: change UA-XXXXX-X to be your site's ID. --> <script> (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]= function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date; e=o.createElement(i);r=o.getElementsByTagName(i)[0]; e.src='//www.google-analytics.com/analytics.js'; r.parentNode.insertBefore(e,r)}(window,document,'script','ga')); ga('create','UA-XXXXX-X');ga('send','pageview'); </script> </body> </html>
ESRCH Posted January 20, 2015 Posted January 20, 2015 Hi adrianmak, This seems to be a bug, I submitted a pull request to correct this. The problem is that when logging out, the user is changed to guest, and the language is set back to default. Interestingly, the $user variable still points to the logged out user (adrian in your case), while wire('user'), which is used by the __() translation function, points to guest. While waiting for the correction to the core, you can solve this issue by adding the following line after $session->logout(): wire('user')->language = $user->language; This will set the language back to what it was before logging out, and the correct translation will be shown. 1
Soma Posted January 20, 2015 Posted January 20, 2015 I have used this to redirect to language the user was in. if(wire("input")->get->logout == 1){ $lang = wire("user")->language; wire("session")->logout(); wire("session")->redirect(wire("page")->localUrl($lang)); }
ESRCH Posted January 20, 2015 Posted January 20, 2015 Just as a side note, instead of doing $lang = ($user->language->name == 'default') ? '' : $user->language->name . '/'; and then <a href="{$config->urls->root}{$lang}user/logoff">$logoff_text</a> it seems easier to do the following: <a href="<?php echo $pages->get('/user/logoff/')->url; ?>">$logoff_text</a> $page->url automatically gives you the localized url for the user's language.
adrianmak Posted January 21, 2015 Author Posted January 21, 2015 Just as a side note, instead of doing $lang = ($user->language->name == 'default') ? '' : $user->language->name . '/'; and then <a href="{$config->urls->root}{$lang}user/logoff">$logoff_text</a> it seems easier to do the following: <a href="<?php echo $pages->get('/user/logoff/')->url; ?>">$logoff_text</a> $page->url automatically gives you the localized url for the user's language. +1
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