dongronge3732 2013-11-18 12:41
浏览 60
已采纳

什么是在Laravel上本地化日期的最佳方式?

Take this example:

{{ $article->created_at->format('M') }}

It returns Nov. I need to localise this to my language, so the output should be Kas

Thought about doing the following:

{{ trans("language.{$article->created_at->format('M')}") }}

app/lang/tr/language.php -> 'nov' => 'kas'

This looks like reinventing the wheel and programmatically pretty terrible. I'm sure there are some localisation standards. Something like:

{{ $article->created_at->format('M')->localiseTo('tr_TR') }}

What's the best way to achieve this?

  • 写回答

6条回答 默认 最新

  • douduoting8408 2014-09-19 09:40
    关注

    Using a library as Laravel-Date you will just need to set the language of the app in the Laravel app config file and use its functions to format the date as you want.

    Set the language in /app/config/app.php

    'locale' => 'es',
    

    I've found this library pretty useful and clean. To use it, you can write something like the example in the library readme file. I leave the results in spanish.

    echo Date::now()->format('l j F Y H:i:s'); // domingo 28 abril 2013 21:58:16
    
    echo Date::parse('-1 day')->diffForHumans(); // 1 día atrás
    

    This is the link to the repository:

    https://github.com/jenssegers/laravel-date

    To install this library you can follow the instructions detailed in the following link:

    https://github.com/jenssegers/laravel-date#installation

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部