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

@@ -76,7 +76,6 @@ class AdminController
$this->post = $this->getPost($post);
$this->route = $route;
$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);
}
/**
* 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.
*
@@ -142,7 +132,7 @@ class AdminController
if ($this->admin->authenticate($this->post)) {
// should never reach here, redirects first
} else {
$this->admin->setMessage($this->translate('PLUGIN_ADMIN.LOGIN_FAILED'), 'error');
$this->admin->setMessage($this->admin->translate('PLUGIN_ADMIN.LOGIN_FAILED'), 'error');
}
return true;
@@ -156,7 +146,7 @@ class AdminController
protected function taskLogout()
{
$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');
return true;
@@ -175,19 +165,19 @@ class AdminController
$user = !empty($username) ? User::load($username) : null;
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('/');
return true;
}
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');
return true;
}
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');
return true;
}
@@ -206,8 +196,8 @@ class AdminController
$from = $this->grav['config']->get('plugins.email.from', 'noreply@getgrav.org');
$to = $user->email;
$subject = $this->translate(['PLUGIN_ADMIN.FORGOT_EMAIL_SUBJECT', $sitename]);
$content = $this->translate(['PLUGIN_ADMIN.FORGOT_EMAIL_BODY', $fullname, $reset_link, $author, $sitename]);
$subject = $this->admin->translate(['PLUGIN_ADMIN.FORGOT_EMAIL_SUBJECT', $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]);
@@ -218,9 +208,9 @@ class AdminController
$sent = $this->grav['Email']->send($message);
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 {
$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('/');
@@ -247,7 +237,7 @@ class AdminController
if ($good_token === $token) {
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');
return true;
}
@@ -260,13 +250,13 @@ class AdminController
$user->filter();
$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('/');
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');
return true;
@@ -275,7 +265,7 @@ class AdminController
$token = $this->grav['uri']->param('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');
return true;
}