Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

php - What's the best way to localise a date on 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...