在Laravel上定位日期的最佳方法是什么? [英] What's the best way to localise a date on Laravel?

查看:81
本文介绍了在Laravel上定位日期的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以这个例子为例:

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

它返回Nov.我需要将此语言本地化为我的语言,因此输出应为Kas

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

请考虑以下事项:

{{ 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?

推荐答案

使用库作为Laravel-Date,您只需在Laravel应用配置文件中设置应用的语言,并使用其功能将日期格式化为你想要的.

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.

在/app/config/app.php中设置语言

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

这是存储库的链接:

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

这篇关于在Laravel上定位日期的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