Move translate function so it's accessible from all admin parts. Correctly change $l->translate() calls to it

This commit is contained in:
Flavio Copes
2015-08-14 15:05:33 +02:00
parent e0675355e2
commit b80678c74b
3 changed files with 36 additions and 25 deletions

View File

@@ -68,6 +68,11 @@ class Admin
*/ */
public $user; public $user;
/**
* @var Lang
*/
protected $lang;
/** /**
* @var Grav\Common\GPM\GPM * @var Grav\Common\GPM\GPM
*/ */
@@ -91,6 +96,7 @@ class Admin
$this->uri = $this->grav['uri']; $this->uri = $this->grav['uri'];
$this->session = $this->grav['session']; $this->session = $this->grav['session'];
$this->user = $this->grav['user']; $this->user = $this->grav['user'];
$this->lang = $this->grav['user']->language;
} }
/** /**
@@ -156,7 +162,7 @@ class Admin
$l = $this->grav['language']; $l = $this->grav['language'];
$this->setMessage($l->translate('PLUGIN_ADMIN.LOGIN_LOGGED_IN'), 'info'); $this->setMessage($this->translate('PLUGIN_ADMIN.LOGIN_LOGGED_IN'), 'info');
// $redirect_route =$this->getLoginRedirect() ?: $this->uri->route(); // $redirect_route =$this->getLoginRedirect() ?: $this->uri->route();
$redirect_route = $this->uri->route(); $redirect_route = $this->uri->route();
@@ -604,6 +610,11 @@ class Admin
} }
} }
/**
* Renders phpinfo
*
* @return string The phpinfo() output
*/
function phpinfo() { function phpinfo() {
ob_start(); ob_start();
phpinfo(); phpinfo();
@@ -613,4 +624,14 @@ class Admin
$pinfo = preg_replace( '%^.*<body>(.*)</body>.*$%ms','$1',$pinfo); $pinfo = preg_replace( '%^.*<body>(.*)</body>.*$%ms','$1',$pinfo);
return $pinfo; return $pinfo;
} }
/**
* Translate a string to the user-defined language
*
* @param $string the string to translate
*/
public function translate($string) {
return $this->grav['language']->translate($string, [$this->grav['user']->authenticated ? $this->lang : 'en']);
}
} }

View File

