无法修改 Lumen 中的本地化
Impossible to modify the localization in Lumen
这个题目已经被放置并解决了,但是随着Lumen更新到5.1版本,它不再有效了!
根据我问的问题 , and ,我们只需要添加:
app()->setLocale('fr');
到bootsrap/app.php
或在控制器功能中使更改生效。
不过今天好像不行了
应用上面的代码后,我应该将我的日期更改为法语,但我仍然得到英语格式(例如:Wednesday 18 November 2015
而不是 Mercredi 18 Novembre 2015
)。
我们将不胜感激您的帮助
我认为你的问题是关于 Carbon 的。您可以阅读 the documentation 和 Carbon 示例:
setlocale(LC_TIME, 'German');
echo $dt->formatLocalized('%A %d %B %Y'); // Mittwoch 21 Mai 1975
setlocale(LC_TIME, '');
echo $dt->formatLocalized('%A %d %B %Y'); // Wednesday 21 May 1975
// In your case (to be more accurate) :
setlocale(LC_TIME, 'French');
ucfirst($dt->formatLocalized('%A %d %B %Y à %Hh%M')); // Mercredi 21 mai 2015 à 14h25
你应该像这样在 lumen 中使用 setlocale(当然在 bootstrap/app.php 文件中):
require_once __DIR__ . '/../vendor/autoload.php';
Dotenv::load(__DIR__ . '/../');
setlocale(LC_TIME, 'fr_FR.utf8', 'fr_FR.utf-8', 'fr_FR@euro', 'fr_FR', 'fr', 'French');
或者您可以使用此包:https://github.com/jenssegers/date 并使用:
在 bootstrap/app.php 文件中:
// ...
$app->register(Jenssegers\Date\DateServiceProvider::class);
class_alias(Jenssegers\Date\Date::class, 'Date');
Date::setLocale(env('APP_LOCALE'));
并且在你的模型中:
// ...
protected $dates = ['x_date'];
public function getXDateAttribute() {
return Date::parse($this->attributes['x_date'])
->format('l j F Y H:i:s');
}
public function setXDateAttribute($date) {
$this->attributes['x_date'] =
Date::createFromFormat('l j F Y H:i:s', $date);
}
这个题目已经被放置并解决了,但是随着Lumen更新到5.1版本,它不再有效了!
根据我问的问题
app()->setLocale('fr');
到bootsrap/app.php
或在控制器功能中使更改生效。
不过今天好像不行了
应用上面的代码后,我应该将我的日期更改为法语,但我仍然得到英语格式(例如:Wednesday 18 November 2015
而不是 Mercredi 18 Novembre 2015
)。
我们将不胜感激您的帮助
我认为你的问题是关于 Carbon 的。您可以阅读 the documentation 和 Carbon 示例:
setlocale(LC_TIME, 'German');
echo $dt->formatLocalized('%A %d %B %Y'); // Mittwoch 21 Mai 1975
setlocale(LC_TIME, '');
echo $dt->formatLocalized('%A %d %B %Y'); // Wednesday 21 May 1975
// In your case (to be more accurate) :
setlocale(LC_TIME, 'French');
ucfirst($dt->formatLocalized('%A %d %B %Y à %Hh%M')); // Mercredi 21 mai 2015 à 14h25
你应该像这样在 lumen 中使用 setlocale(当然在 bootstrap/app.php 文件中):
require_once __DIR__ . '/../vendor/autoload.php';
Dotenv::load(__DIR__ . '/../');
setlocale(LC_TIME, 'fr_FR.utf8', 'fr_FR.utf-8', 'fr_FR@euro', 'fr_FR', 'fr', 'French');
或者您可以使用此包:https://github.com/jenssegers/date 并使用:
在 bootstrap/app.php 文件中:
// ...
$app->register(Jenssegers\Date\DateServiceProvider::class);
class_alias(Jenssegers\Date\Date::class, 'Date');
Date::setLocale(env('APP_LOCALE'));
并且在你的模型中:
// ...
protected $dates = ['x_date'];
public function getXDateAttribute() {
return Date::parse($this->attributes['x_date'])
->format('l j F Y H:i:s');
}
public function setXDateAttribute($date) {
$this->attributes['x_date'] =
Date::createFromFormat('l j F Y H:i:s', $date);
}