Laravel 5 Carbon 全球语言环境
Laravel 5 Carbon global Locale
我正在尝试设置与 laravel 相同的全局区域设置,即:
config('app.locale')
使用 Carbon。
看来您可以使用以下任一方法来完成:
Carbon::setLocale('fr')
或
setlocale(LC_TIME, 'theLocale');
所以我尝试使用中间件或提供程序但没有成功。
(为什么这不是 laravel 的默认功能?)
所以这是我的错,Carbon 实际上使用的是 php
setlocale();
Carbon::setLocale('fr')
方法仅适用于
->diffForHumans()
方法。
请注意 php setlocale() 对存储在 OS 上的语言环境的引用
选择已安装的一种使用
locale -a
在您的主机上
其次,你必须使用
->formatLocalized()
方法代替
->format()
方法
最后是所有有用的方法,例如
->toDateString()
->toFormattedDateString()
->toTimeString()
->toDateTimeString()
->toDayDateTimeString()
未本地化
最后你必须使用这些解析字母
我在AppServiceProvider里配置的。
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
// Localization Carbon
\Carbon\Carbon::setLocale(config('app.locale'));
}
}
使用全球本地化格式翻译碳日期
测试时间:Laravel 5.8、Laravel 6、Laravel 8
在config/app.php
'locale' => 'id', // The default is 'en', but this time I want localize them to Indonesian (ID)
然后,要使语言环境输出执行如下操作:
// WITHOUT LOCALE
Carbon\Carbon::parse('2019-03-01')->format('d F Y'); //Output: "01 March 2019"
now()->subMinute(5)->diffForHumans(); // Output: "5 minutes ago"
// WITH LOCALE
Carbon\Carbon::parse('2019-03-01')->translatedFormat('d F Y'); // Output: "01 Maret 2019"
now()->subMinute(5)->diffForHumans(); // Output: "5 menit yang lalu"
有关转换本地化日期的更多信息,您可以在下面查看 link
https://carbon.nesbot.com/docs/#api-localization
我正在尝试设置与 laravel 相同的全局区域设置,即:
config('app.locale')
使用 Carbon。
看来您可以使用以下任一方法来完成:
Carbon::setLocale('fr')
或
setlocale(LC_TIME, 'theLocale');
所以我尝试使用中间件或提供程序但没有成功。
(为什么这不是 laravel 的默认功能?)
所以这是我的错,Carbon 实际上使用的是 php
setlocale();
Carbon::setLocale('fr')
方法仅适用于
->diffForHumans()
方法。 请注意 php setlocale() 对存储在 OS 上的语言环境的引用 选择已安装的一种使用
locale -a
在您的主机上
其次,你必须使用
->formatLocalized()
方法代替
->format()
方法
最后是所有有用的方法,例如
->toDateString()
->toFormattedDateString()
->toTimeString()
->toDateTimeString()
->toDayDateTimeString()
未本地化
最后你必须使用这些解析字母
我在AppServiceProvider里配置的。
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
// Localization Carbon
\Carbon\Carbon::setLocale(config('app.locale'));
}
}
使用全球本地化格式翻译碳日期
测试时间:Laravel 5.8、Laravel 6、Laravel 8
在config/app.php
'locale' => 'id', // The default is 'en', but this time I want localize them to Indonesian (ID)
然后,要使语言环境输出执行如下操作:
// WITHOUT LOCALE
Carbon\Carbon::parse('2019-03-01')->format('d F Y'); //Output: "01 March 2019"
now()->subMinute(5)->diffForHumans(); // Output: "5 minutes ago"
// WITH LOCALE
Carbon\Carbon::parse('2019-03-01')->translatedFormat('d F Y'); // Output: "01 Maret 2019"
now()->subMinute(5)->diffForHumans(); // Output: "5 menit yang lalu"
有关转换本地化日期的更多信息,您可以在下面查看 link https://carbon.nesbot.com/docs/#api-localization