S
S
Sergei Gurdjiyan2017-08-30 13:26:33
Internationalization and localization
Sergei Gurdjiyan, 2017-08-30 13:26:33

How to do Laravel date localization?

There is a date display
{{ $article->created_at->format('d M Y') }}
on a multilingual site, 5 languages.
Is there a way to localize the months?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dude, 2017-09-06
@mrKorg

Yes. I myself recently encountered this problem, it is solved trivially if the thing is hosted on Linux, but problems arise if on Windows.
First, in the place where you do it, you app()->setLocale()need to additionally call the native php function to change the locale:
Here:
But remember that the OS on which the project is hosted must have the appropriate locales.
Further, if it $article->created_atreturns Carbonan object, then instead format()you need to call
. For convenience, you can put this transformation in an accessor, if necessary.
I hope you are using utf-8. And there may be problems with this if the project is hosted on Windows, as I said above. The fact is that Cyrillic locales in utf-8 are not always on board, and the only way that can be used without resorting to dancing around the OS (that is, so that the project can be deployed without pain in any environment) is to wrap the returned value in iconv(). In the same accessor, for example.
Accessor implementation example:

public function getСreatedAtAttribute($value){
    return iconv('cp1251', 'utf-8', Carbon::parse($value)->formatLocalized("%d %B %Y"));
}

But there may be problems if the system still has a corresponding UTF-8 locale, but here the behavior of iconv is absolutely unpredictable - I had cases when the conversion did not occur in this case, and everything was fine, but it happened that if the encoding was already correct , then iconv broke everything to hell and returned unreadable krakozyabry.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question