php 在 Laravel 上本地化日期的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20057450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the best way to localise a date on Laravel?
提问by Aristona
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
它返回Nov。我需要将其本地化为我的语言,所以输出应该是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?
实现这一目标的最佳方法是什么?
采纳答案by Marco Florian
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.
使用库作为 Laravel-Date,您只需要在 Laravel 应用程序配置文件中设置应用程序的语言,并使用其函数根据需要设置日期格式。
Set the language in /app/config/app.php
在 /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:
这是存储库的链接:
To install this library you can follow the instructions detailed in the following link:
要安装此库,您可以按照以下链接中的详细说明进行操作:
回答by Brian Glaz
When you retrieve a date off a model in Laravel, you get back a Carbon object: https://github.com/briannesbitt/Carbon
当您从 Laravel 中的模型中检索日期时,您会得到一个 Carbon 对象:https: //github.com/briannesbitt/Carbon
If you refer to the Carbon documentation, it tells you how you can get a locale formatted Date:
如果您参考 Carbon 文档,它会告诉您如何获取区域设置格式的日期:
Unfortunately the base class DateTime does not have any localization support. To begin localization support a formatLocalized($format) method has been added. The implementation makes a call to strftime using the current instance timestamp. If you first set the current locale with setlocale() then the string returned will be formatted in the correct locale.
不幸的是,基类 DateTime 没有任何本地化支持。为了开始本地化支持,已添加 formatLocalized($format) 方法。该实现使用当前实例时间戳调用 strftime 。如果您首先使用 setlocale() 设置当前语言环境,则返回的字符串将被格式化为正确的语言环境。
setlocale(LC_TIME, 'German');
echo $dt->formatLocalized('%A %d %B %Y'); // Donnerstag 25 Dezember 1975
setlocale(LC_TIME, '');
echo $dt->formatLocalized('%A %d %B %Y'); // Thursday 25 December 1975
So basically, just use formatLocalized('M')instead of format('M')
所以基本上,只需使用formatLocalized('M')代替format('M')
回答by zub0r
It is also a good idea to set locale globally for each request (eg. in filters.php) when using Carbon date instances.
在使用 Carbon 日期实例时,为每个请求(例如在 filters.php 中)全局设置区域设置也是一个好主意。
App::before(function($request) {
setlocale(LC_TIME, 'sk_SK.utf8');
});
and then proceed as usual
然后照常进行
$dt->formatLocalized('%b'); // $dt is carbon instance
回答by Joshua - Pendo
In addition to the accepted answer, I was looking for a way to use this within Blade using the Carbon instance of created_at for example. The class in the accepted answer didn't seem to accept a Carbon instance as date, but rather parsed a date from a string.
除了接受的答案之外,我正在寻找一种方法,例如使用 created_at 的 Carbon 实例在 Blade 中使用它。接受的答案中的类似乎不接受 Carbon 实例作为日期,而是从字符串中解析日期。
I wrote a little helper function to shorten the code in the blade templates:
我写了一个小辅助函数来缩短刀片模板中的代码:
function localeDate($date, $format)
{
return Jenssegers\Date\Date::createFromFormat('d-m-Y H:i:s', $date->format('d-m-Y H:i:s'))->format($format);
}
Within Blade you can now use:
在 Blade 中,您现在可以使用:
{{ localeDate($model->created_at, 'F') }}
Which would return the fully written name of the month in the locale set in config/app.php
这将返回 config/app.php 中设置的语言环境中月份的完整书写名称
If there's a better way (or I missed something in the code), please let me know. Otherwise this might be helpfull for others.
如果有更好的方法(或者我错过了代码中的某些内容),请告诉我。否则这可能对其他人有帮助。
回答by apoq
回答by Harhukam Singh
Goto your App->Config->app.php put
转到您的 App->Config->app.php put
'timezone' => 'Asia/Kolkata',
use simple {{$post->created_at->diffForHumans()}}
使用简单 {{$post->created_at->diffForHumans()}}

