Laravel 5 Carbon global Locale

前端 未结 3 1160
有刺的猬
有刺的猬 2020-12-10 12:28

I\'m trying to set the same global locale of laravel which is :

config(\'app.locale\')

to work with Carbon.

It seems like you can d

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 13:07

    in AppServiceProvider

    public function register()
    {
    
        // For example im gonna locale all dates to Indonesian (ID)
        config(['app.locale' => 'id']);
        \Carbon\Carbon::setLocale('id');
    }
    

    then to make locale output do something like this

    // Without locale, the output gonna be like this    
    Carbon\Carbon::parse('2019-03-01')->format('d F Y'); //Output: "01 March 2019"
    
    // With locale
    Carbon\Carbon::parse('2019-03-01')->translatedFormat('d F Y'); //Output: "01 Maret 2019"
    

    For more information about converting localize dates you can see on below link https://carbon.nesbot.com/docs/#api-localization

提交回复
热议问题