@@ -76,7 +76,6 @@ class AdminController
$this->post = $this->getPost($post); $this->post = $this->getPost($post);
$this->route = $route; $this->route = $route;
$this->admin = $this->grav['admin']; $this->admin = $this->grav['admin'];
$this->lang = $this->grav['user']->language;
} }
/** /**
@@ -123,15 +122,6 @@ class AdminController
$this->grav->redirect($base . '/' . preg_replace('|/+|', '/', $path), $this->redirectCode); $this->grav->redirect($base . '/' . preg_replace('|/+|', '/', $path), $this->redirectCode);
} }
/**
* Translate a string to the user-defined language
*
* @param $string the string to translate
*/
protected function translate($string) {
return $this->grav['language']->translate('PLUGIN_ADMIN.LOGIN_FAILED', [$this->grav['user']->authenticated ? $this->lang : 'en']);
}
/** /**
* Handle login. * Handle login.
* *
@@ -142,7 +132,7 @@ class AdminController
if ($this->admin->authenticate($this->post)) { if ($this->admin->authenticate($this->post)) {
// should never reach here, redirects first // should never reach here, redirects first
} else { } else {
$this->admin->setMessage($this->translate('PLUGIN_ADMIN.LOGIN_FAILED'), 'error'); $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.LOGIN_FAILED'), 'error');
} }
return true; return true;
@@ -156,7 +146,7 @@ class AdminController
protected function taskLogout() protected function taskLogout()
{ {
$this->admin->session()->invalidate()->start(); $this->admin->session()->invalidate()->start();
$this->admin->setMessage($this->translate('PLUGIN_ADMIN.LOGGED_OUT'), 'info'); $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.LOGGED_OUT'), 'info');
$this->setRedirect('/logout'); $this->setRedirect('/logout');
return true; return true;
@@ -175,19 +165,19 @@ class AdminController
$user = !empty($username) ? User::load($username) : null; $user = !empty($username) ? User::load($username) : null;
if (!isset($this->grav['Email'])) { if (!isset($this->grav['Email'])) {
$this->admin->setMessage($this->translate('PLUGIN_ADMIN.FORGOT_EMAIL_NOT_CONFIGURED'), 'error'); $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.FORGOT_EMAIL_NOT_CONFIGURED'), 'error');
$this->setRedirect('/'); $this->setRedirect('/');
return true; return true;
} }
if (!$user || !$user->exists()) { if (!$user || !$user->exists()) {
$this->admin->setMessage($this->translate(['PLUGIN_ADMIN.FORGOT_USERNAME_DOES_NOT_EXIST', $username]), 'error'); $this->admin->setMessage($this->admin->translate(['PLUGIN_ADMIN.FORGOT_USERNAME_DOES_NOT_EXIST', $username]), 'error');
$this->setRedirect('/forgot'); $this->setRedirect('/forgot');
return true; return true;
} }
if (empty($user->email)) { if (empty($user->email)) {
$this->admin->setMessage($this->translate(['PLUGIN_ADMIN.FORGOT_CANNOT_RESET_EMAIL_NO_EMAIL', $username]), 'error'); $this->admin->setMessage($this->admin->translate(['PLUGIN_ADMIN.FORGOT_CANNOT_RESET_EMAIL_NO_EMAIL', $username]), 'error');
$this->setRedirect('/forgot'); $this->setRedirect('/forgot');
return true; return true;
} }
@@ -206,8 +196,8 @@ class AdminController
$from = $this->grav['config']->get('plugins.email.from', 'noreply@getgrav.org'); $from = $this->grav['config']->get('plugins.email.from', 'noreply@getgrav.org');
$to = $user->email; $to = $user->email;
$subject = $this->translate(['PLUGIN_ADMIN.FORGOT_EMAIL_SUBJECT', $sitename]); $subject = $this->admin->translate(['PLUGIN_ADMIN.FORGOT_EMAIL_SUBJECT', $sitename]);
$content = $this->translate(['PLUGIN_ADMIN.FORGOT_EMAIL_BODY', $fullname, $reset_link, $author, $sitename]); $content = $this->admin->translate(['PLUGIN_ADMIN.FORGOT_EMAIL_BODY', $fullname, $reset_link, $author, $sitename]);
$body = $this->grav['twig']->processTemplate('email/base.html.twig', ['content' => $content]); $body = $this->grav['twig']->processTemplate('email/base.html.twig', ['content' => $content]);
@@ -218,9 +208,9 @@ class AdminController
$sent = $this->grav['Email']->send($message); $sent = $this->grav['Email']->send($message);
if ($sent < 1) { if ($sent < 1) {
$this->admin->setMessage($this->translate('PLUGIN_ADMIN.FORGOT_FAILED_TO_EMAIL'), 'error'); $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.FORGOT_FAILED_TO_EMAIL'), 'error');
} else { } else {
$this->admin->setMessage($this->translate(['PLUGIN_ADMIN.FORGOT_INSTRUCTIONS_SENT_VIA_EMAIL', $to]), 'info'); $this->admin->setMessage($this->admin->translate(['PLUGIN_ADMIN.FORGOT_INSTRUCTIONS_SENT_VIA_EMAIL', $to]), 'info');
} }
$this->setRedirect('/'); $this->setRedirect('/');
@@ -247,7 +237,7 @@ class AdminController
if ($good_token === $token) { if ($good_token === $token) {
if (time() > $expire) { if (time() > $expire) {
$this->admin->setMessage($this->translate('PLUGIN_ADMIN.RESET_LINK_EXPIRED'), 'error'); $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.RESET_LINK_EXPIRED'), 'error');
$this->setRedirect('/forgot'); $this->setRedirect('/forgot');
return true; return true;
} }
@@ -260,13 +250,13 @@ class AdminController
$user->filter(); $user->filter();
$user->save(); $user->save();
$this->admin->setMessage($this->translate('PLUGIN_ADMIN.RESET_PASSWORD_RESET'), 'info'); $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.RESET_PASSWORD_RESET'), 'info');
$this->setRedirect('/'); $this->setRedirect('/');
return true; return true;
} }
} }
$this->admin->setMessage($this->translate('PLUGIN_ADMIN.RESET_INVALID_LINK'), 'error'); $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.RESET_INVALID_LINK'), 'error');
$this->setRedirect('/forgot'); $this->setRedirect('/forgot');
return true; return true;
@@ -275,7 +265,7 @@ class AdminController
$token = $this->grav['uri']->param('token'); $token = $this->grav['uri']->param('token');
if (empty($user) || empty($token)) { if (empty($user) || empty($token)) {
$this->admin->setMessage($this->translate('PLUGIN_ADMIN.RESET_INVALID_LINK'), 'error'); $this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.RESET_INVALID_LINK'), 'error');
$this->setRedirect('/forgot'); $this->setRedirect('/forgot');
return true; return true;
} }

View File

@@ -118,7 +118,7 @@ class Popularity
$data = array(); $data = array();
foreach ($chart_data as $date => $count) { foreach ($chart_data as $date => $count) {
$labels[] = self::getGrav()['grav']['language']->translate(['PLUGIN_ADMIN.' . strtoupper(date('D', strtotime($date)))]); $labels[] = self::getGrav()['grav']['admin']->translate(['PLUGIN_ADMIN.' . strtoupper(date('D', strtotime($date)))]);
$data[] = $count; $data[] = $count;
} }