A
A
Alexander Ivanov2015-05-21 15:09:52
Yii
Alexander Ivanov, 2015-05-21 15:09:52

Where are the 404 and 403 error files in Yii2 (I want to translate these pages)?

In the file error.php in the views folder, I have already translated part of the text.

<div class="site-error">

    <h1><?= Html::encode($this->title) ?></h1>

    <div class="alert alert-danger">
        <?= nl2br(Html::encode($message)) ?>

But I want to translate these entries as well ->
Forbidden (#403)
You are not allowed to access this page.
Not Found (#404)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Ivanov, 2015-05-21
@cimonlebedev

Figured it out myself:
vendor\yiisoft\yii2-gii\Module.php

if (Yii::$app instanceof \yii\web\Application && !$this->checkAccess()) {
            throw new ForbiddenHttpException('You are not allowed to access this page.');
        }

I
Igor Vasiliev, 2017-06-20
@Isolution666

Hello fellow programmers.
---
The code is not as scary as it is interpreted ))
Oddly enough, I found a hint for this question in the "views/site/"
folder File: error.php
===
The developers realized that programmers would face such a question, so they left hints:

<?php

use yii\helpers\Html;

/* @var $this yii\web\View */
/* @var $name string */
/* @var $message string */
/* @var $exception Exception */

$this->title = $name;
?>

From them we see that the variable <?=$name?>
gives us <?=$this->title;?>
And this name fell on our heads from the View class!
After refactoring, I realized what the <?=$message;?>
Yii2 language pack is responsible for /
Namely. /vendor/yiisoft/yii2/messages/ru/
Unless, of course, you specified the Russian language in the main.php file config. There are a bunch of them, you can make sure of this, and the class itself showed that the message substitution model is used, through the construction: <?=Yii::t('yii','значение');?>
You ask, why the hell do you need such hemorrhoids ??
I answer, firstly, this is not hemorrhoids, but a very cool solution! Kills two birds with one stone! Multilanguage - one, Repeating elements - two! Thanks to this solution, you don’t have to pick the whole code, it’s enough to replace the replacing elements with such a design, and that’s all, I changed the text or design of the “update” button, I did it in one place, in the yii.php file and that’s all, the design and text changed throughout the site, and edited one file!!! It's incredibly convenient. So it is with errors and messages, why write on each page with pens "your message was successfully sent", if you can write the value = translation. So, in the head of the code there was another hint /* @var $exception Exception */
and in the class itself a link to the php7 manual - php.net/manual/ru/exception.getmessage.php
Thus we have:
class HttpException extends UserException
{
    /**
     * @var int HTTP status code, such as 403, 404, 500, etc.
     */
    public $statusCode;


    /**
     * Constructor.
     * @param int $status HTTP status code, such as 404, 500, etc.
     * @param string $message error message
     * @param int $code error code
     * @param \Exception $previous The previous exception used for the exception chaining.
     */
    public function __construct($status, $message = null, $code = 0, \Exception $previous = null)
    {
        $this->statusCode = $status;
        parent::__construct($message, $code, $previous);
    }

    /**
     * @return string the user-friendly name of this exception
     */
    public function getName()
    {
        if (isset(Response::$httpStatuses[$this->statusCode])) {
            return Response::$httpStatuses[$this->statusCode];
        } else {
            return 'Error';
        }
    }
}

Well, since we can specify the error code, public $statusCode;
then we can stupidly set the conditions, for example:
<?=($exception->statusCode == '404') ? $this->title = "Ошибка 404" : ''; ?>
<?=($exception->statusCode == '403') ? $this->title = "Доступ запрещён" : ''; ?>
<?=($exception->statusCode == '500') ? $this->title = "Внутренняя ошибка сервера" : ''; ?>

And the text of the error under the title is edited in the language pack:
<?php
return [
...
'Page not found.' => 'Страница не найдена.',
'You are not allowed to perform this action.' => 'Вам не разрешено производить данное действие.',
'An internal server error occurred.' => 'Возникла внутренняя ошибка сервера.',
...
];

It is displayed as <?=Yii::t('yii', 'Page not found.');?> и т д...
But you don't need to display anything, the class itself will determine the error code and display its text.
How to replace the title of the error I have already written above.
--
This was a complete breakdown of the error page, thank you all for your attention. Till :)

M
Maxim Grechushnikov, 2015-05-21
@maxyc_webber

in the application config
sourceLanguage => en
language => ru
ps read the documentation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question