dongronge3732 2013-11-18 20: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 17: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条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值