Answer the question
In order to leave comments, you need to log in
How to set language for Carbon date localization?
Hello. I am engaged in the internalization of the project on Yii2. There was a need to translate dates and times. I use Yii2 Locale URLs for internalization and Carbon for working with dates .
Previously, when the language was only Russian, I did it this way ( web/index.php ):
<?php
use Carbon\Carbon;
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');
require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
$config = require __DIR__ . '/../config/web.php';
Carbon::setLocale('ru');
(new yii\web\Application($config))->run();
'as afterRequest' => [
'class' => 'app\components\CarbonLang',
],
class CarbonLang extends \yii\base\Behavior
{
public function events()
{
return [
Application::EVENT_AFTER_REQUEST => 'setCarbonLocale'
];
}
public function setCarbonLocale($event)
{
Carbon::setLocale(Yii::$app->language);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question